Here are my code where I implemented one button has named "start/stop"
- When I press button once, I got stop as a text on button, but my button colour was not change. When I second time press this button, I got start as a new name of button and got green colour on it. How can I set a red colour when the text on the button is "stop"?
Here is my code.....
//Buttons for start and stop
btnStartStop = new JToggleButton("Start/Stop");
//btnStartStop.setEnabled(false);
btnStartStop.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (chk_start) {
tout_textPane.setText("Stop Magnetic Levitation Project");
btnStartStop.setText("start");
btnStartStop.setBackground(new Color(170, 255, 0));
btnStartStop.setForeground(Color.WHITE);
btnStartStop.setSelected(false);
CommandHandler(".plot");
chk_start = false;
} else {
tout_textPane.setText("Starting Magnetic Levitation Project");
btnStartStop.setText("stop");
btnStartStop.setBackground(Color.RED);
btnStartStop.setForeground(Color.WHITE);
btnStartStop.setSelected(true);
chk_start = true;
}
CommandHandler(".isr");
}
});
btnStartStop.setBounds(27, 10, 210, 21);
panel_ctrl_i.add(btnStartStop);
Works fine for me, and no, you don't seem to be able to get rid of the "selected" highlight, this is something controlled by the look and feel and that's a whole other mess
You should also do a search on jbutton change background as it's basically the same problem