The Coding Wombat
The Coding Wombat

Reputation: 815

Input Dialog without icon and only OK option

I'm trying to make a JOptionPane with input area that only has an OK button.

When trying to do this, there is no icon but an additional cancel button:

String name = (String) JOptionPane.showInputDialog(null, "Please enter your name", "Name required", JOptionPane.PLAIN_MESSAGE, null, null, "name");

And when I do this there's an icon:

String name = (String) JOptionPane.showInputDialog(null, "Please enter your name", "Name required", JOptionPane.OK_OPTION, null, null, "name");

Is there a way to combine the two? I don't understand how the latter works because I used null where you'd place an icon.

Upvotes: 1

Views: 2154

Answers (1)

Sergiy Medvynskyy
Sergiy Medvynskyy

Reputation: 11327

Something like this:

JTextField field  = new JTextField(20);
JLabel label = new JLabel("Enter your text here");
JPanel p = new JPanel(new BorderLayout(5, 2));
p.add(label, BorderLayout.WEST);
p.add(field);
JOptionPane.showMessageDialog(null, p, "Name required", JOptionPane.PLAIN_MESSAGE, null);
String text = field.getText();
System.out.println("You've entered: " + text);

Upvotes: 4

Related Questions