Search code examples
javaswingjlabel

Swing JLabel text change on the running application


I have a Swing window which contains a button a text box and a JLabel named as flag. According to the input after I click the button, the label should change from flag to some value.

How to achieve this in the same window?


Solution

  • import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.event.*;
    public class Test extends JFrame implements ActionListener
    {
        private JLabel label;
        private JTextField field;
        public Test()
        {
            super("The title");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setPreferredSize(new Dimension(400, 90));
            ((JPanel) getContentPane()).setBorder(new EmptyBorder(13, 13, 13, 13) );
            setLayout(new FlowLayout());
            JButton btn = new JButton("Change");
            btn.setActionCommand("myButton");
            btn.addActionListener(this);
            label = new JLabel("flag");
            field = new JTextField(5);
            add(field);
            add(btn);
            add(label);
            pack();
            setLocationRelativeTo(null);
            setVisible(true);
            setResizable(false);
        }
        public void actionPerformed(ActionEvent e)
        {
            if(e.getActionCommand().equals("myButton"))
            {
                label.setText(field.getText());
            }
        }
        public static void main(String[] args)
        {
            new Test();
        }
    }