Important alert: (current site time 7/15/2013 3:01:10 PM EDT)
 

VB icon

BetterTable

Email
Submitted on: 3/30/2007 8:03:52 AM
By: omprakash visvanathan  
Level: Advanced
User Rating: Unrated
Compatibility: Java (JDK 1.4)
Views: 7842
(About the author)
 
     Installs a caret in a JTable that provides visual cue
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: BetterTable
// Description:Installs a caret in a JTable that provides visual cue
// By: omprakash visvanathan
//
// Inputs:TableModel
//
// Returns:void
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=5620&lngWId=2//for details.//**************************************

/* Two issues are mixed together here. One is that there is caret
as you type. The other is that, when you type into a cell, the
focus remains with the table. (In this state, you won't see the
blinking caret.) If you click again, the focus will shift to the cell
editor, and the blinking caret will appear. The arrow keys will
behave differently under these two states. When the table has the
focus the arrows will move from cell to cell. When the cell editor
has it, the arrows will move the text caret.
Sun's fix in JDK 1.4 will only address the second issue. JTable will
have a surrenderFocusOnKeystroke property. If you set it to true,
the cell editor will always have the focus, and you'll always see
the blinking caret.
If you can't use JDK 1.4, or if you want the table to keep the
focus, try this to get the caret.
This installs a custom caret which doesn't hide itself when the cell
editor loses the focus.
One disadvantage is that there is no visual cue to
tell you whether the table or the cell editor has the focus. (Sun's
visual cue is to hide the caret.)
You can easily add your own visual cue by changing the cell editor's
border when it gains or loses the focus. */
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.awt.event.*;
public class BetterTable extends JTable
{
 // You'll want to add constructors. They should all call init().
 BetterTable(TableModel mdl)
 {
super(mdl);
init();
 }
 private void init()
 {
// Either one of these two lines fixes the problem,
// but they produce different behavior by the arrow keys
//setSurrendersFocusOnKeystroke(true); // This requires JDK 1.4
installCaret(); // This works on JDK 1.3, 1.2. & 1.1
 }
 private void installCaret()
 {
DefaultCellEditor edt
 =(DefaultCellEditor)getDefaultEditor(Object.class);
JTextComponent tc=(JTextComponent)edt.getComponent();
tc.setCaret(new TableCaret(tc.getCaret()));
 }
 private class TableCaret extends DefaultCaret
implements ComponentListener
 {
int myBlinkRate;
TableCaret(Caret oldCaret)
{
 super();
 // save the old blink rate.
 myBlinkRate = oldCaret.getBlinkRate();
}
/**
 * The key to always showing the caret is in overriding the
 * focusLost method. The base class hides both the caret and the
 * selection when it loses the focus. By overriding
 * and doing nothing, both remain visible.
 */
public void focusLost(FocusEvent e) { }
/**
 * Since the caret and selection aren't visible by default,
 * we need to turn them on when the caret is installed.
 */
public void install(JTextComponent tc)
{
 super.install(tc);
 // This next line has problems in JDK 1.4
//setBlinkRate(myBlinkRate);// set the blinkRate before setVisible
 setVisible(true); // or it won't blink the first time.
 setSelectionVisible(true);
 // Use this for JDK 1.4 or JDK 1.3 to set the blink rate.
 // It won't work in JDK 1.2 or 1.1
 tc.addComponentListener(this);
}
// This ComponentListener is only needed in JDK 1.4 to
// prevent a strange but inconsequential NullPointerException.
// It fails in JDK 1.2 and 1.1.
//
// In JDK 1.3 or earlier, you can remove the ComponentListener
// code and set the blink rate in the install() method. Be sure to
// put it before the call to setVisible(). (Except for the
// spurious exception, that actually works fine in JDK 1.4, too.)
public void componentResized(ComponentEvent e)
{
 setBlinkRate(myBlinkRate);
 setVisible(true);
 getComponent().removeComponentListener(this);
}
public void componentMoved(ComponentEvent e){}
public void componentHidden(ComponentEvent e){}
public void componentShown(ComponentEvent e){}
 }
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.