Originally Posted by
aussiemcgr
You could lock it while it is searching. Set: JTextArea.setEditable(false); while the find is activated, and then set it back to JTextArea.setEditable(true);
Then the user can't edit it.
For your errors, you'll just have to add more print statements to find out what is happening. It is difficult for people on this forum to debug runtime GUI issues. If you are familiar with unit testing, this might be a good candidate for that.
I do already have some printlns and they aren't showing the problem. In fact, they are, with the ones I've got so far, showing that it is reading it in and everything as it should be.
(Also, Windows notepad can somehow manage to do this with the document still able to be edited. I think a DocumentListener added to the JTextArea passed as a param should do the trick for that.)
Also, I found that I didn't need setCaretPosition as the setSelectionStart() or setSelectionEnd(0 already does it for me.
--- Update ---
My findDown() is now working but my findUp() is only finding the first term (I fear it may be halting at spaces or something or else just is plain buggy. If I move the cursor, then it finds the right spot, though still is buggy. I had found an issue where I had kept using it to find the last String that matched in the main String, hence, why it kept staying at the same spot. However, even after fixing it to now look for the last String that matches in the substring, and using some calculations to change the value of the String and stuff, the problem is STILL happening.)
package gui.utilities;
import components.JTitledTextArea;
import java.awt.Color;
import javax.swing.JOptionPane;
import java.util.ArrayList;
import javax.swing.event.DocumentListener;
import javax.swing.event.DocumentEvent;
public class Finder
{
private String mainString;
private String foundString;
private JTitledTextArea jtta;
private int length;
private String sub;
public Finder(JTitledTextArea jtta, String foundString)
{
this.jtta = jtta;
this.foundString = foundString;
this.mainString = jtta.getText();
this.length = foundString.length();
}
public void findDown()
{
int pos = jtta.getCaretPosition();
sub = mainString.substring(pos);
if (!(sub.contains(foundString)))
{
JOptionPane.showMessageDialog(null, "The search item '" + foundString + "' could not be found.", "Not found.", JOptionPane.INFORMATION_MESSAGE);
return;
}
else
{
int start = mainString.indexOf(foundString, pos);
int stop = start + length;
jtta.setSelectionStart(start);
jtta.setSelectionEnd(stop);
//jtta.setCaretPosition(stop);
}
}
public void findUp()
{
int pos = jtta.getCaretPosition();
sub = mainString.substring(0, pos);
if (!(sub.contains(foundString)))
{
JOptionPane.showMessageDialog(null, "The search item '" + foundString + "' could not be found.", "Not found.", JOptionPane.INFORMATION_MESSAGE);
return;
}
else
{
int start2 = sub.lastIndexOf(foundString, pos);
int start1 = mainString.indexOf(sub);
int start = start2 + start1;
int stop = start + length;
jtta.setSelectionStart(start);
jtta.setSelectionEnd(stop);
//jtta.setCaretPosition(stop);
}
}
public void setFoundString(final String foundString)
{
this.foundString = foundString;
this.length = foundString.length();
}
}
(Note: Should I have kept the String object "sub" local as I could get away with in findDown() or was I right to make it a class variable?)