Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JTextField focus problem

 
Yvonne Wooyt
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have problem with active textfield when start the GUI.
Got the JTextField component inside JTable, and the jTable inside another JTable ie. JTableOuter - > JTableInner - > JTextField.
If window is displayed, I set up the focus for the first JTextField component, but nothing happened, the focus occurs and component
is active when I do mouse clicking on it.

I modified this source code:
http://pekalicious.com/blog/custom-jpanel-cell-with-jbuttons-in-jtable/

Any ideas ?
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Yvonne and welcome to the Ranch!

A JTable's editor doesn't get focused by a call to requestFocus[inWindow](...). To do that, you need to set the table in editing mode -- see the API for the correct method to use for that..

In your case, with nested tables, it might get a little tricky. Take a shot at it and post your best efforts, in the form of a SSCCE (Short, Self Contained, Correct (Compilable), Example) if you can't achieve your goal.
 
Yvonne Wooyt
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still no solution for JTextField inside JTable, and JTable inside another JTable cell.

See the example JTextField inside JPanel, and JPanel inside JTable cell.



Where, MyCellRenderer.java


And, MyCellComponent.java


What I want to achieve, after window displaying make first JTextField in table row 0 focused ie. active with cursor, without mouse cliking.
Any ideas ?
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JTable.editCellAt
 
Yvonne Wooyt
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:JTable.editCellAt


Not working with custom renderers where inside the cells are other components.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Any ideas ?

post something we can compile/run/observe/tinker i.e. the link in Darryl's reply
 
Yvonne Wooyt
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class Main.java


MyTableExample.java


MyTableModel.java


MyDataTwo.java

MyDataOne.java


All source code, with 2 classes described above.

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code also has a huge focus problem - tabbing into or out of the table doesn't work.
tabbing, once in the table doesn't work. the best seems to be ctrl-tab to move between
the 3 components of a single row - won't go to the next row.

gut feeling is the table should be 3 columns - just seems one column with 3 components
is breaking everything.
 
Yvonne Wooyt
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using one table with lots of columns and the components in the cells (JButtons, JTextField, JComboBoxex, JLabels) instead of JPanel with components would be very hard in my situation.
Can you give me a good working example, please ? To make sure it really works, before I start changing my code ?

Hmm, in my real source code I am having 2 tables. What I can do, instead of inner Table I can use panels and grid layout - so it will remove one of them.

The outer table gives me possibility to use several rows, which I can add/remove/move up/down ?
Similar work I can do with JList, but can I make JList editable ? And what about the focus inside the JList rows (panels with other components) ?

Have we got other possibilities in Swing Components ( must work under java 1.5) where we can add/remove/move up/down selected rows ?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> Can you give me a good working example, please ?

working example - yes, good example - doubtful
(couldn't quite get the button editor to work properly,
so I copied a working example of one from google)



> Similar work I can do with JList

funny thing is, the most common recommendation
for an editable JList is to use a single column JTable,
which sounds like what you're doing now.
 
Yvonne Wooyt
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Michael for your example, it works ... but does not solve my problems.

What you did, you just use custom renderers in 3d column.
The 2 first columns do not have components ... just the jTable.cellEditAt function to normal JTable cell with set cell border (no "|" key in this cell).

No JTextField which I wanted and described. Also in the 2 column I have to use also another components such as JComboBoxes.
So in 2 second column could be JtextField ( the editable JTable cell - fine), JLabel ( no editable JTable cell - fine)
and also JComboBoxes , so for this need custom renderers.
 
Yvonne Wooyt
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, have found the example and I have done modification and it works similar to Michael example but in JTable cells we go JTextField.



That's step forward ... .
 
Yvonne Wooyt
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we want to have active TextField inside JTable cell ( in this situation JTextfields are located on JPanel ) without mouse clicking. See the example:



Life becomes much easier now
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic