Search code examples
javaswingjbuttonjtextfield

How to use multiple button with multiple jtextField


I'm newbie in Java programming.

I'm trying to test a program where there is at least four(4) JButton and there are at least three(3) JTextField. Let say Button A,B,C, and D, then Textfield 1, 2, and 3.

What I want to happen is that: when I click any of the Button. The first Textfield, let's say 1 will hold a string text. And when I click another button the second Textfield will now hold the string since 1 already holds a string text. However the tricky part is when all the textfields holds a string. When i click a button again, "textfield 1" will be overwritten from the clicked button. Then 2 and the cycle will be repeated.

package testing;


public class NewJFrame extends javax.swing.JFrame {


public NewJFrame() {
    initComponents();
}

 private void initComponents() {

    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();
    jButton4 = new javax.swing.JButton();
    jTextField1 = new javax.swing.JTextField();
    jTextField2 = new javax.swing.JTextField();
    jTextField3 = new javax.swing.JTextField();
    jTextField4 = new javax.swing.JTextField();

}                 

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
   jTextField1.setText("A");

}                                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    jTextField1.setText("B");
}                                        

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    jTextField1.setText("C");
}                                        

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    jTextField1.setText("D");
}                                        


public static void main(String args[]) {

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new NewJFrame().setVisible(true);
        }
    });
}

Solution

  • Please note that this code was written before the OP posted his code, but I think it will still solve the issue and therefore be worth an answer


    I just created an example that should help you with this, it will work with any amount of buttons and textfields (except it's <= 0).

    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.ArrayList;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    
    public class Examplerino {
    
        ArrayList<JTextField> tfs = new ArrayList<JTextField>();
        ArrayList<JButton> bts = new ArrayList<JButton>();
    
        int counter = 0;
    
        int amountBts = 4;
        int amountTfs = 3;
    
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new Examplerino();
                }
            });
        }
    
        public Examplerino() {
    
            JFrame frame = new JFrame();
            frame.getContentPane().setLayout(new FlowLayout());
    
            // Add new buttons to bts
            for (int i = 0; i < amountBts; i++) {
                bts.add(new JButton("b" + (i + 1)));
            }
    
            // Add new textfields to tfs
            for (int i = 0; i < amountTfs; i++) {
                tfs.add(new JTextField(3));
            }
    
            // Add buttons with action listener to the frame
            for (JButton b : bts) {
                b.addActionListener(new ButtonActionListener());
                frame.getContentPane().add(b);
            }
    
            // Add textfields to frame
            for (JTextField tf : tfs) {
                tf.setEditable(false);
                frame.getContentPane().add(tf);
            }
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(500, 500);
            frame.setVisible(true);
    
        }
    
        public class ButtonActionListener implements ActionListener {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                if (counter >= tfs.size()) {
                    counter = 0;
                }
                tfs.get(counter).setText(((JButton) e.getSource()).getText());
                counter++;
            }
        }
    
    }