aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes color to persist in cell of jtable after combo Box selection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "color to persist in cell of jtable after combo Box selection" Watch "color to persist in cell of jtable after combo Box selection" New topic
Author

color to persist in cell of jtable after combo Box selection

Jason Richard
Ranch Hand

Joined: Oct 05, 2007
Posts: 69
Hi all

I have a ComboBox I'm using for a cellEditor. The list of items in the comboBox might have colors behind them, which I've managed to render. What I haven't figured out is a good way to keep the color in the cell after the item is selected.

I don't' want to have a persistent comboBox in the cell, i only want to see it when editing that cell.




Brett Spell
Ranch Hand

Joined: Feb 24, 2001
Posts: 96
If I understand your code and comments correctly, it looks like your combo box-based editor is being used to select a PickListEntry value for a table cell, and it looks like PickListEntry potentially contains a reference to a color that you're using for the combo box popup list background and you also want to use for the table cell when it's not being edited.

Assuming that your code is already saving the selected PickListEntry correctly, that means that it's being saved in your table model, which in turn means that when the table goes to render the non-edited cells that your table cell renderer will also (like the combo box's list cell renderer) be passed an instance of PickListEntry. In that case, you just need to create a TableCellEditor implementation that does essentially the same as your ListCellRenderer: cast the value to be rendered to a PickListEntry, retrieve the color (if any) from it, and set the renderer component's background to that color.
Jason Richard
Ranch Hand

Joined: Oct 05, 2007
Posts: 69
Brett Spell wrote:If I understand your code and comments correctly, it looks like your combo box-based editor is being used to select a PickListEntry value for a table cell, and it looks like PickListEntry potentially contains a reference to a color that you're using for the combo box popup list background and you also want to use for the table cell when it's not being edited.

Assuming that your code is already saving the selected PickListEntry correctly, that means that it's being saved in your table model, which in turn means that when the table goes to render the non-edited cells that your table cell renderer will also (like the combo box's list cell renderer) be passed an instance of PickListEntry. In that case, you just need to create a TableCellEditor implementation that does essentially the same as your ListCellRenderer: cast the value to be rendered to a PickListEntry, retrieve the color (if any) from it, and set the renderer component's background to that color.



The plot thickens. So my PickListEntry contains a bunch of data, including the label, the color (if one exists) and a few other items.

The Item selected however, gets mapped to a String in another custom object that I'm using for my datamodel in my abstractTableModel.

So we use the picklist to select the value, but the value is stored into a String. I do not have the ability to change this, as the dataobjects I'm mapping to come from a differnt API, and they need to be strings.

So once I pick the value, I want/need to force the cell to render the color of the selection. Does that make sense?
Brett Spell
Ranch Hand

Joined: Feb 24, 2001
Posts: 96
Jason Richard wrote:Does that make sense?


Frankly, no.

Well, ok, maybe a little. Let's see if I understood you correctly: you have a mapping from the PickListEntry to a corresponding String and it's that String value and not the PickListEntry that you're storing in your TableModel when a selection / edit occurs; is that right?
Jason Richard
Ranch Hand

Joined: Oct 05, 2007
Posts: 69
Brett Spell wrote:
Jason Richard wrote:Does that make sense?


Frankly, no.

Well, ok, maybe a little. Let's see if I understood you correctly: you have a mapping from the PickListEntry to a corresponding String and it's that String value and not the PickListEntry that you're storing in your TableModel when a selection / edit occurs; is that right?


correct!
Brett Spell
Ranch Hand

Joined: Feb 24, 2001
Posts: 96
It sounds like all you need is to be able to perform a reverse lookup: given a String from the table model you want to be able to get to the corresponding PickListEntry that was selected via the JList-based editor. Based on your comment that you "do not have the ability to change this" I'm assuming that you can't modify the TableModel or the class(es) that maintain the set of PickListEntry objects, because those would have been my first choices for where to suggest adding the reverse lookup method. If neither of those is an option then I'd suggest creating a singleton that maintains the String-to-PickListEntry mapping with a couple of methods like this:



and



If the String values are unique you can just use a HashMap to maintain the association; otherwise an IdentityHashMap is probably what you need.
Jason Richard
Ranch Hand

Joined: Oct 05, 2007
Posts: 69
Brett Spell wrote:It sounds like all you need is to be able to perform a reverse lookup: given a String from the table model you want to be able to get to the corresponding PickListEntry that was selected via the JList-based editor. Based on your comment that you "do not have the ability to change this" I'm assuming that you can't modify the TableModel or the class(es) that maintain the set of PickListEntry objects, because those would have been my first choices for where to suggest adding the reverse lookup method. If neither of those is an option then I'd suggest creating a singleton that maintains the String-to-PickListEntry mapping with a couple of methods like this:


I can't modify the PickListEntries. That's coming from metadata. I have full control over the table model.

Jason Richard
Ranch Hand

Joined: Oct 05, 2007
Posts: 69
I worked out my solution, ended up being pretty easy.



I also added an ActionListener to the comboBox(es)
class comboActionListener implements ActionListener
{

@Override
public void actionPerformed(ActionEvent e) {

objectModel.fireTableDataChanged();

}

}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: color to persist in cell of jtable after combo Box selection