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.
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.
Use textArea. append("text") , but I recomend JTextPane for more control like color, selection, etc.
JTextArea0. selectAll(); JTextArea0. replaceSelection(""); This selects the entire textArea and then replaces it will a null string, effectively clearing the JTextArea.
The JOptionPane is a subclass of JComponent class which includes static methods for creating and customizing modal dialog boxes using a simple code.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With