File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JTextField  focus problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextField  focus problem" Watch "JTextField  focus problem" New topic
Author

JTextField focus problem

Yvonne Wooyt
Greenhorn

Joined: Feb 08, 2012
Posts: 8
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

Joined: May 03, 2008
Posts: 4527
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Yvonne Wooyt
Greenhorn

Joined: Feb 08, 2012
Posts: 8
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

Joined: Oct 27, 2005
Posts: 19670
    
  18

JTable.editCellAt


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Yvonne Wooyt
Greenhorn

Joined: Feb 08, 2012
Posts: 8
Rob Spoor wrote:JTable.editCellAt


Not working with custom renderers where inside the cells are other components.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Any ideas ?

post something we can compile/run/observe/tinker i.e. the link in Darryl's reply
Yvonne Wooyt
Greenhorn

Joined: Feb 08, 2012
Posts: 8
Class Main.java


MyTableExample.java


MyTableModel.java


MyDataTwo.java

MyDataOne.java


All source code, with 2 classes described above.

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Feb 08, 2012
Posts: 8
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

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Feb 08, 2012
Posts: 8
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

Joined: Feb 08, 2012
Posts: 8
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

Joined: Feb 08, 2012
Posts: 8
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTextField focus problem