This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Create and dynamically change buttons in jtable column Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Create and dynamically change buttons in jtable column" Watch "Create and dynamically change buttons in jtable column" New topic
Author

Create and dynamically change buttons in jtable column

Ruchi Kumctr
Greenhorn

Joined: May 18, 2012
Posts: 8
I am having a lot of problems fully grasping tablecellrenderer and tablecelleditor. I have researched a lot, looked in a lot of forums, but still can't solve my problem.
Without posting an sscce (because my code is probably very incorrect), how would I do the following:
Create a jtable with a column that has ButtonX in it. Then when another column in a specific row gets a certain value, ButtonX will change to ButtonY, but other rows will remain what they are already. In the following example, all rows started with ButtonX, but when Progress changed from Uploading to Complete, ButtonX became ButtonY.

Row Name Progress Abort/Complete
0 File 1 Complete ButtonY
1 File 2 Complete ButtonY
2 File 3 Uploading ButtonX

The buttons need to update accordingly if a row is deleted. For example if row 0 is deleted, it should look like:
Row Name Progress Abort/Complete
0 File 2 Complete ButtonY
1 File 3 Uploading ButtonX
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
from your description you only need the renderer where you override
getCellRenderer(int row,int column)
to return a buttonRenderer if the column is [whatever the button column is] e.g. 3

and in your buttonRenderer you create/return a JButton with initial text of "ButtonX",
and if the value of the cell at column [whatever] .equals([a certain value]),
the button text changes to "ButtonY".
Ruchi Kumctr
Greenhorn

Joined: May 18, 2012
Posts: 8
That makes sense. Thanks! The only thing now is that the first time, I get the correct button. However, after my progress changes, I don't get an update in button until I click on the table (or anywhere on the applet for that matter). How do I make it programmatically update the table?
Ruchi Kumctr
Greenhorn

Joined: May 18, 2012
Posts: 8
I just repainted the table in order to update the table. But now my button is not clickable. All the forums I'm looking at say that I need to do the same thing in my tablecelleditor as in my tablecellrenderer. Is that true?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
if you need the button to do something, you'll need the editor
Ruchi Kumctr
Greenhorn

Joined: May 18, 2012
Posts: 8
Thanks for the help!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Create and dynamically change buttons in jtable column
 
Similar Threads
adding a JCheckBox in a JTable
JTable
adding JCheckBox in JTable
jtable
Customizing JTable