'check a button color in another button
JToggleButton start = new JToggleButton("start block");
start.setBounds(50, 50, 100, 100);
JToggleButton end = new JToggleButton("end block");
end.setBounds(50, 50, 100, 100);
CreateSelectionButtons(frame, panelOnTheLeft, size, end,start);
public static void CreateSelectionButtons(JFrame frame, JPanel panelOnTheLeft, int[] size, AbstractButton end,AbstractButton start) {
int buttonNum = size[0] * size[1];
JButton[] buttons = new JButton[buttonNum];
class Actions implements ActionListener {
public void actionPerformed(ActionEvent e) {
// Get the button that was clicked
JButton theButton = (JButton) e.getSource();
// Set it's background color to white
Color color = theButton.getBackground();
if(start.isSelected()){
for(int i=0;i<=buttonNum;i++){
Color check=buttons[i].getBackground();
if(check==Color.green){
buttons[i].setBackground(Color.white);
}
}
theButton.setBackground(Color.green);}
else if(end.isSelected()){
for(int i=0;i<=buttonNum;i++){
Color check=buttons[i].getBackground();
if(check==Color.red){
buttons[i].setBackground(Color.white);
}
}
theButton.setBackground(Color.red);}
else if (color==Color.black)
{
theButton.setBackground(Color.white);}
else{theButton.setBackground(Color.black);
}
}}
public static void main(String[] args) {
JFrame frame = new JFrame("Maze Program");
JPanel panelOnTheLeft = new JPanel();
JPanel panelOnTheRight = new JPanel();
frame.setLayout(new BorderLayout());
CreateSelectionButtons(frame, panelOnTheLeft, defaultSize, null,null);
CreateInputSection(frame, panelOnTheLeft, panelOnTheRight);
frame.add(panelOnTheLeft, BorderLayout.WEST);
frame.add(panelOnTheRight, BorderLayout.EAST);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
i want the button on the left will change color depent on the the button i clicked (start and end),but i get
exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "javax.swing.AbstractButton.isSelected()" because "this.val$start" is null)
what should i do
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|