I have a JFrame with some a JTextField and a JButton. I want it to behave like JOptionPane.showInputDialog(). Basically, I want to construct it, then call .start() or something which will make it visible and then wait for a button press and then return the contents of the JTextField. I have heard that wait()/notify() might do this, but I don't know if that's right for this and if it is, could I see a short example of how to use it?
Asked
Active
Viewed 2,147 times
2
-
1Why not use `JDialog` in place of `JOptionPane` ? – nIcE cOw Jun 20 '12 at 03:39
-
That sounds like it might work. Can a JDialog contain more information than a JOptonPane (so it could replace a JFrame?) – Fractaly Jun 20 '12 at 03:41
-
Actually they both have equal functionality, seems like you really don't know what `JOptionPane` can do, in real terms, other than showing the default Dialog windows to the user. You can manipulate `JOptionPane` to show what ever you need it to have. Here is one example regarding [JOptionPane usage](http://stackoverflow.com/questions/10306901/how-to-change-highlighting-color-in-java-swing-textarea-and-also-change-the-be/10309277#10309277), have a look at this too :-) – nIcE cOw Jun 20 '12 at 04:02
-
Do watch the `JButton` (with name "HIGHLIGHT TEXT")'s `actionPerformed(...)` method, watch the second argument in this line `int selection = JOptionPane.showConfirmDialog(frame, getOptionPanel(), "Highlight Colour : ",JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);`, this can give you one idea, which means you can add your own `JPanel` and put whatever you want to put in that `JPanel` and use the same `JOptionPane` for your specific needs :-) – nIcE cOw Jun 20 '12 at 04:07
2 Answers
3
Also JDialog is your solution for custom input dialogs, there is library which can help speed up your development. It is called TaskDailog.
More info at http://code.google.com/p/oxbow/wiki/TaskDialogIntroduction?tm=6

Eugene Ryzhikov
- 17,131
- 3
- 38
- 60
3
Here try this code example using JDialog
:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DialogExample extends JFrame
{
private JLabel nameLabel;
public DialogExample()
{
super("Dialog Example");
}
private void createAndDisplayGUI()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
nameLabel = new JLabel();
contentPane.add(nameLabel);
setContentPane(contentPane);
setSize(200, 100);
setLocationByPlatform(true);
setVisible(true);
MyDialog dialog = new MyDialog(this, "Credentials : ", true);
dialog.createAndDisplayGUI();
}
public void setName(String name)
{
if (name.length() > 0)
nameLabel.setText(name);
else
nameLabel.setText("Empty string received.");
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new DialogExample().createAndDisplayGUI();
}
});
}
}
class MyDialog extends JDialog
{
private JTextField nameField;
private JFrame frame;
public MyDialog(JFrame f
, String title, boolean isModal)
{
super(f, title, isModal);
frame = f;
}
public void createAndDisplayGUI()
{
JPanel contentPane = new JPanel();
contentPane.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
JLabel nameLabel = new JLabel("Please Enter your Name : ");
nameField = new JTextField(10);
JButton submitButton = new JButton("SUBMIT");
submitButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
if (nameField.getDocument().getLength() > 0)
frame.setName(nameField.getText());
else
frame.setName("");
MyDialog.this.dispose();
}
});
contentPane.add(nameLabel);
contentPane.add(nameField);
contentPane.add(submitButton);
add(contentPane);
pack();
setVisible(true);
}
}

nIcE cOw
- 24,468
- 7
- 50
- 143
-
That was almost exactly the solution I was working on. You probably just saved me half an hour. Thanks – Fractaly Jun 20 '12 at 04:09
-
You are MOST WELCOME and KEEP SMILING :-). HAPPY to know that my answer did helped you somewhere :-) – nIcE cOw Jun 20 '12 at 04:10
-
One thing though- can I make it return its value like JOptionPane.showInputDialog, where it doesn't need to backcall on the frame? – Fractaly Jun 20 '12 at 04:11
-
Yesh, true, just change the logic inside the `actionPerformed(...)` method of the `Submit JButton`, or else you can take the approach as shown in the comment by me, regarding using `JOptionPane` in the same manner as `JDialog` (in some sense) :-) – nIcE cOw Jun 20 '12 at 04:13