January 21, 2005

Java tip #1

Working with Swing JTables can be tricky. Working with custom cell editors is even trickier. Here's a tip that may save you some time and frustration.

This is a custom cell editor (based on a text field) that will automatically highlight its contents as soon as it is becomes active and save the user from having to select the text with the mouse or with the all-too-common "home - shift+end" key sequence.

The trick is to select the text after the editor gets focus. Otherwise, the JTextField object will ignore the request.

Here's the code:

private class CustomTableCellEditor
    extends DefaultCellEditor
{
  public CustomTableCellEditor()
  {
     // use a JTextField as the actual editor
     super(new JTextField());
     final JTextField editor = (JTextField) getComponent();

     editor.setName("Table.editor");
     editor.setBorder(new LineBorder(Color.black));

     // listen for focusing events
     editor.addFocusListener(new FocusListener()
     {
       public void focusGained(FocusEvent e)
       {
         // now is the time to select the text in the JTextField
         editor.selectAll();
       }

       public void focusLost(FocusEvent e) { }
     });
   }

   public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
   {
     final JTextField textField = (JTextField) super.getTableCellEditorComponent(table, value, isSelected, row, column);

     Runnable task = new Runnable() {
       public void run()
       {
         // set the focus on the JTextField
         textField.requestFocusInWindow();
       }
     };
     SwingUtilities.invokeLater(task);

     return textField;
   }
}

3 Comments:

Blogger Ljudevit said...

This is awesome! THNX!

December 17, 2006 1:13 PM  
Blogger nasha said...

Why was there no follow on bankruptcy then? The bailout of AIG FP went to (wow power leveling) hedge funds that bound credit swaps on Lehman failing or others betting on rating (wow power leveling) declines. AIG has drained over 100 billion from the government. Which had to go to (wow power leveling) those who bet on failures and downgrades. Many of whom (power leveling)were hedge funds. I-banks that had offsetting swaps needed the money from the AIG bailout or they would have been caught. Its an (wow powerleveling) insiders game and it takes just a little bit too much time for most people to think (wow gold) through where the AIG 100 billion bailout money went to, hedge funds and players, many of whom hire from the top ranks of DOJ, Fed, Treasury, etc. ZHANG XIAO CHEN

April 19, 2009 10:03 PM  
Blogger uwanna said...

gucci replica handbags
men gucci shoes
Gucci men sneakers
Gucci men moccasins
gucci women sneakers
gucci women boots
Gucci men boots
Gucci
Gucci shop
Gucci bags
Gucci shoes
Replica GUCCI SHOES
wholesale gucci shoes
cheap Gucci handbags
Gucci ON sale
Gucci Belts
Gucci small accessories
Gucci hats & scarves
Gucci wallets
Gucci Handbags
Women Gucci shoes
Men Gucci shoes
discount gucci shoes
cheap Gucci shoes

February 04, 2010 9:34 PM  

Post a Comment

Links to this post:

Create a Link

<< Home