java swing get or set unique id for new JButton while create thim on for loop

Viewed 288

i have java swing Class that create JButton

it is working but what i need is when i pressed a JButton lets seed that it is number 1 the code in the ActionEvent is to change the Background of the JButton but what i need is if i pressed another JButton the first one i need it to go back to red Color :

Example :

  package Classes;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

 

       public class testbtn {
        
            public JFrame frame = new JFrame();
        
            public int copcounter = 5;
        
            public testbtn() {
        
                JPanel jdb = new JPanel();
        
                jdb.setLayout(new FlowLayout());
        
                for (int x = 1; x <= copcounter; x++) {
        
                    JButton btn = new JButton();
        
                    btn.setText(String.valueOf(x));
        
                    if (x == 1) {
        
                        btn.setBackground(Color.yellow);
        
                    } else {
        
                        btn.setBackground(Color.red);
        
                    }
        
                    btn.putClientProperty("id", x);
        
                    btn.addActionListener((ActionEvent e) -> {
        
                        btn.setBackground(Color.yellow);
        
                        System.out.println(e.getID());
        
                    });
        
                    jdb.add(btn);
        
                }
        
                frame.add(jdb);
        
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
        
            }
        
            public static void main(String[] args) {
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        new testbtn();
                    }
                });
            }
        }

this code will show form like this :

enter image description here

when i press JButton 4 the output become like this :

enter image description here

but i need it to be like this so the other button will become red color but what i pressed i need it to become yellow ! :

enter image description here

i know that i can use id but how to get the id for them ! or if there is better way ?

1 Answers

No need for an id, simply put all the JButtons into a List<JButton>, say called buttonList, and then iterate through the list in the button's ActionListener, turning backgrounds red for all the buttons in the list, and then turn the current button's background yellow.

And then in the code that uses it:

public void actionPerformed(ActionEvent e) {
    // iterate through the list
    for (JButton button : buttonList) {
        button.setBackground(Color.RED);
    }
    
    // then set *this* button's color yellow:
    ((JButton) e.getSource).setBackground(Color.YELLOW);
}

That's it

or for your code...

public class TestBtn {
    public JFrame frame = new JFrame();
    public int copcounter = 5;
    private List<JButton> buttonList = new ArrayList<>();

    public TestBtn() {
        JPanel jdb = new JPanel();
        jdb.setLayout(new FlowLayout());
        for (int x = 1; x <= copcounter; x++) {
            JButton btn = new JButton();
            
            // add this
            buttonList.add(btn);
            
            btn.setText(String.valueOf(x));
            if (x == 1) {
                btn.setBackground(Color.yellow);
            } else {
                btn.setBackground(Color.red);
            }
            // btn.putClientProperty("id", x);
            btn.addActionListener((ActionEvent e) -> {
                // iterate through the list
                for (JButton button : buttonList) {
                    button.setBackground(Color.RED);
                }
    
                // then set *this* button's color yellow:
                ((JButton) e.getSource).setBackground(Color.YELLOW);
                
                // show button text
                System.out.println(e.getActionCommand());  
            });
            jdb.add(btn);
        }
        frame.add(jdb);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new testbtn();
            }
        });
    }
}

Also, the ActionEvent's actionCommand String should match the button's text (with some exceptions).

Related