Search code examples
javaswingawtactionlistenerjtextfield

Enter key JTextField


I have a JTextField with an Actionlistener to it. Now I want it to do certain things when I press enter. I am using a shared ActionListener so trying to do a getSource on the JTextField but it doesn't work! Hope anyone can help.

JTextField txtProductAantal = new JTextField(String.valueOf(WinkelApplication.getBasket().getProductAmount(productdelete)));
            txtProductAantal.setBounds(340, verticalPosition + i * productOffset, 40, 20);
            txtProductAantal.addActionListener(this);
            add(txtProductAantal); 

public void actionPerformed(ActionEvent event) {

        if (event.getSource() == btnEmptyBasket) {
            WinkelApplication.getBasket().empty();
            WinkelApplication.getInstance().showPanel(new view.CategoryList());
        }

        if(event.getSource() == txtProductAantal){
            String productgetal = txtProductAantal.getText();
            txtProductAantal.setText(productgetal);
            WinkelApplication.getInstance().showPanel(new view.Payment());
        }
    }

Solution

    • have to create an temporary Object for comparisons

    for example

       public void actionPerformed(ActionEvent event) {
            Object source = event.getSource();
            if (source == btnEmptyBasket) {
               //...........
            } else if (source == txtProductAantal) {
               //........... 
            } else {
    
            }
        }
    
    • or there are only JTextFields (avoiding instanceof inside if - else statement) you can casting Object to JTextField directly

    JTextField source = (JTextField) event.getSource();

    EDIT,

    • one from the next adviced 17 problems.

    • please to read suggestion by @Andrew Thompson, again

      • 1) For better help sooner, post an SSCCE.

      • 2) txtProductAantal.setBounds(..)

      • Use layouts to avoid the next 17 problems.

    my code works as I'm expected

    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    
    public class JTextFieldAndActionListener implements ActionListener {
    
        private JFrame frm = new JFrame("JTextFieldAndActionListener");
        private JTextField one = new JTextField(10);
        private JTextField two = new JTextField();
        private JTextField three = new JTextField();
    
        public JTextFieldAndActionListener() {
            one.addActionListener(this);
            two.addActionListener(this);
            three.addActionListener(this);
            frm.setLayout(new GridLayout());
            frm.add(one);
            frm.add(two);
            frm.add(three);
            frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frm.setLocation(400, 300);
            frm.pack();
            frm.setVisible(true);
        }
    
        public void actionPerformed(ActionEvent event) {
            Object source = event.getSource();
            if (source == one) {
                System.out.println("firing from JTextField one");
            } else if (source == two) {
                System.out.println("firing from JTextField two");
            } else if (source == three) {
                System.out.println("firing from JTextField three");
            } else {
                System.out.println("something went wrong");
            }
        }
    
        public static void main(String[] args) {
    
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    JTextFieldAndActionListener ie = new JTextFieldAndActionListener();
                }
            });
        }
    }
    

    print_out me on ENTER key

    run:
    firing from JTextField one
    firing from JTextField two
    firing from JTextField three
    BUILD SUCCESSFUL (total time: 15 seconds)