Is this your first time here? SwingWiki is a Java Swing Developer community site with an big archive of Swing-related usenet groups and mailing lists, but also tips, tricks and articles and book reviews written by your colleagues from around the world. If you came here through a search engine and did not find what you were looking for, make sure to check the wiki table of contents.

Edit cells like Excel

Most users have learned from working with Excel that they can start editing cell content by typing over it. JTable, however, inserts the typed characters, and does not overwrite the current content if the user just start typing. So, select the content of JTextField editors in JTable to make your application more user-friendly.

Here is a code snippet that will do the job1).

public class XCellEditor extends DefaultCellEditor
  public XCellEditor()
    super(new JTextField());
  public Component getTableCellEditorComponent(JTable parm1, Object parm2, boolean parm3, int parm4, int parm5)
    Component c=super.getTableCellEditorComponent( parm1,  parm2,  parm3,  parm4,  parm5);
    if (c instanceof javax.swing.JTextField)
      ((javax.swing.JTextField) c).selectAll();
    return c;

Install the editor with this line:

table.setDefaultEditor(java.lang.Object.class,new XCellEditor());
1) alternatively, do not subclass the DefaultCellEditor but embed the current editor in a wrapper that selects the content. This will make your code more flexible

Comments? Corrections? Contact us or Login to edit pages directly (registration is free and takes less than displaying a JLabel)
  best/edit_cells_like_excel.txt · Last modified: 2005/10/29 06:48 by (icyocean)
Recent changes | RSS changes | Table of contents | News Archive | Terms And Conditions | Register | The Quest For Software++| Ruby Resources

Sedo - Buy and Sell Domain Names and Websites project info: Statistics for project etracker� web controlling instead of log file analysis