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 Another JTable Cell Editor / Renderer question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Another JTable Cell Editor / Renderer question." Watch "Another JTable Cell Editor / Renderer question." New topic
Author

Another JTable Cell Editor / Renderer question.

David Garratt
Ranch Hand

Joined: Aug 08, 2003
Posts: 196

I have a complex JTable which is totally dynamic in as much as the number and type of columns are determined at runtime. Also I have the first 3 columns fixed with only the variable columns scrolling. Needless to say the code is a little complex and for this reason I'm not posting any code at the moment. Everything is working as intended apart from one annoying issue. I have the ability to define the editor for a column as being a JComboBox with the list of possible values also dynamically loaded from a table. The renderer is working ok and the table model seems to work fine as the selected value from the combo box is stored correctly. I can set random values within a single column and they are all rendered correctly after being set from the popup list.

The only problem I have is that the combo box put's a marker in it's list (default value) according to the last value selected by the combo box - in any cell within the column, rather than the actual cell it's been activated on. If you look at pic2 you will see that there is a tick next to the 1st value in the list even though the cell previously contained 001 - Pass.

If I select 001 - Pass in this cell and then move onto the next row the ticked item will be 001 - Pass even if the cell contained a different value.

I have experimented "to death" with "setSelectedItem(value)" at the bottom of my cell renderer but I'm not having any joy. My combo box contained a list of objects where the object contains a key/description pair and has a toString() method to make the cell renderer display the values nicely.

I appreciate it's difficult to answer without seeing the code, but I'm reluctant to post too much out of context stuff. A nice example using a JTable combo box renderer/editor would be great but I've not found one and the only close examples use plain string lists for the combo box values.

Clues / Comments most welcome

Thanks in advance

Dave




[Thumbnail for pic1.jpg]


[Thumbnail for pic2.jpg]

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I have a complex JTable...

just create a 1-column table for a JComboBox (hard code the contents),
add your renderer/editor code, add the table/scrollpane to a frame, make
sure it compiles/runs/displays the problem, and post that code.

often, in renderers/editors, if there is an 'if', there needs to be an 'else'
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4531
    
    5

The only problem I have is that the combo box put's a marker in it's list (default value) according to the last value selected by the combo box - in any cell within the column, rather than the actual cell it's been activated on. If you look at pic2 you will see that there is a tick next to the 1st value in the list even though the cell previously contained 001 - Pass.

If I select 001 - Pass in this cell and then move onto the next row the ticked item will be 001 - Pass even if the cell contained a different value.


Form that I would conclude that the selected item isn't being set in getTableCellEditorComponent(...)


luck, db
There are no new questions, but there may be new answers.
David Garratt
Ranch Hand

Joined: Aug 08, 2003
Posts: 196

I will try and create a standalone simple example - thanks for everyones help.

Dave
David Garratt
Ranch Hand

Joined: Aug 08, 2003
Posts: 196

I have created a small app which has the basic problem described. I have put the source in an attached zip, but it's not permitted to upload a zip. Is there a alternative - I was trying not to paste all the code if possible.

Thanks

Dave
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
if it's a small app it would be < 50 lines, so post the code (inside code tags, so it retains formatting)
David Garratt
Ranch Hand

Joined: Aug 08, 2003
Posts: 196

Unfortunately being a JTable with renderers and cell editors etc I'm not able to get it down to that size.

But if anyone is willing to take I look I can email a small zip with the source.

My email is davegarratt@mailshack.com

Thanks

Dave
David Garratt
Ranch Hand

Joined: Aug 08, 2003
Posts: 196

I have posted a pic of the cut down demo app below. There are 2 specific problems which are driving me nuts.

1) The combo box in the right hand column. When I click on one the combo drop downs the popup window appears as normal and I can choose the new value at leisure. However when I repeat the process on the next combo it appears and disappears in a flash as if it's processing some click event is still in a queue. You can click and hold the mouse to access it, but the 1st time and 2nd time you access the combo's its not acting the same.

2) My combo box (model) is populated with a object rather than a string. The object is very simple and contains 2 public strings called "key" and "description" - I do this because I only want to store the key in the database and use the description to make the selection a little more user friendly. My object has a toString() method so that the combo box displays each item in the format "key - description" - All that works ok. What does not work it that the selected item in the combo does not reflect the current item in the cell. I'm trying to create an instance of my object with key and description matching the item in the list - but no joy.

The later problem is not helped by most examples I've seen in the internet using simple literal strings for the combo box values rather than objects.

As per above post I can email the zip of my source to anyone who is willing to help.

Many thanks

Dave


[Thumbnail for test.jpg]

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I have posted a pic of the cut down demo app below. There are 2 specific problems which are driving me nuts.

both problems relate to just 1 of the columns:
- cut the table down to just that column
- hard-code 2 or 3 items for the combo's
- add your renderer/editor
- post the code here (cannot possibly be overly long)

David Garratt
Ranch Hand

Joined: Aug 08, 2003
Posts: 196

Here goes then :-

testFrame.java = Frame to display table
tableModel.java = Dummy model to return data (celEditorListItem's)
cellRenderer.java = Renderer for Combo Box containing (cellEditorListItems)
cellEditorListItem.java = Simple class containing a key and a description
cellEditorList.java = Cell Editor for Combo Box populated with cellEditorListItems


testFrame.java



tableModel.java



cellRenderer.java



cellEditorListItem.java



cellEditorList.java


>
David Garratt
Ranch Hand

Joined: Aug 08, 2003
Posts: 196

Additional info.

I am running on a Mac (OSX Lion)

java version "1.6.0_33"
Java(TM) SE Runtime Environment (build 1.6.0_33-b03-424-11M3720)
Java HotSpot(TM) 64-Bit Server VM (build 20.8-b03-424, mixed mode)

David Garratt
Ranch Hand

Joined: Aug 08, 2003
Posts: 196

Just wondered - has anyone felt brave enough to take a look at this for me ?

I'm stumped.

Thanks

Dave
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
in tableModel's getValueAt(), add the indicated line

the printout is endless - if you can fix that, it might solve your problem
David Garratt
Ranch Hand

Joined: Aug 08, 2003
Posts: 196

I think I have eliminated the repeated called to getValue - although not entirely sure why it makes a difference, by commenting out the following line from the testFrame.java

However this does not seem to correct either of the 2 reported problems.

Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

The problem is not with your renderer. In fact, the renderer is redundant in your case. As you have toString() implemented, it will be called by the defaul renderer in the table.

The problem is that, you add 'cellEditorListItem' objects to the combo first - as the options. Then, in the getValueAt(), you again create new objects of 'cellEditorListItem' - these may be equal to the human eye. but it is not equal in the Java world. So, when the editor sees that its option is not equal to the value in the table, it just shows the first value.
So, you should first create the list of options and share them between the table model and the combo box. The getValueAt should return an existing object based on your comparison - there is no need to create the objects repeatedly. In fact, this is a waste as the getValueAt will be called many times whenever the table paints itself. Overriding equals can be considered, but in any case, creating objects repeatedly within getValueAt is not good.

In normal cases, String objects would be used. As 2 strings of the same value anyway point to the same object, it will work fine. Or, it may be enum values which would again refer to the same object.
You should do the same with your dynamic stuff.

PS: Having combo box as editors is actually easy. I would suggest extending the DefaulCellEditor and you can call invoke the super class constructor by passing the JComboBox instance and rest should work.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
David Garratt
Ranch Hand

Joined: Aug 08, 2003
Posts: 196

I think I understand what your saying. However this snippet of code is not really representative of my full application as it creates a table dynamically and I don't know if a column will be represented as a combo box or checkbox or textfield until I interrogate the contents of another "definition" table on the fly. Fundamentally the problem is as I understand it that two objects created independently won't be equal. Somehow I'm going to have to find a way to create a common list of objects between the model and the combo on the fly.

I definitely need a good nights sleep to get my head around this one.

Thanks very much for you help so far.

Dave
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1076
    
  10

Thats right. You need to find a way to create the list of objects and pass it around.

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Another JTable Cell Editor / Renderer question.