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?
Asked
Active
Viewed 507 times
2 Answers
3
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);
}
}

Hovercraft Full Of Eels
- 283,665
- 25
- 256
- 373
-
1*"Search for the section titled "Direct Use:""* Ooh, I had not thought of that. +1 It *is* a hassle to recreate the functionality of `JOptionPane`.. – Andrew Thompson Dec 15 '13 at 20:46
-
I checked it out but i don't see how it works with inputdialogs? i have inputdialogs in the styles of tablename = JOptionPane.showInputDialog("Enter tablename *(use capital letters)"); – Looptech Dec 15 '13 at 20:53
3
Use a non-modal JDialog
instead. See How to Use Modality in Dialogs for details.

Andrew Thompson
- 168,117
- 40
- 217
- 433