alicedimarco
alicedimarco

Reputation: 325

How do I display something I enter in a JOptionPane on the JFrame?

What I want to do is when I input something in JOptionPane, say, APPLES. I want to display it as APPLES in the JFrame. Now, if I want to input CATS next, it'll appear in the JFrame together with the APPLES.

It should look like this: APPLES CATS

And when I input more, it just displays and displays. The only way I know how to do it is to use setText for JLabel, but it's only displaying ONE word. How do I display ALL of the words I input?

Upvotes: 0

Views: 247

Answers (3)

Andrew Thompson
Andrew Thompson

Reputation: 168845

JTextArea has an append(String) method. It is a multi-line component that might be more appropriate for displaying a list of strings. Or, for that matter, a JList, or JTable might be better suited to displaying the user input.

Upvotes: 3

davidfrancis
davidfrancis

Reputation: 3849

Try this: myLabel.setText(myLabel.getText() + " " + myTextField.getText());

Upvotes: 3

Harry Joy
Harry Joy

Reputation: 59694

but it's only displaying ONE word

Hoping that you are getting word written in JOptionPane in JFrame, try this:

jLabel.setText(jLabel.getText() + " " + strGotFromJOptionPane);

Here strGotFromJOptionPane is the text you got from JOptionPane.

Upvotes: 3

Related Questions