I've added some JToggleButtons to a JPanel with a BoxLayout. The buttons have been placed in a ButtonGroup. Now I want to toggle some attributes for the currently selected button, only while it is selected. I'm looking for something like this:
buttonGroup.addSomeListener(new SomeListener() {
@Override
public void whenToggled() {
/*set attributes for button*/
}
});
Specifically I want selected buttons to display a BevelBorder.LOWERED
as opposed to the
button.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
that the buttons in my JPanel have.
EDIT
this code doesn't print out "rollover" unless I simply change the type of button
to JButton
How can a JToggleButton listen for mouseOver in a changelistener?
public class Test {
public static void main(String[] args) {
JPanel panel = new JPanel();
final JToggleButton button = new JToggleButton();
button.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent arg0) {
ButtonModel mod = button.getModel();
if (mod.isRollover()) {
System.out.println("rollover");
}
}
});
JFrame frame = new JFrame();
frame.add(panel);
panel.add(button);
frame.setVisible(true);
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
} }
Again, myself, I'd use an ItemListener. I'd also consider combining the beveled border with Toggle's inside border of its compound border as this will make the button fuller in size. For example:
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;
import javax.swing.border.*;
public class Foo {
static CompoundBorder lowered; // = BorderFactory.createBevelBorder(BevelBorder.LOWERED);
static CompoundBorder raised; // = BorderFactory.createBevelBorder(BevelBorder.RAISED);
public static void main(String[] args) {
JPanel panel = new JPanel();
final ButtonGroup btnGrp = new ButtonGroup();
for (int i = 0; i < 4; i++) {
JToggleButton toggle = new JToggleButton("Toggle " + (i + 1));
if (lowered == null) {
CompoundBorder toggleBorder = (CompoundBorder) toggle.getBorder();
Border toggleInside = toggleBorder.getInsideBorder();
lowered = BorderFactory.createCompoundBorder(
BorderFactory.createBevelBorder(BevelBorder.LOWERED),
toggleInside);
raised = BorderFactory.createCompoundBorder(
BorderFactory.createBevelBorder(BevelBorder.RAISED),
toggleInside);
}
toggle.setBorder(raised);
btnGrp.add(toggle);
panel.add(toggle);
toggle.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent iEvt) {
Border border = iEvt.getStateChange() == ItemEvent.SELECTED ? lowered
: raised;
((JComponent) iEvt.getSource()).setBorder(border);
}
});
}
JOptionPane.showMessageDialog(null, panel);
}
}
Yep, I agree that this does not work:
toggle.getModel().addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent evt) {
ButtonModel model = (ButtonModel) evt.getSource();
// this doesn't work
if (model.isRollover()) {
System.out.println(model.getActionCommand() + " is rolled over");
}
// but this does
if (model.isSelected()) {
System.out.println(model.getActionCommand() + " is selected");
}
}
});