0

I have a JTable with about 17 columns. For some of these columns I want ComboBoxes, for others, I don't. Some code:

public final JTable table;

void setCellEditors(){

    setBooleanCellEditor (table); // comboBox for boolean values
    setIntCellEditor (table); // comboBox for int values
    setTypeCellEditor (table); 
    setAnotherTypeCellEditor (table);
    // .. and so on, for all types I need comboboxes
}

The cellEditor function for most types look like this:

private void setTypeCellEditor (JTable jt) {
    DefaultCellEditor dce = new DefaultCellEditor (Type.buildComboBox ());
    jt.setDefaultEditor (Type.class, dce);
}

And this works fine, because that type is unique for that table, in other words, I only have one column with type Boolean, one with Int, one with AnotherType etc. The problem now is that two columns have String values, but need different ComboBoxes. Meaning, the code above doesn't work, because they are both String.class.

Naturally, I tried solving this by saying "On column 10 I want this ComboBox":

private void setYetAnotherTypeCellEditor (JTable jt) {
    DefaultCellEditor dce = new DefaultCellEditor (YetAnotherType.buildComboBox ());
    if (jt.getColumnModel ().getColumnCount() > 0) {
        jt.getColumnModel ().getColumn (9).setCellEditor (dce);
    }
}

This, however, doesn't seem to work and I do not know why. I also tried this guide but that doesn't help. Basically, I think setCellEditor isn't setting the cell editor for some reason.

It is difficult to be more specific because there is a lot of code behind this.

hokosha
  • 305
  • 2
  • 4
  • 11
  • 2
    *"One column, for example, has boolean values so I create a.."* .. check box - would be the logical control for a boolean value. – Andrew Thompson May 25 '16 at 07:35
  • you can look into this example.. http://stackoverflow.com/questions/14355712/adding-jcombobox-to-a-jtable-cell – Cedrick Clemente May 25 '16 at 07:43
  • Possible duplicate of [How to add a JComboBox to a JTable cell?](http://stackoverflow.com/questions/2059229/how-to-add-a-jcombobox-to-a-jtable-cell) – Tiz May 25 '16 at 09:10
  • 2
    Its difficult to tell what the problem you're having is. To help us answer your question, try posting a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Tiz May 25 '16 at 09:17
  • @CedrickClemente From what I can see, that is exactly what I have tried, and does not work. – hokosha May 25 '16 at 09:57
  • @Tiz It might be a duplicate, but that answer does not help me. Which I think is strange. Also, I tried to be more specific and give more code. – hokosha May 25 '16 at 09:58
  • @hokosha thanks for adding some more code. Have a look at trashgod's answer and see if that helps. Try comparing the way they've done it to what you've got and see if that helps. You could also double check that its actually getting into that `if` statement in `setYetAnotherTypeCellEditor()`- throw a `System.out.println` in there or something. – Tiz May 26 '16 at 14:22
  • @Tiz I have looked at trashgod's answer, which works great. But for some reason, the code I am dealing with does not. So it is a bit more difficult than I originally thought. Will have to look through it and see if I can be more specific. – hokosha May 27 '16 at 15:02

2 Answers2

3

It is difficult to be more specific because there is a lot of code behind this.

Using a Combo Box as an Editor illustrates setting a cell editor by column. A Minimal, Complete, and Verifiable example as shown below will allow you to study the problem in isolation.

enter image description here

import java.awt.EventQueue;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;

/**
 * @see https://stackoverflow.com/a/37435196/230513
 */
public class Test {

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTable table = new JTable(1, 2);
        table.getColumnModel().getColumn(0).setCellEditor(Type1.buildComboBox());
        table.getColumnModel().getColumn(1).setCellEditor(Type2.buildComboBox());
        f.add(new JScrollPane(table));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private static class Type1 {

        private static TableCellEditor buildComboBox() {
            return new DefaultCellEditor(new JComboBox(
                new DefaultComboBoxModel<>(new String[]{"A", "B", "C"})));
        }
    }

    private static class Type2 {

        private static TableCellEditor buildComboBox() {
            return new DefaultCellEditor(new JComboBox(
                new DefaultComboBoxModel<>(new String[]{"X", "Y", "Z"})));
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Test()::display);
    }
}
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

I don't see where you initialize the JTable named table, and I don't see where it's the same JTable passed to setYetAnotherTypeCellEditor() as the parameter named jt. I'm guessing that jt isn't null; but without any columns, jt.getColumnModel().getColumnCount() > 0 may be false. Try

private void setYetAnotherTypeCellEditor (JTable jt) {
    DefaultCellEditor dce = new DefaultCellEditor (YetAnotherType.buildComboBox());
    jt.getColumnModel ().getColumn (9).setCellEditor (dce);
}
Catalina Island
  • 7,027
  • 2
  • 23
  • 42