Reputation: 252
Hi I am trying to make java desk top application where I am using 3 JButtons. I want that when I click any JButton it should change the color of that button and if I click on any other JButton then previous clicked button should be like before and recently clicked button change its color until another JButton is clicked
How can I achieve this
here is my button code
b1 = new JButton("Ok");
b1.setBounds(800, 725, 100, 40);
b1.setForeground(Color.BLACK);
c.add(b1);
b2 = new JButton("Print");
b2.setBounds(925, 725, 100, 40);
b2.setForeground(Color.BLACK);
c.add(b2);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
b1.setBackground(Color.BLUE);
JOptionPane.showMessageDialog(frame,"Welcome to allhabad High Court");
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
b2.setBackground(Color.BLUE);
JOptionPane.showMessageDialog(frame,"Welcome to allhabad High Court");
}
});
Upvotes: 0
Views: 251
Reputation: 347184
This is actually more tricky then it sounds. Some look and feels don't use the background
color property when rendering the buttons (for example Windows)
A possible solution might be to use a ButtonGroup
and JToggleButton
, which will ensure that only one button is selected at a time and allow you to monitor the selected states of the buttons, for example...
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JToggleButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class ToggleButton {
public static void main(String[] args) {
new ToggleButton();
}
public ToggleButton() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
final JToggleButton b1 = new JToggleButton("Ok");
b1.setContentAreaFilled(false);
b1.setOpaque(true);
b1.getModel().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (b1.isSelected()) {
b1.setBackground(Color.BLACK);
} else {
b1.setBackground(null);
}
}
});
final JToggleButton b2 = new JToggleButton("Print");
b2.setContentAreaFilled(false);
b2.setOpaque(true);
b2.getModel().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (b2.isSelected()) {
b2.setBackground(Color.BLUE);
} else {
b2.setBackground(null);
}
}
});
ButtonGroup bg = new ButtonGroup();
bg.add(b1);
bg.add(b2);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridBagLayout());
frame.add(b1);
frame.add(b2);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
Another solution might be to use JRadioButton
instead, which is generally used to indicate a single selection from a group of options.
See How to Use Buttons, Check Boxes, and Radio Buttons
Upvotes: 3
Reputation: 3820
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
b1.setBackground(Color.BLUE);
b2.setBackground(Color.BLACK);
JOptionPane.showMessageDialog(frame,"Welcome to allhabad High Court");
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
b2.setBackground(Color.BLUE);
b1.setBackground(Color.BLACK);
JOptionPane.showMessageDialog(frame,"Welcome to allhabad High Court");
}
});
Upvotes: 0