I tried to create an even and then just call said event for each button but it doesn't seem to be working. Down below is the pastebin with the embedded code Just to clarify, there are nine individual buttons. When each button is selected it is supposed to change color. The color to be displayed is shown by its name. However when you execute the code it only alternates between the default colors. As you can see I have tried two different methods to using the ItemEvent and neither of them work.
---------------------
Code starts here
---------------------
package soph.color.arb;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ItemEvent;
import javax.swing.JTextField;
import javax.swing.JToggleButton;
import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class ColorShift extends javax.swing.JFrame {
public JToggleButton jToggleButton1;
public JToggleButton jToggleButton2;
public JToggleButton jToggleButton3;
public JToggleButton jToggleButton4;
public JToggleButton jToggleButton5;
public JToggleButton jToggleButton6;
public JToggleButton jToggleButton7;
public JToggleButton jToggleButton8;
public JToggleButton jToggleButton9;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ColorShift inst = new ColorShift();
inst.setLocationRelativeTo(null);
inst.setVisible(true);
}
});
}
public ColorShift() {
super();
initGUI();
}
private void updateColorIfSelected(JToggleButton btn, ItemEvent ev, Color color) {
if (ev.getStateChange() == ItemEvent.SELECTED) {
btn.setBackground(color);
repaint();
}
else
btn.setBackground(null);
}
public void initGUI() {
try {
FlowLayout thisLayout = new FlowLayout();
getContentPane().setLayout(thisLayout);
setDefaultCloseOperation(WindowConstants.DISPOSE_O N_CLOSE);
jToggleButton1 = new JToggleButton();
getContentPane().add(jToggleButton1);
jToggleButton1.setText("Red");
jToggleButton1.setPreferredSize(new java.awt.Dimension(100, 100));
jToggleButton1.addItemListener(ev -> updateColorIfSelected(jToggleButton1, ev, Color.RED));
jToggleButton2 = new JToggleButton();
getContentPane().add(jToggleButton2);
jToggleButton2.setText("Green");
jToggleButton2.setPreferredSize(new java.awt.Dimension(100, 100));
jToggleButton2.addItemListener(ev -> updateColorIfSelected(jToggleButton2, ev, Color.GREEN));
jToggleButton3 = new JToggleButton();
getContentPane().add(jToggleButton3);
jToggleButton3.setText("Blue");
jToggleButton3.setPreferredSize(new java.awt.Dimension(100, 100));
jToggleButton3.addItemListener(ev -> updateColorIfSelected(jToggleButton3, ev, Color.BLUE));
jToggleButton4 = new JToggleButton();
getContentPane().add(jToggleButton4);
jToggleButton4.setText("Orange");
jToggleButton4.setPreferredSize(new java.awt.Dimension(100, 100));
jToggleButton4.addItemListener(ev -> updateColorIfSelected(jToggleButton4, ev, Color.ORANGE));
jToggleButton5 = new JToggleButton();
getContentPane().add(jToggleButton5);
jToggleButton5.setText("Black");
jToggleButton5.setPreferredSize(new java.awt.Dimension(100, 100));
jToggleButton5.addItemListener(ev -> updateColorIfSelected(jToggleButton5, ev, Color.BLACK));
jToggleButton6 = new JToggleButton();
getContentPane().add(jToggleButton6);
jToggleButton6.setText("Yellow");
jToggleButton6.setPreferredSize(new java.awt.Dimension(100, 100));
jToggleButton6.addItemListener(ev -> updateColorIfSelected(jToggleButton6, ev, Color.YELLOW));
jToggleButton7 = new JToggleButton();
getContentPane().add(jToggleButton7);
jToggleButton7.setText("Magenta");
jToggleButton7.setPreferredSize(new java.awt.Dimension(100, 100));
jToggleButton7.addItemListener(ev -> updateColorIfSelected(jToggleButton7, ev, Color.MAGENTA));
jToggleButton8 = new JToggleButton();
getContentPane().add(jToggleButton8);
jToggleButton8.setText("Pink");
jToggleButton8.setPreferredSize(new java.awt.Dimension(100, 100));
jToggleButton8.addItemListener(ev -> updateColorIfSelected(jToggleButton8, ev, Color.PINK));
jToggleButton9 = new JToggleButton();
getContentPane().add(jToggleButton9);
jToggleButton9.setText("White");
jToggleButton9.setPreferredSize(new java.awt.Dimension(100, 100));
jToggleButton9.addItemListener(ev -> updateColorIfSelected(jToggleButton9, ev, Color.WHITE));
pack();
this.setSize(350, 370);
} catch (Exception e) {
e.printStackTrace();
}
}
}
--- Update ---
https://pastebin.com/7qhJ7GDs
Heres the pastebin just in case its easier to read this way