How can I focus a JLabel with a mouse click?

Omar Almonte

I am doing a calculator with 3 labels, I can't use text field and I want to know how to focus a JLabel with a click.

Hovercraft Full Of Eels

Simply make the JLabel focusable via myLabel.setFocusable(true);. Then it will gain focus when clicked. You can notify observers of its status by several ways, including by assigning a border to the label, or by changing its text font or color or background color (but then you will need to make the label opaque)

e.g.,

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;

@SuppressWarnings("serial")
public class JLabelFocus extends JPanel {
    private String[] labelTexts = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};

    public JLabelFocus() {
        setPreferredSize(new Dimension(400, 200));
        for (String text : labelTexts) {
            JLabel label = new JLabel(text);
            label.setFocusable(true);
            label.addFocusListener(new FocusAdapter() {
                @Override
                public void focusGained(FocusEvent e) {
                    System.out.println("Focus now on: " + label.getText());
                    label.setBorder(BorderFactory.createLineBorder(Color.RED));
                } 

                @Override
                public void focusLost(FocusEvent e) {
                    label.setBorder(null);
                }
            });
            label.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    label.requestFocusInWindow();
                }
            });
            add(label);
        }
    }

    private static void createAndShowGui() {
        JLabelFocus mainPanel = new JLabelFocus();

        JFrame frame = new JFrame("JLabelFocus");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I set the margin of a JLabel?

How can I move mouse cursor and click?

How i can send mouse click in powershell

How can I disable/enable LineRenderer on mouse click on ogameobject?

How can I focus a button on click with styled-components for React?

How can I handle the Angular (click) event for the middle mouse button?

How to display Image form JTable into JLabel or from database to JLabel using JTable mouse click event?

How can I prevent triggering an on click event when the actual mouse click didn't start on the target element?

How can I remove Textfield focus when I press return or click outside Textfield? (SwiftUI, MacOS)

How can I still capture a mouse click after the ComboBox is disabled?

Middle mouse click emulation on trackpad, how can I set it?

Can I use mouse click to focus on Byobu splits?

How can I stop Flash from leaving full-screen mode when it loses focus due to a mouse-click in the other monitor?

How can I obscure a screen but still view mouse and click?

How can I keep focus on a window after the mouse left said window

How create a JLabel when I pass with mouse on the button

Can't focus on a div (it won't scroll if I scroll with the mouse, unless I click the content)

How can I make a jlabel highlight when a mouse pointer is pointing at it in Java NetBeans

How do I set focus follows mouse?

How do I disable focus follows mouse?

How Can I Make a Javascript Program Simulate a Mouse Click?

In Angular 2, how can I focus to the bottom of a div on button click

How to prevent awesome from changing focus when a mouse click occurs?

How can i get mouse click event element in cocoa

How can I focus or change label color on mouse-hover in Timeline Highchart?

How to programmatically show focus visual of a control in mouse click

How can I perform a click without moving the mouse in python?

How can I focus on textinput when i click on TouchableWithoutFeedback?

How can I call the mouse right click event in SwingGui?