Highlighting text in JTable
hi,
I have a JTable which i would like to automatically highlight the text when a column is on focus.
How can i do that ?
I already have a class that does a look ahead if user keys in any letters. This is based on Kim Topley's book (Core Swing advanced programming). What i want to do though is to highlight whatever text that is already in the column upon receiving focus.
Any help would be most appreciated. The working code is posted below.
publicclass LookAheadTextFieldextends JTextField{
privatestaticint lastSpaceIndex = 0;
protected CtrlSpaceAction ctrlSpaceAction;
public LookAheadTextField(){
this(0,null);
}
public LookAheadTextField(int columns){
this(columns,null);
}
public LookAheadTextField(int columns,
TextLookAhead lookAhead){
super(columns);
ctrlSpaceAction =new CtrlSpaceAction();
setLookAhead(lookAhead);
addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent evt){
// Remove any existing selection
//System.out.printf("document length <%d>\n",getDocument().getLength());
setCaretPosition(getDocument().getLength());
}
});
addFocusListener(new FocusListener(){
publicvoid focusGained(FocusEvent evt){
//System.out.println("LookAheadTextField: focusGained");
}
publicvoid focusLost(FocusEvent evt){
//System.out.println("LookAheadTextField: focusLost");
if (evt.isTemporary() ==false){
// Remove any existing selection
setCaretPosition(getDocument().getLength());
}
}
});
InputMap inputMap = getInputMap();
KeyStroke key = KeyStroke.getKeyStroke(KeyEvent.VK_SPACE,InputEvent.CTRL_DOWN_MASK);
inputMap.put(key,ctrlSpaceAction);
}
publicvoid setLookAhead(TextLookAhead lookAhead){
this.lookAhead = lookAhead;
}
public TextLookAhead getLookAhead(){
return lookAhead;
}
publicvoid replaceSelection(String content){
//boolean lookUp = false;
String newContent =null;
String oldContent =null;
int len = 0;
super.replaceSelection(content);
if (isEditable() ==false || isEnabled() ==false){
return;
}
Document doc = getDocument();
try{
oldContent = doc.getText(0, doc.getLength());
}catch (BadLocationException ex){
ex.printStackTrace();
}
if (content.equals(new String(" "))){
return;
}
if (doc !=null && lookAhead !=null){
try{
String strForLookUp =null;
String lookAheadStr =null;
lastSpaceIndex = doc.getText(0,doc.getLength()).lastIndexOf(' ');
if (lastSpaceIndex >= 0){
len = getCaretPosition() - lastSpaceIndex - 1;
strForLookUp = doc.getText(lastSpaceIndex+1,len);
}else{
strForLookUp = doc.getText(0,doc.getLength());
}
lookAheadStr = lookAhead.doLookAhead(strForLookUp);
if (lookAheadStr !=null && lookAheadStr.length() > 0){
newContent = doc.getText(0,lastSpaceIndex+1).concat(lookAheadStr);
// Substitute the new content
if (newContent !=null){
setText(newContent);
// Highlight the added text
setCaretPosition(oldContent.length());
moveCaretPosition(newContent.length());
}
}
}catch (BadLocationException e){
// Won't happen
}
}
}
protected TextLookAhead lookAhead;
// The TextLookAhead interface
publicinterface TextLookAhead{
public String doLookAhead(String key);
}
}

