Here's the code to reproduce, further down I have a (quick and dirty) fix. Just execute the main method and click the buttons, only one should be active at any time, as they are toggle buttons in a button group. If it looks weird for you, good! You reproduced the bug.
- Code: Select all
package com.jidesoft.swing;
import javax.swing.*;
import java.awt.*;
public class JideToggleSplitButtonTest {
public static void main(String[] args) throws Exception {
JPanel panel = new JPanel();
final ButtonGroup buttonGroup = new ButtonGroup();
panel.add(t("b1", buttonGroup));
panel.add(t("b2", buttonGroup));
panel.add(t("b3", buttonGroup));
JFrame frame = new JFrame("ToggleTest");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(panel, BorderLayout.NORTH);
frame.pack();
frame.setVisible(true);
}
private static AbstractButton t(String s, ButtonGroup bg) {
JideToggleSplitButton b = new JideToggleSplitButton(s);
b.setActionCommand(s);
b.setFocusable(false);
if (bg != null) bg.add(b);
b.add(new JMenuItem(s + " menu"));
return b;
}
}
Add the following method to JideToggleSplitButton.ToggleSplitButtonModel and toggling/untoggling will work.
- Code: Select all
/**
* Calls from ButtonGroup will be redirected to setButtonSelected(boolean).
* @param b true selects the menu (or toggle button if called from ButtonGroup), false deselects it
*/
@Override
public void setSelected(boolean b) {
StackTraceElement[] stackFrame = Thread.currentThread().getStackTrace();
for (int i = 0; i < 5 && i < stackFrame.length; i++) {
if ("javax.swing.ButtonGroup".equals(stackFrame[i].getClassName())) {
setButtonSelected(b);
return;
}
}
super.setSelected(b);
}
It's ugly, but due to the ambiguity of setSelected (MenuItem vs. Button) I don't know whether there's an easy cleaner fix. Neither did I do extensive tests for side effects and regression. Maybe someone familiar with Jide/Swing internals can share their view on this or propose a better solution.