I am facing issues when I set my first JDialog modal and the second one non-modal.
This is the functionality I am trying to implement:
On click of "Test the dialog!" button, a JDialog with name Custom Dialog
Main will open.
If click "yes" option in Custom Dialog Main, another
JDialog named Custom Dialog Search will open.
If click "yes" option in Custom Dialog Search, then
Custom Dialog Main should come front.
And I should be able to select any JDialog. For example if I select
Custom Dialog Search, the other dialog should go
back and vice versa.
Problem I am facing is when I click "yes" in Custom Dialog Main, then Custom Dialog Search is displayed behind the main dialog.
This is happening because I am setting Custom Dialog Search non-modal.
If I this dialog modal it is displayed correctly but after I click "yes" Custom Dialog Main doesn't come front.
I even tried to set CustomDialogSearch's parent to be CustomDialog the behaviour still not correct.
Below is the example code I am testing.
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionEvent;
import java.awt.Dimension;
public class TestTheDialog implements ActionListener {
JFrame mainFrame = null;
JButton myButton = null;
public TestTheDialog() {
mainFrame = new JFrame("TestTheDialog Tester");
mainFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
myButton = new JButton("Test the dialog!");
myButton.addActionListener(this);
mainFrame.setLocationRelativeTo(null);
mainFrame.getContentPane().add(myButton);
mainFrame.pack();
mainFrame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(myButton == e.getSource()) {
System.err.println("Opening dialog.");
CustomDialog myDialog = new CustomDialog(mainFrame, true, "Custom Dialog Main?");
System.err.println("After opening dialog.");
if(myDialog.getAnswer()) {
System.err.println("The answer stored in CustomDialog is 'true' (i.e. user clicked yes button.)");
}
else {
System.err.println("The answer stored in CustomDialog is 'false' (i.e. user clicked no button.)");
}
}
}
public static void main(String argv[]) {
TestTheDialog tester = new TestTheDialog();
}
}
import javax.swing.JDialog;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
public class CustomDialog extends JDialog implements ActionListener {
private JPanel myPanel = null;
private JButton yesButton = null;
private JButton noButton = null;
private boolean answer = false;
private JFrame parentFrame;
public boolean getAnswer() { return answer; }
public CustomDialog(JFrame frame, boolean modal, String myMessage) {
super(frame, modal);
parentFrame = frame;
myPanel = new JPanel();
getContentPane().add(myPanel);
myPanel.add(new JLabel(myMessage));
yesButton = new JButton("Yes");
yesButton.addActionListener(this);
myPanel.add(yesButton);
noButton = new JButton("No");
noButton.addActionListener(this);
myPanel.add(noButton);
pack();
setLocationRelativeTo(frame);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(yesButton == e.getSource()) {
CustomDialogSearch myDialog = new CustomDialogSearch(parentFrame, false, "CustomDialog Search?");
System.err.println("User chose yes.");
answer = true;
myDialog.getAnswer();
System.out.println("myDialog.getAnswer()="+myDialog.getAnswer());
myDialog.show();
if(myDialog.getAnswer()==true)
{
System.out.println("tofront");
this.toFront();
}
//setVisible(false);
}
else if(noButton == e.getSource()) {
System.err.println("User chose no.");
answer = false;
setVisible(false);
}
}
}
import javax.swing.JDialog;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
public class CustomDialogSearch extends JDialog implements ActionListener {
private JPanel myPanel = null;
private JButton yesButton = null;
private JButton noButton = null;
private boolean answer = false;
public boolean getAnswer() { return answer; }
public CustomDialogSearch(JFrame frame, boolean modal, String myMessage) {
super(frame, modal);
myPanel = new JPanel();
getContentPane().add(myPanel);
myPanel.add(new JLabel(myMessage));
yesButton = new JButton("Yes");
yesButton.addActionListener(this);
myPanel.add(yesButton);
noButton = new JButton("No");
noButton.addActionListener(this);
myPanel.add(noButton);
pack();
setLocationRelativeTo(frame);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(yesButton == e.getSource()) {
System.err.println("Search User chose yes.");
answer = true;
//setVisible(false);
}
else if(noButton == e.getSource()) {
System.err.println("Search User chose no.");
answer = false;
setVisible(false);
}
}
}
I even tried to set CustomDialogSearch's parent to be CustomDialog the
behaviour still not correct.
I think you're in the right track here but you need to play with dialogs modality type. For instance:
Set the modality type of Custom Dialog Main ("parent" dialog) as Dialog.ModalityType.APPLICATION_MODAL. By doing this when this dialog is visible it will block all windows except its children.
Set the modality type of Custom Dialog Search ("child" dialog) as Dialog.ModalityType.MODELESS. This way it won't block any other window and you can go from the child to the parent and vice versa.
For a better understanding take a look to How to Use Modality in Dialogs article.
Example
Here is a code example about using modality as I've suggested above:
import java.awt.Dialog;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class Demo {
private void createAndShowGUI() {
JButton button = new JButton("Create Parent modal dialog");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();
JFrame owner = (JFrame)SwingUtilities.windowForComponent(button);
Demo.this.createAndShowParentDialog(owner);
}
});
JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(button);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void createAndShowParentDialog(JFrame owner) {
JButton button = new JButton("Create Child non-modal dialog");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();
JDialog parent = (JDialog)SwingUtilities.windowForComponent(button);
Demo.this.createAndShowChildrenDialog(parent);
}
});
JDialog parentDialog = new JDialog(owner, "Parent dialog");
parentDialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
parentDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
parentDialog.getContentPane().add(button);
parentDialog.pack();
parentDialog.setLocationRelativeTo(null);
parentDialog.setVisible(true);
}
private void createAndShowChildrenDialog(JDialog parent) {
JButton backButton = new JButton("Back to parent dialog");
backButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JButton button = (JButton)e.getSource();
Window dialog = SwingUtilities.windowForComponent(button);
dialog.getOwner().toFront();
}
});
JDialog childDialog = new JDialog(parent, "Child dialog");
childDialog.setModalityType(Dialog.ModalityType.MODELESS);
childDialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
childDialog.getContentPane().add(backButton);
childDialog.pack();
childDialog.setLocationRelativeTo(null);
childDialog.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Demo().createAndShowGUI();
}
});
}
}
Edit
I can select the windows of parent and child JDialogs but when I
select the parent JDialog window, the child JDialog is still in front
of parent JDialog.
Well I have a better understanding now on what is the problem. This behaviour depends on how native windowing system handles focused and active windows. Having said this if you call for instance toFront() it will attempt to place the window at the top of the stack BUT some platforms do not allow windows which own other windows to appear on top of its childre. The same happens when you call toBack() method. See the javadocs for more details.
I've tested my code on Windows 7 (32 bits if it makes any difference) and parent dialog becomes focused but its children still showing (not focused) at the top. As mentioned above it's up to the windowing system decide how to handle this matter.
Related
I have discovered class javax.swing.Popup. I wrote code to test its behavior. Code is shown below. When I run the code, after the JFrame is displayed, I click the SHOW button and the Popup is displayed. After that I click the HIDE button and the Popup disappears. But then if I click the SHOW button again, nothing happens. The Popup only appears after clicking the SHOW button for the first time. Also, if I click the HIDE button first, before clicking the SHOW button, then when I click the SHOW button, the Popup does not appear.
Am I missing something?
Am I doing something wrong?
I admit I haven't investigated this behavior. I haven't searched the Internet and I haven't looked at the code for class Popup nor PopupFactory, simply due to laziness and in the hope that someone can explain it to me.
Here is my MCVE.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
public class PopupTest implements ActionListener, Runnable {
private static final String HIDE = "HIDE";
private static final String SHOW = "SHOW";
private Popup popup;
public void actionPerformed(ActionEvent actionEvent) {
String actionCommand = actionEvent.getActionCommand();
switch (actionCommand) {
case HIDE:
popup.hide();
break;
case SHOW:
popup.show();
break;
}
}
public void run() {
showGui();
}
private void showGui() {
JFrame frame = new JFrame("Popup");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JLabel centerLabel = new JLabel("CENTER LABEL", SwingConstants.CENTER);
frame.add(centerLabel, BorderLayout.CENTER);
JPanel buttonsPanel = new JPanel();
JButton showButton = new JButton(SHOW);
showButton.addActionListener(this);
buttonsPanel.add(showButton);
JButton hideButton = new JButton(HIDE);
hideButton.addActionListener(this);
buttonsPanel.add(hideButton);
frame.add(buttonsPanel, BorderLayout.PAGE_END);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
JLabel popupLabel = new JLabel("Popup_Label");
PopupFactory factory = PopupFactory.getSharedInstance();
Point pt = centerLabel.getLocationOnScreen();
int x = pt.x + 10;
int y = pt.y - 10;
popup = factory.getPopup(centerLabel, popupLabel, x, y);
}
public static void main(String[] args) {
PopupTest instance = new PopupTest();
EventQueue.invokeLater(instance);
}
}
https://docs.oracle.com/javase/7/docs/api/javax/swing/Popup.html#hide()
The documentation that you have chosen not to read is very clear that hide() disposes of the Popup and any further method calls on that Popup will result in indeterminate behaviour.
You must create a new Popup instance each time.
From the docs for hide():
"Hides and disposes of the Popup. Once a Popup has been disposed you
should no longer invoke methods on it. A disposed Popup may be
reclaimed and later used based on the PopupFactory."
As such, here is a quick and dirty modified version that works.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;
public class PopupTest implements ActionListener, Runnable {
private static final String HIDE = "HIDE";
private static final String SHOW = "SHOW";
private Popup popup;
private JLabel centerLabel;
private JLabel popupLabel = new JLabel("Popup_Label");
private PopupFactory factory = PopupFactory.getSharedInstance();
public void actionPerformed(ActionEvent actionEvent) {
String actionCommand = actionEvent.getActionCommand();
switch (actionCommand) {
case HIDE:
if (popup == null) {
return;
}
popup.hide();
popup = null; // necessary to avoid using the disposed popup
break;
case SHOW:
if (popup != null) { // it's already showing
return;
}
Point pt = centerLabel.getLocationOnScreen();
int x = pt.x + 10;
int y = pt.y - 10;
popup = factory.getPopup(centerLabel, popupLabel, x, y);
popup.show();
break;
}
}
public void run() {
showGui();
}
private void showGui() {
JFrame frame = new JFrame("Popup");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
centerLabel = new JLabel("CENTER LABEL", SwingConstants.CENTER);
frame.add(centerLabel, BorderLayout.CENTER);
JPanel buttonsPanel = new JPanel();
JButton showButton = new JButton(SHOW);
showButton.addActionListener(this);
buttonsPanel.add(showButton);
JButton hideButton = new JButton(HIDE);
hideButton.addActionListener(this);
buttonsPanel.add(hideButton);
frame.add(buttonsPanel, BorderLayout.PAGE_END);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
PopupTest instance = new PopupTest();
EventQueue.invokeLater(instance);
}
}
I am displaying a JOptionPane suppose A on a button click from JFrame, and again displaying another JOptionPane suppose B on a button click from JOptionPane A, and I have a button on JOptionPane B suppoce button1, on the click event of button1, I am using code JOptionPane.getRootFrame().dispose() for closing the JOptionPane B, but it closes both A and B, please help me how can close only B but not A.
here is my sample code
i want second JOptionPane must be open
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class SampleCode extends JFrame {
public SampleCode() {
setSize(new Dimension(500, 500));
setLocation(450, 150);
but1 = new JButton("Click me");
add(but1);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
but1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
but1Function();
}
});
}
public static void main(String args[]) {
new SampleCode();
}
void but1Function() {
JPanel panel1 = new JPanel();
JButton but2 = new JButton("Open new dialog");
panel1.add(but2);
but2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JPanel pan2 = new JPanel();
JButton but3 = new JButton("click me to close");
pan2.add(but3);
but3.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JOptionPane.getRootFrame().dispose();
}
});
JOptionPane.showMessageDialog(null, pan2);
}
});
JOptionPane jp = new JOptionPane(panel1, JOptionPane.CLOSED_OPTION,
JOptionPane.DEFAULT_OPTION, null, new Object[] {}, null);
JDialog dialog = jp.createDialog(null, "This one must be remain open");
dialog.setLocation(500, 200);
dialog.setSize(new Dimension(345, 200));
dialog.setVisible(true);
}
JButton but1;
}
You don't want to get the root frame nor dispose of it. You want to get the window that is displaying the JOptionPane, a Window that should be a modal JDialog. So instead, use SwingUtilities.getWindowAncestor(someComponentInJOptionPane), and call dispose() on that Window if you want to programmatically dispose of your JOPtionPane.
import java.awt.Component;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class OptionPaneFun {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#SuppressWarnings("serial")
public void run() {
final JPanel panel1 = new JPanel();
panel1.add(new JButton(new AbstractAction("Show new option pane") {
{
putValue(MNEMONIC_KEY, KeyEvent.VK_S);
}
#Override
public void actionPerformed(ActionEvent e1) {
final JPanel panel2= new JPanel();
panel2.add(new JButton(new AbstractAction("Dispose of this option pane") {
{
putValue(MNEMONIC_KEY, KeyEvent.VK_D);
}
#Override
public void actionPerformed(ActionEvent e2) {
Component comp = (Component) e2.getSource();
Window win = SwingUtilities.getWindowAncestor(comp);
if (win != null) {
win.dispose();
}
}
}));
JOptionPane.showMessageDialog(panel1, panel2);
}
}));
JOptionPane.showMessageDialog(null, panel1);
}
});
}
}
The static method "getRootFrame()" returns your root frame which is the only one and it's the same for both your components (A and B). What you need to do - you have to put two frames in your root frame (call them frameA and frameB) and put paneA to frameA and paneB to frameB. Instead of calling this static method just invoke frameB.dispose() on reference frameB which you already have.
Try to add
panel.validiate();
After the dispose command. I had the same problem once and it helped a lot when I used this trick.
Basically when you add this command, it is telling the frame to validate or actually do it.
Read the oracle docs for more info.
My problem is the following: In my application the user clicks a button which brings up a dialog box (a custom jOptionPane). This dialog contains a JTextArea in which the user will type a response, which will then be processed by the application, however I would like this JTextArea (which will hold the user's input and currently contains example text like "Write your answer here") to be automatically highlighted.
I can do this normally, by calling requestFocusInWindow() followed by selectAll() on the JTextArea however there seems to be a problem when this is done using a JOptionPane which I'm guessing is to do with the fact that the focus cannot shift to the JTextArea successfully.
I've made a SSCCE to demonstrate this clearly, and hopefully get an answer from one of you guys as to how I can make this possible. Thanks in advance!
Class 1/2 : Main
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class Main extends JFrame{
public static void main(String[] args) {
Main main = new Main();
main.go();
}
private void go() {
JPanel background = new JPanel();
JPanel mainPanel = new ExtraPanel();
((ExtraPanel) mainPanel).setupPanel();
JButton testButton = new JButton("Test the jOptionPane");
testButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
optionPaneTest();
}
});
background.add(mainPanel);
background.add(testButton);
getContentPane().add(background);
pack();
setVisible(true);
}
private void optionPaneTest() {
JPanel testPanel = new ExtraPanel();
((ExtraPanel) testPanel).setupPanel();
int result = JOptionPane.showConfirmDialog(null, testPanel,
"This is a test", JOptionPane.OK_CANCEL_OPTION);
}
}
----------------------------------------------------------------------------- Class 2/2 : ExtraPanel
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class ExtraPanel extends JPanel{
public void setupPanel() {
JTextArea textArea = new JTextArea();
textArea.setText("Write your response here");
textArea.requestFocusInWindow();
textArea.selectAll();
add(textArea);
}
}
Just add
textArea.getCaret().setSelectionVisible(true)
After textArea.selectAll();
If you want focus in the TextArea so that the user can immediately start typing, you can trigger the selection using the ancestor added event.
public void setupPanel() {
final JTextArea textArea = new JTextArea();
textArea.setText("Write your response here");
textArea.addAncestorListener(new AncestorListener() {
public void ancestorRemoved(AncestorEvent event) { }
public void ancestorMoved(AncestorEvent event) { }
public void ancestorAdded(AncestorEvent event) {
if (event.getSource() == textArea) {
textArea.selectAll();
textArea.requestFocusInWindow();
}
}
});
add(textArea);
}
i want to open new JDialog box inside JDialog box. so i have used that code to prevent to come on ancestor JFrame but i am facing the problem to open new JDialog box in previous JDialog box. please give me the solution to get rid of this problem.
Here is the code :-
TestbedWorkflow tbwf = new TestbedWorkflow();
JDialog dialog = new JDialog();
Dimension s = SOAStreamer.getSOAStreamerObj().getContentPanel().getSize();
dialog.setSize(s);
dialog.setTitle("TestBed Workflow Design");
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
tbwf.setSize(s);
dialog.add(tbwf);
tbwf.updateUI();
dialog.setVisible(true);
TestbedWorkflow is JFrame which i have added in to JDialog box.now i want to open new JDialog box.
thanks in advance
The example below opens an arbitrary number of dialogs in an APPLICATION_MODAL hierarchy; only the most recent is operable. As an alternative, consider a modeless dialog, illustrated here.
Update: The revised example below shows the hierarchy depth in the dialog title and eliminates a spurious subclass.
import java.awt.Dialog;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
/**
* #see https://stackoverflow.com/a/12301243/230513
*/
public class DialogTest {
private static int index;
static class OpenAction extends AbstractAction {
public OpenAction() {
super("Open");
}
#Override
public void actionPerformed(ActionEvent e) {
JDialog jd = new JDialog();
jd.setTitle("D" + String.valueOf(++index));
jd.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
jd.add(new JButton(new OpenAction()));
jd.pack();
jd.setLocationRelativeTo(null);
jd.setVisible(true);
}
}
private void display() {
JFrame f = new JFrame("DialogTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(new JButton(new OpenAction()));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new DialogTest().display();
}
});
}
}
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.
Are you asking about checkboxes or radiobuttons? You mention both.
To create a multiple-exclusion scope you have to use ButtonGroup class
I found something that seems to fix it, though it's an ugly hack. I added item listeners to each of the Checkboxes. When those trigger I resize the window by 1 pixel, then do an invokeLater() on pack() (my dialogs aren't resizable). Here's the modified test code:
package mactest;
import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.Dialog;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Main {
public static void main(String[] args) {
boolean modal = false;
boolean tweak = false;
if(args.length > 0) {
modal = args[0].toLowerCase().equals("true");
}
if(args.length > 1) {
tweak = args[1].toLowerCase().equals("true");
}
TestDialog dlg = new TestDialog(new Frame(), modal, tweak);
dlg.setVisible(true);
}
private static class TestDialog extends Dialog {
private Checkbox cb1;
private Checkbox cb2;
private CheckboxGroup cbg;
public TestDialog(Frame owner, boolean modal, boolean tweak) {
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();
if(tweak) {
cb1.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
onSelection();
}
});
cb2.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
onSelection();
}
});
}
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
TestDialog.this.setVisible(false);
System.exit(0);
}
});
}
private void onSelection() {
Rectangle bounds = this.getBounds();
this.setBounds(bounds.x, bounds.y, bounds.width + 1, bounds.height);
EventQueue.invokeLater(new Runnable() {
public void run() {
TestDialog.this.pack();
}
});
}
}
}
For resizable components you'd need to store the size pre-tweak and restore that instead of calling pack().
In windows I could see the dialog flicker every once in a while when I changed the selection, though on the mac it wasn't noticeable at all.