aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes jButton in jTable Cell with Action Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "jButton in jTable Cell with Action" Watch "jButton in jTable Cell with Action" New topic
Author

jButton in jTable Cell with Action

Fidel Edwards
Ranch Hand

Joined: Mar 19, 2008
Posts: 229
Hi Members,

I do not know where I am wrong. I tried lots of thing for designing a JTable having JButton also but everything goes in vain. I do not know how to add two textboxes and two buttons in a single row.

Here is my code.


I want to show them as in my table
--------------------------------------------------------------------------------------
Name | Role | Edit | Delete
---------------------------------------------------------------------------------------
jTextField/Table Cell | jTextField/Table Cell | Edit Button | Delete Button
-----------------------------------------------------------------------------------------

I do not know where I am wrong. Please correct me.


Thanks in advance !

God Gave Me Nothing I Wanted, He Gave Me Everything I Needed.
OCPJP6
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2090
    
    7
Table Button Column should help.
Fidel Edwards
Ranch Hand

Joined: Mar 19, 2008
Posts: 229
Rob Camick wrote:Table Button Column should help.


Thanks Rob for reply!

But I am not able to understand that how should I use it ?
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2090
    
    7
You add text to every row/column like you do for your other data. So in the 3rd column you add "Edit" for each row. In the 4th column you add "Delete" for every row. Then you create two different TableColumn classes to be used as the renderer/editor of each of those columns.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

You just must make sure that the cells in that column are editable (as returned by the TableModel's isCellEditable method), or the buttons will not be activated as cell editors.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Fidel Edwards
Ranch Hand

Joined: Mar 19, 2008
Posts: 229
Rob Camick wrote:You add text to every row/column like you do for your other data. So in the 3rd column you add "Edit" for each row. In the 4th column you add "Delete" for every row. Then you create two different TableColumn classes to be used as the renderer/editor of each of those columns.


Exactly !!

But little more explanation would be more appreciable and helpful !

Thanks for reply !
Fidel Edwards
Ranch Hand

Joined: Mar 19, 2008
Posts: 229
Rob Prime wrote:You just must make sure that the cells in that column are editable (as returned by the TableModel's isCellEditable method), or the buttons will not be activated as cell editors.


I am new to Swing (and JTable is most mind shaking and cool Component of swing). Any better tutorial (except Sun Java that I read too many time and still banging my head on computer ) will be highly appreciable.


Thanks for reply !

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2090
    
    7
Any better tutorial (except Sun Java that I read too many time and still banging my head on computer


There is nothing wrong with the Sun tutorial on using Swing. It teaches you the basics. Why are you even tring to add a button to a table when you don't know the basics?

Start by creating a table with a single row of data. The row should contains 4 Strings to represent the 4 columns of your table. Once you master that we can move forward.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Mr. Camick suggests a reasonable plan of action. However, I can't help but comment
on this part of your code.

Ashish Tiwari wrote:

It is ok to have one class that is both a CellRenderer and CellEditor like this, but you
shouldn't use the same instance as both a Renderer and an Editor. (The table could
very well need to repaint part of itself while the Editor is active.) So simply instantiate
two instances, one to be used as a Renderer and one as a Renderer. You can share
those two instances over multiple columns and multiple JTables, though.

[edit: Actually, looking at your implementation of ActionPanelEditorRenderer, I see
that you are returning different components getTableCellRendererComponent() and
getTableCellEditorComponent(). So I guess it's ok to use a single instance as both
a Renderer and an Editor simultaneously. I think it would be cleaner to split it into
two classes, though.]

bitguru blog
Fidel Edwards
Ranch Hand

Joined: Mar 19, 2008
Posts: 229
Rob Camick wrote:
Any better tutorial (except Sun Java that I read too many time and still banging my head on computer


Why are you even tring to add a button to a table when you don't know the basics?

Start by creating a table with a single row of data. The row should contains 4 Strings to represent the 4 columns of your table. Once you master that we can move forward.


Thanks Rob!

I got it sorry for moving hastily


Thanks for advice !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jButton in jTable Cell with Action
 
Similar Threads
JButton in JTable
JTable ........
problem in JTable
JRadioButton in JTable
Row Selection problems with JTable