• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

JComboBox cell editor for JTable

 
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am trying to create a JCombobox cell Editor for JTable by following the instructions given in the below url,

How to create JComboBox cell editor for JTable
https://www.codejava.net/java-se/swing/how-to-create-jcombobox-cell-editor-for-jtable

Getting the look & feel as expected. Able to change the menu selection. But the change in menu selection not persisting with different row selection. I mean, when I try to select the different row, unable to persist the menu selection change.

Ex: I've two rows, row1 & row2. Column 5 is named as 'Status' with different status values like 'Review', 'Complete', 'Progress', 'Inactive' ..etc. Initially row-1 is having status as 'Review' and row-2 is having status as 'Complete'. I tried to change the status of row-1 to 'Progress'. Till now, it's ok. Now I am selecting the row-2, the status of row-1 is changed to 'Review' which is it's initial value.

I tried to debug the code. Found that, setValueAT() method of AcmsFilesTableModel extends AbstractTableModel is not getting the changed selected value.
public void setValueAt(Object value, int row, int column) {}

Do we need to do any thing to persist the selected value in combobox? Please correct me if Iam doing something wrong.
Please find the attached table model & cell editor implementation classes.
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know where the code on codejava.net comes from, but IMHO their CountryCellEditor is not quality code.

Here's their implementation:Lines 38-42 create a new instance of JComboBox and populate its entries, which is slow. The idea is that this method should be fast. Speed is crucial in a Cell Renderer, and definitely not as crucial in a Cell Editor, but this method is called when the user clicks a cell for editing. The ComboBox should be already set up (as much as possible) before this method is called, so that the user does not perceive a delay. This code could be changed to instantiate the ComboBox, and populate its entries, in the constructor.

Line 44 is appropriate. If the code were changed to instantiate the ComboBox in the constructor, this line would still appear here in the getTableCellEditorComponent() method.

Line 45 is setting an ActionListener on the ComboBox. This is appropriate, so that when the user hits the enter key the Cell Editor will finish editing, rather than remaining in the cell and requiring the user to hit the escape key or click somewhere to actually complete the edit. But there are problems:
  • Presuming that we have improved the code by only instantiating the ComboBox only once, then the action listener needs to be added only once, outside of this method.
  • Having the CountryCellEditor actually implement the ActionListener interface, and adding this as the listener, is an old coding style. Modern code would probably structure this differently. But it could work with this structure, except...
  • But what it's actually doing in the actionPerformed() method (lines 58-59) isn't quite right. It is taking the selected value and storing it in the country instance variable, so it can later be returned by the getCellEditorValue() method, which I guess is fine. [If the ComboBox were an instance variable, then wouldn't need to keep country as a separate variable but could simply have getCellEditorValue() return cb.getSelectedItem()—no ActionListener needed to get getCellEditorValue() to work correctly.] But the ActionListener is not doing anything else. It's not notifying the table that the edit has completed, and it should.


  • Lines 47-51 are misguided, I think. This would be appropriate for a Cell Renderer, not so much for a Cell Editor. It's possible that something like this could work in a Cell Editor, but it would take some experimentation. As the very least, I think line 50 is setting the wrong color. It would be more appropriate to setBackground(table.getBackground()). That is, to match the ComboBox's background with the table's (regular non-selected) background, rather than to the table's (selected) foreground. But I think it would probably be best to skip these lines entirely and let the ComboBox have its default background.

    They declare the CountryCellEditor class itself (lines 16-17) is like this:I can understand why they want to extend AbstractCellEditor because that way they inherit a bunch of event-handling methods and don't have to mess with them. But I think I would prefer to do it like this instead:This way, the Cell Editor is a JComboBox, which I kind of like better, but the trade-off is that we then need to implement those event-handling methods (which isn't too bad). But the other way could work too. This is simply my preference.

    Anyway, my code might look something like this:
     
    Brian Cole
    Author
    Posts: 986
    3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Here's a quick driver for the ComboBoxCellEditor in the posting above, which I used so I could test it (minimally) before posting.

    Well, it's not so quick, because my TableModel takes 100 lines of code. But the rest of it is fairly quick. The three rightmost column of the table each have their own custom ComboBoxCellEditor, set in lines 165-167.
     
    Rancher
    Posts: 3324
    32
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    JTable already supports a combo box as an editor.

    Read the section from the Swing tutorial on Using a Combo Box as an Editor.
     
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    yes rob it does, but if you run the example, TableRender.java that they have, you'll see that when you click on the cell, you see a nice combo box and this works. However if you are not actively editing the cell, it appears as a normal cell.  I believe it is desired that the cell looks like a combo box that is closed when it is not being edited AND appears as a combo while it is being edited.

     
    Bartender
    Posts: 10964
    87
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    In the example that Rob linked to...
    CellEditor is being overridden. If you want to change the default display that is used once the CellEditor goes away you have to also override setCellRenderer().
     
    Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic