Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inputbox

Status
Not open for further replies.

apffal

Technical User
Apr 10, 2004
97
PT
I've created a listener, which opens an inputbox, for searching strings in a textarea.
But, when clicking OK, that inputbox disappears and I need to restart the listener for searching another ocurrence of the same string.
How to solve this ?
Thanks.

That's my code

class Listener implements ActionListener {
public void actionPerformed(ActionEvent e) {

String wordToFind;
wordToFind = JOptionPane.showInputDialog(null, "Expression");
int caretPos=txt1.getCaretPosition();
int i;

for(i=caretPos; i<(txt1.getText().length()-wordToFind.length()); i++)
{
String temp=txt1.getText().substring(i,i+wordToFind.length());
if (temp.equals(wordToFind))
{
txt1.select(i,i+wordToFind.length());
break;
}
}

}
}
 
The variable wordtofind is just in the scope of the method actionPerformed(). Once the method is finished, all inner variables are deleted.

I think you should define wordtofind as a class variable so it will remain and just show the OptionPane is it's null.

Cheers,

Dian
 
That depends on what you're trying to do.

Searching on the same word every time the button is clicked?
Jsut ask once and the search many times?

To know difficult is it.

May the strength be with you

Dian
 
What I want is to search on the same word every time the button is clicked.
 
Then you have to take the variable that holds the String and the code that shows the input dialgo out of the actionPerformed method.code.

The right place to put this code depends on the structure of the rest od the program.

The idea behind this is that the actionPerformed method is called with every click. If you include the input panel inside it, as you have it now, it will be reinvoked with each click.

Cheers,

Dian
 
That's my problem :
The program shows the inputbox when I click a menu item (Locate).
And I think it's not possible associate another listener to the OK button in inputbox.
Is there any other way to do it ?
 
I'm not sure, JOptionPane is just another Component, you can register to the close event. Anyway, it will appear again every time the menu is clicked.

I think you should redesign your app with two menu options: set the string to search and do the search itself.

Cheers,

Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top