Search code examples
javaswingawtjbuttonjtextfield

Removing the text from a JPanel when a certain button is clicked


I am attempting to create a GUI for a text based adventure game I am working on. Something I need to have it do, is that when the button "jButton3" is clicked, it will remove the text of "jText1". I tried adding a ItemListener, but i cannot seem to figure it out. The code is below. For simplicity sake i have left out all of my Imports, as well as my package name, just know that nothing errors out when I attempt to run this program.

I have searched on other posts related to my topic, and could not find exactly what I was looking for, most of them are about replacing the text from the TextField to the JLabel

public class DemoGUI extends javax.swing.JFrame {
    public JLabel jLabel1;
    public JTextField jText1;
    public JButton jButton1;
    public JButton jButton2;
    public JButton jButton3;
    String string1;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                DemoGUI inst = new DemoGUI();
                inst.setLocationRelativeTo(null);
                inst.setVisible(true);
            }
        });
    }
    public DemoGUI() {
        super();
        initGUI();
    }
    private void removeTextWhenClicked(JButton btn, ItemEvent ev) {
        if(ev.getStateChange() == ItemEvent.ITEM_STATE_CHANGED) {
            jText1.setText("");
        }
    }

    public void initGUI() {
        try {
            FlowLayout thisLayout = new FlowLayout();
            getContentPane().setLayout(thisLayout);
            setDefaultCloseOperation(EXIT_ON_CLOSE);

                jLabel1 = new JLabel("Center", SwingConstants.CENTER);
                getContentPane().add(jLabel1);
                jLabel1.setPreferredSize(new Dimension(320, 250));
                jLabel1.setText(string1);
                jLabel1.setBorder(BorderFactory.createLineBorder(Color.BLACK));

                jButton1 = new JButton();
                getContentPane().add(jButton1);
                jButton1.setPreferredSize(new Dimension(100, 50));
                jButton1.setText("Map");

                jButton2 = new JButton();
                getContentPane().add(jButton2);
                jButton2.setPreferredSize(new Dimension(100, 50));
                jButton2.setText("Inventory");

                jText1 = new JTextField();
                getContentPane().add(jText1);
                jText1.setPreferredSize(new Dimension(320, 100));
                jText1.setBorder(BorderFactory.createLineBorder(Color.BLACK));

                jButton3 = new JButton();
                getContentPane().add(jButton3);
                jButton3.setPreferredSize(new Dimension(100, 40));
                jButton3.setText("Submit");
                jButton3.addItemListener(ev -> removeTextWhenClicked(jButton3, ev));

            pack();
            this.setSize(350, 500);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

Solution

  • Instead of using addItemListener, use addActionListener

     jButton3.addActionListener(new ActionListener(){  
        public void actionPerformed(ActionEvent e){  
              
            }  
        });  
    

    You can find the difference at Java: What's the difference between ActionEvent and ItemEvent on a JRadioButton?

    ItemListeners are notified when ever the state of the button is changed, whether through a user interacting with the button or programmatically (via the setSelected method).

    ActionListeners will be called when a user interacts with the button