Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JOptionPane with a JTextArea instead of a text field?

I need to grab multiple lines of input from a popup JOptionPane (or some other popup, but most of my searches direct me there, which is where I get stuck..). I am stuck at using

JOptionPane.showInputDialog(null, new JTextArea(20,20));

but I want just the 20x20 area to be read to a String and no text field shown. I figure there must be some way to do this, but the other types of dialog only seem to return an int... It doesn't have to be a JOptionPane, as long as it is a separate popup from my main GUI that can read a string back in to be read.

like image 929
Broots Waymb Avatar asked Jan 27 '15 05:01

Broots Waymb


People also ask

What is JOptionPane Inputdialog?

The JOptionPane class is used to provide standard dialog boxes such as message dialog box, confirm dialog box and input dialog box. These dialog boxes are used to display information or get input from the user. The JOptionPane class inherits JComponent class.

How do I add text to JTextArea?

Use textArea. append("text") , but I recomend JTextPane for more control like color, selection, etc.

How do I clear a JTextArea?

JTextArea0. selectAll(); JTextArea0. replaceSelection(""); This selects the entire textArea and then replaces it will a null string, effectively clearing the JTextArea.

Is JOptionPane a method?

The JOptionPane is a subclass of JComponent class which includes static methods for creating and customizing modal dialog boxes using a simple code.


1 Answers

Keep a reference to the JTextArea before passing to the JOptionPane, the JOptionPane will tell you what the user did (how they closed the dialog) and depending on the result, you might want to do different things

JTextArea ta = new JTextArea(20, 20);
switch (JOptionPane.showConfirmDialog(null, new JScrollPane(ta))) {
    case JOptionPane.OK_OPTION:
        System.out.println(ta.getText());
        break;
}

See How to Make Dialogs for more details

like image 171
MadProgrammer Avatar answered Oct 07 '22 13:10

MadProgrammer