6

All I want to do is have a JOptionPane inputDialog with a JTextArea instead of a JTextField.
I tried putting the JTextArea inside of the Message parameter like so

Object[] inputText = new Object[]{new JLabel("Enter Graph Information"),
                                  newJTextArea("",20,10)};
graphInfo=(String)JOptionPane.showInputDialog(null,
                                              inputText,
                                              "Create Graph",
                                              JOptionPane.PLAIN_MESSAGE,
                                              null,
                                              null,
                                              "");

But it still has the text field at the bottom and I cannot get the text from the JTextArea. Is there any way to either remove the original text field and get the text from the jtextarea or replace the text field with the text area completely? I'm trying to avoid having to make a custom dialog if possible and this "seems" like something that should be easy to do?

Matt Phillips
  • 11,249
  • 10
  • 46
  • 71
  • possible duplicate of [Java - How to create a custom dialog box?](http://stackoverflow.com/questions/789517/java-how-to-create-a-custom-dialog-box) – Tony Oct 25 '14 at 11:38

1 Answers1

9

You're on the right lines; you just need to use showConfirmDialog instead of showMessageDialog, which allows you to pass any Component as your "message" and have it displayed within the JDialog. You can then capture the contents of the JTextArea if the user clicks OK; e.g.

int okCxl = JOptionPane.showConfirmDialog(SwingUtilities.getWindowAncestor(this),
                                    textArea,
                                    "Enter Data",
                                    JOptionPane.OK_CANCEL_OPTION)

if (okCxl == JOptionPane.OK_OPTION) {
  String text = textArea.getText();
  // Process text.
}

If you want to show a JLabel in conjunction with your JTextArea you can create and pass in a JPanel containing both Components; e.g.

JTextArea textArea = ...
JPanel pnl = new JPanel(new BorderLayout());

pnl.add(new JLabel("Please enter some data:"), BorderLayout.NORTH);
pnl.add(textArea, BorderLayout.CENTER);

JOptionPane.show...
Adamski
  • 54,009
  • 15
  • 113
  • 152
  • +1 you beat me to it :) Also, I didn't know about SwingUtilities.getWindowAncestor() - cool. – Joshua McKinnon Jan 18 '10 at 21:55
  • Thanks. I used to use JOptionPane.getFrameForComponent(this) but realised SwingUtilities.getWindowAncestor is better in case your dialog's parent is also a JDialog. – Adamski Jan 18 '10 at 21:57
  • interesting that when I create the dialog inside of an actionlistener i cannot use SwingUtilities.getWindowAncestor :( the following line worked though. int okCxl = JOptionPane.showConfirmDialog(null,temp,"Create new graph",JOptionPane.OK_CANCEL_OPTION,JOptionPane.PLAIN_MESSAGE,null); – Matt Phillips Jan 20 '10 at 16:38
  • From within an ActionListener, calling SwingUtilities.getWindowAncestor(this) will return a reference to the ActionListener, not the parent component. Rather than passing in "this", you need to pass in ".this". – Adamski Nov 04 '13 at 11:49