• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting a JComponent inside a table cell to recieve focus

 
Talhah Mafawalla
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I was wondering if anyone could help. I have been having problems with this for months now. I searched the internet and read a java book but got no direct help on what i am trying to do.

I have a JTable which has 5 columns, the last column is not editable and the first column when clicked opens a new JFrame. The second column has a combo box and the third and fourth columns have JTextFields in them which are formatted for currency.

What i am trying to do is that when i tab or press enter to go into the third and fourth fields i would like the focus to shift from the cell to the JTexField so that the caret is visible and the contents are highlighed.

Any help with this would be greatly appreciated.

Thanking you
Talhah Mafawalla
 
Sean O'Donnell
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello...

I haven't worked with a JTable before, but I'm certian it's like all other Swing components. You must get a handle on the actual component in the cell, in your case the textArea, and then requestFocus() on that textArea, instead of just requestFocus() on the cell.

Hope this helps.
Sean
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JTable which has 5 columns, the last column is not editable and the first column when clicked opens a new JFrame.
You can do this but it is generally advisable to have only one top-level container in an app. JDialog is a lighter–weight alternative. A minor point.

The second column has a combo box and the third and fourth columns have JTextFields in them which are formatted for currency. ... What i am trying to do is that when i tab or press enter to go into the third and fourth fields i would like the focus to shift from the cell to the JTexField so that the caret is visible and the contents are highlighed.
A JTable is actually a drawing of the components/data it contains. It does not really show components. You can construct a component with layout managers to display components in grids that can look like a table. To create this affect in a JTable takes a little more work. The editable cells in a JTable can be changed easily enough - they use the default renderers and editors provided in JTable according to the value returned from the getColumnClass method of the TableModel interface. To actually get a component to show up in the table column you provide an editor component that matches the renderer that renders the column "component". You can configure/customize the editor with selected text, colors, caret properties and the like. When the table recognizes an edit event it shows the editor in the selected cell.
If your editor is/will be a JTextField, JComboBox or JCheckBox you can extend the DefaultCellEditor class which has constructors for these components. This is easier. Otherwise, your editor class can extend the AbstractCellEditor class and/or implement the TableCellEditor interface whose methods you'll have to implement. You can look up the source code for the DefaultCellEditor class in your SDK/src folder to get some ideas about implementing the required methods. You have a lot of leeway to create what you want this way.
 
Talhah Mafawalla
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Sean and Craig. Atleast I got a reply from you guys. I have been looking for help on this for months.

Thank you guys again
Talhah Mafawalla
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!