• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

color to persist in cell of jtable after combo Box selection

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.




 
Ranch Hand
Posts: 118
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();

}

}
 
reply
    Bookmark Topic Watch Topic
  • New Topic