tags:

views:

61

answers:

1

I am a student at ISU. I was working on a homework assignment where I wanted to get text that was selected (highlighted) in a TextArea to appear in a JOptionPane dialog. I tried many of the methods for JOptionPane, but I could not get any of them to place the text the user selected in the input field of the dialog.

I guess that I could make a one element String array and pass this to the JOptionPane constructor listed.

JOptionPane(Object message, int messageType, int optionType, Icon icon, Object[] options, Object initialValue)

This is not way I wanted to implement the JOptionPane. Please give me any help you can. Thanks and there is no hurry as the assignment has been handed in.

+1  A: 

This tutorial shows how to use JOptionPane and how to get input from the user.

Read it to see if works for you or not.

How to make dialogs

I guess that I could make a one element String array and pass this to the JOptionPane constructor listed.

Sounds correct to me.

This is not way I wanted to implement the JOptionPane.

What's wrong with that? Or how do you want to implement it?

Take a look at that article perhaps you could manage to write something like:

JOptionPane.showMessageDialog(frame, getSelectedTextFrom( someTextArea ) , "Message");

Where the method:

String getSelectedTextFrom( JTextArea )

Will return... well the selected text from the text area... :)

OscarRyz
While your answer did not give me the code I needed directly, I was able to guess what I had to do using the JOptionPane constructor I gave. I now have my input dialog box populated with the text that had been selected in a TextArea. Thanks. Here is the line of code if you would like to see it. findTxt = (String) JOptionPane.showInputDialog(findDialog, "Text to find?", "Question", JOptionPane.QUESTION_MESSAGE, null, null, textArea.getSelectedText());
David
I'm glad. It was a bit hard to know what code to post since I have no clue what did you have already.
OscarRyz