JTable's problem of hot k
To: comp.lang.java.gui
I have a JTable and set my customizing CellEditor to the column, if
the cell has been focused, the hot key to start editing it is F2, but
if i pressed any one of the function keys(F1 to F12), the cell editor
is also opened.
What's more, if i want to use Alt-F4 to close the frame, the cell
editor is opened too.
Both JDK 1.4.2 (1.4.2-b28) and JDK 6 (1.6.0_03-b05) has proved the
problem above, the code is suce as:
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.event.*;
/**
* <p>Title: TableEditorTest</p>
* <p>Description: Create a JTable and set my customizing CellEditor
to
* a specified column, use F2 to start editing the cell, but F1 to
F12
* can open the CellEditor too, what's more, Alt-F4 is also open the
* CellEditor before close the JFrame.</p>
* <p>Copyright: Copyright (c) 2007</p>
* @author Parson
* @version 1.0
*/
public class TableTest
extends JFrame
{
public TableTest()
{
JTable table = new JTable ( 2 , 2 );
getContentPane ().add ( new JScrollPane ( table ) );
// set my cell editor
table.getColumnModel ().getColumn ( 0 ).setCellEditor ( new
MyCellEditor ( this , new JTextField () ) );
table.setValueAt ( new Date () , 0 , 0 );
table.setValueAt ( new Date () , 1 , 0 );
table.setValueAt ( "haha!" , 0 , 1 );
table.setValueAt ( "we are here" , 1 , 1 );
setVisible ( true );
pack ();
}
public static void main( String args[] )
{
TableTest test = new TableTest ();
}
/**
* <p>Title: MyCellEditor</p>
* <p>Description: my cell editor, only open an option pane.</p>
* <p>Copyright: Copyright (c) 2007</p>
* @author Parson
* @version 1.0
*/
public class MyCellEditor
extends DefaultCellEditor
{
JFrame parentFrame;
Object value;
public MyCellEditor( JFrame frame , JTextField textField )
{
super ( textField );
parentFrame = frame;
textField.setEditable ( false );
}
// These methods implement the TableCellEditor interface
public Component getTableCellEditorComponent( JTable table ,
Object value ,
boolean
isSelected ,
int row , int
column )
{
( ( JTextField ) editorComponent ).setText ( "editing" );
( ( JTextField ) editorComponent ).setForeground
( Color.RED );
this.value=value;
return editorComponent;
}
public Object getCellEditorValue()
{
return this.value;
}
public boolean isCellEditable( EventObject evt )
{
if ( evt instanceof MouseEvent )
{
if ( ( ( MouseEvent ) evt ).getClickCount () >=
getClickCountToStart () )
{
return true;
}
return false;
}
return true;
}
public boolean shouldSelectCell( EventObject evt )
{
return true;
}
public boolean stopCellEditing()
{
return true;
}
public void cancelCellEditing()
{
fireEditingCanceled ();
}
public void addCellEditorListener( CellEditorListener l )
{
super.addCellEditorListener ( l );
showDialog ();
}
protected void showDialog()
{
JOptionPane.showMessageDialog(parentFrame,
"The editor is opened!");
this.cancelCellEditing();
}
}
}
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24