JOptionPane configure login

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

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 a text box at the bottom, and I cannot get the text from JTextArea. Is there a way to remove the original text box and get the text from jtextarea or completely replace the text box with a text area? Am I trying to avoid having to customize the dialog box if possible, and is it "looking like" something that should be easy to do?

+6
java swing joptionpane
source share
1 answer

You are on the right lines; you just need to use showConfirmDialog instead of showMessageDialog , which allows you to pass any Component as your "message" and display it in JDialog . You can then capture the contents of JTextArea if the user clicks OK; eg.

 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 JLabel in conjunction with your JTextArea , you can create and pass to JPanel , containing as Component s; eg.

 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... 
+7
source share

All Articles