Home > Software engineering >  How to click on JLabel, edit with JTextField and exit changing Jlabel?
How to click on JLabel, edit with JTextField and exit changing Jlabel?

Time:07-23

I am trying to create a GUI and in that GUI I have different JLabels with a value. I want to be able to click on a JLabel to edit it in my JTextfield (only have 1) and after I press enter it should leave Editing the JLabel. At the moment if I try to edit a JLabel it will change but when I click on the next one the old one will also still change.

This is my code:

public class GUI {
JFrame frame;
int n1=1;
int n2=1;
int n3=1;
GUI(){
    frame=new JFrame();//creating instance of JFrame
    JLabel l1=new JLabel(Integer.toString(n1));
    JLabel l2=new JLabel(Integer.toString(n2));
    JLabel l3=new JLabel(Integer.toString(n3));
    JTextField t=new JTextField();

    l1.setBounds(40,50,100, 40);
    l2.setBounds(40,100,100, 40);
    l3.setBounds(40,150,100, 40);
    t.setBounds(20,200,100, 40);

    frame.add(l1);
    frame.add(l2);
    frame.add(l3);
    frame.add(t);


    l1.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            t.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    l1.setText(t.getText());
                    n1=parseInt(t.getText());
                }
            });
        }
    });

    l2.addMouseListener(new MouseAdapter() {
        @Override
        public void mouseClicked(MouseEvent e) {
            t.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    l2.setText(t.getText());
                    n2=parseInt(t.getText());
                }
            });
        }
    });



    frame.setSize(400,500);//400 width and 500 height
    frame.setLayout(null);//using no layout managers
    frame.setVisible(true);//making the frame visible
}

public static void main(String[] args) {
    new GUI();
}
}

Thanks in advance.

CodePudding user response:

Don't add action listeners for each click. Clicking on a label should record the state of your UI -- that that label is now being edited, and set up the value in the JTextField. Then enter should transfer the value to the JLabel which was recorded as selected.

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class GUI {
    JLabel currentEditLabel = null;
    JFrame frame;
    int n1 = 1;
    int n2 = 1;
    int n3 = 1;

    GUI() {
        frame = new JFrame();//creating instance of JFrame
        JLabel l1 = new JLabel(Integer.toString(n1));
        JLabel l2 = new JLabel(Integer.toString(n2));
        JLabel l3 = new JLabel(Integer.toString(n3));
        JTextField t = new JTextField();

        l1.setBounds(40, 50, 100, 40);
        l2.setBounds(40, 100, 100, 40);
        l3.setBounds(40, 150, 100, 40);
        t.setBounds(20, 200, 100, 40);

        frame.add(l1);
        frame.add(l2);
        frame.add(l3);
        frame.add(t);

        t.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (currentEditLabel != null) {
                    currentEditLabel.setText(t.getText());
                    currentEditLabel = null;
                }
            }
        });

        addMouseListener(l1, t);
        addMouseListener(l2, t);
        addMouseListener(l3, t);

        frame.setSize(400, 500);//400 width and 500 height
        frame.setLayout(null);//using no layout managers
        frame.setVisible(true);//making the frame visible
    }

    private void addMouseListener(JLabel label, JTextField t) {
        label.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                currentEditLabel = (JLabel) e.getComponent();
                t.setText(currentEditLabel.getText());
            }
        });
    }

    public static void main(String[] args) {
        new GUI();
    }
}

Figuring out how to set n1, n2, ... is left as an exercise, as is how to indicate in the UI that no label is selected (hint: should you allow input in the JTextField when no label has been selected?)

  • Related