Reputation: 223
Im using several showInputDialogs in a program. When one of these input pops up it freezes all the other windows in the background until it has recieved an input, is there a way to make it not freeze the other windows?
Upvotes: 0
Views: 523
Reputation: 285405
If by "freeze" you mean that the user cannot access the other windows, then the key is to make the new dialog a non-modal dialog. You can extract the JDialog from the JOptionPane and then elect to display it in a non-modal way. The JOptionPane API will show you how. Search for the section titled "Direct Use:"
Edit: as Andrew states as well! 1+
Playing with code....
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import javax.swing.*;
public class Foo {
public static void main(String[] args) {
final JTextField textfield = new JTextField(10);
textfield.setFocusable(false);
final JPanel panel = new JPanel();
panel.add(textfield);
panel.add(new JButton(new AbstractAction("Push Me") {
private JOptionPane optionPane;
private JDialog dialog;
private JTextField optionTextField = new JTextField(10);
@Override
public void actionPerformed(ActionEvent arg0) {
if (dialog == null) {
JPanel optionPanel = new JPanel(new BorderLayout());
optionPanel.add(new JLabel("Enter some stuff"),
BorderLayout.PAGE_START);
optionPanel.add(optionTextField, BorderLayout.CENTER);
optionPane = new JOptionPane(optionPanel,
JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
dialog = optionPane.createDialog(panel, "Get More Info");
dialog.setModal(false);
dialog.addComponentListener(new ComponentAdapter() {
@Override
public void componentHidden(ComponentEvent arg0) {
Integer value = (Integer) optionPane.getValue();
if (value == null) {
return;
}
if (value == JOptionPane.OK_OPTION) {
textfield.setText(optionTextField.getText());
}
}
});
}
dialog.setVisible(true);
}
}));
JFrame frame = new JFrame("Frame");
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Upvotes: 3
Reputation: 168835
Use a non-modal JDialog
instead. See How to Use Modality in Dialogs for details.
Upvotes: 3