I'm in the process of testing my Java application on the Mac and I've run into a very strange issue. Checkboxes that appear in a modal Dialog render incorrectly, though non-modal Dialogs work fine.
For example, say I have a window with 2 radio buttons. When the dialog opens the first one is selected. When I click on the second button it suddenly looks like both are selected. Clicking anywhere else within the dialog will cause the rendering to fix itself and only the selected button will be displayed.
The following code reproduces this for me:
package mactest;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Main {
public static void main(String[] args) {
boolean modal = false;
if(args.length > 0) {
modal = args[0].toLowerCase().equals("true");
}
TestDialog dlg = new TestDialog(new Frame(), modal);
dlg.setVisible(true);
}
private static class TestDialog extends Dialog {
private Checkbox cb1;
private Checkbox cb2;
private CheckboxGroup cbg;
public TestDialog(Frame owner, boolean modal) {
super(owner);
cbg = new CheckboxGroup();
cb1 = new Checkbox("One", true, cbg);
cb2 = new Checkbox("Two", false, cbg);
this.setLayout(new FlowLayout());
this.add(cb1);
this.add(cb2);
this.setModal(modal);
this.pack();
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
TestDialog.this.setVisible(false);
System.exit(0);
}
});
}
}
}
If I call this like so:
java -cp MacTest.jar mactest.Main false
the dialog is not modal and everything works fine. However, If I tell it to be modal:
java -cp MacTest.jar mactest.Main true
then the rendering issues occur.
I've tried every trick I can think of to try to fix the problem (invalidate, doLayout, requesting focus, explicitly setting the state of every button when one is selected, etc)., but so far the only thing I've come up with that works is to make the dialog not be modal. Unfortunately, that's not an option in my application.
In case it matters, this is on a MacBook running OS X 10.5 running Java 1.5.0_16.