Home > Blockchain >  How to change button's color?
How to change button's color?

Time:11-11

In my program I've got 2 buttons: the first one changes frame background, the second one changes button background(only for itself). The point is that program should change button background for every button in the program(not only for itself). How am I supposed to to rewrite dialog?

class ButtonBackgroundChange implements ActionListener{
    private JDialog dialog;
    private JColorChooser chooser;
    private Color currentBackground;

    public ButtonBackgroundChange(JButton button1, Component component, Color currentBackground){
        this.currentBackground = currentBackground;
        chooser = new JColorChooser();
        dialog = JColorChooser.createDialog(component, "Background Color", false /* not modal */, chooser, event -> button1.setBackground(chooser.getColor()), null /* no Cancel button listener */);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        chooser.setColor(currentBackground);
        dialog.setVisible(true);
    }
}

CodePudding user response:

If you store your buttons in a list that you pass in to the function you will be able to iterate through it and set the background color for each button.

  • Related