I have found your advice very helpful. I have changed updateHints method to:
- Code: Select all
public boolean updateHints(Object context) {
if (context == null) {
return false;
}
String s = context.toString();
s = s.substring(0, getTextComponent().getCaretPosition());
int indexSpace = s.lastIndexOf(" ");
int indexNewLine = s.lastIndexOf("\n");
int indexTab = s.lastIndexOf("\t");
int index = java.lang.Math.max(indexSpace, indexNewLine);
index = java.lang.Math.max(index, indexTab);
if (index != -1)
{
s = s.substring(index + "\n".length());
}
int substringLen = s.length();
if (substringLen == 0) {
return false;
}
List<String> possibleStrings = new ArrayList<String>();
for (Object o : getCompletionList()) {
String listEntry = (String) o;
if (substringLen > listEntry.length())
continue;
if (!isCaseSensitive()) {
if (s.equalsIgnoreCase(listEntry.substring(0, substringLen)))
possibleStrings.add(listEntry);
}
else if (listEntry.startsWith(s))
possibleStrings.add(listEntry);
}
Object[] objects = possibleStrings.toArray();
setListData(objects);
return objects.length > 0;
}
It works fine when I input data to my JTextField from keyboard.
When I want to read a file from a hard drive, I create new JTextField, active intellihints (like in manual) and read it line by line (for some reason).
- Code: Select all
try {
FileInputStream fstream = new FileInputStream(filePath);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
int offset = 0;
while ((strLine = br.readLine()) != null)
{
tabEl.getTextPane().getDocument().insertString(offset, strLine + "\n", null);
offset += strLine.length() + 1;
}
in.close();
} catch (Exception e) {
System.out.println("Error while reading file.");
e.printStackTrace();
}
When I want to add some text (anywhere in the document), the "s" variable does not contain the appropriate value. It seams to me that getCaretPosition does not work correct.
What do you think could go wrong?
If you would like any screen-shots or more details please reply.