1

I am using this function to create a jTable, where some special cells have a jComboBox as editor :

void fillTable_(){
        final JComboBox myEditor = new JComboBox(new String[] {"yes", "no", "maybe"});
        String[][] data = new String[10][2];
            data[0][0] = "0,0";
            data[0][1] = "0,1";
            data[1][0] = "1,0";
            data[1][1] = "1,1";
            data[2][0] = "2,0";
            data[2][1] = "2,1";
            String[] columnNames = {"Nom", "Valeur"};
            DefaultTableModel model = new DefaultTableModel(data, columnNames);
            jTable1 = new JTable(model){
                DefaultCellEditor myCellEditor = new DefaultCellEditor(myEditor);
                @Override
                public TableCellEditor getCellEditor(int row, int column){
                    int modelColumn = convertColumnIndexToModel(column);
                    int modelRow = convertRowIndexToModel(row);
                    if(modelColumn == 1 && modelRow == 1){
                        return myCellEditor;
                    } else {
                        return super.getCellEditor(row, column);
                    }
                }
            };
    }

But the jTable stays empty not even plain text display. Is there something wrong?

tenorsax
  • 21,123
  • 9
  • 60
  • 107
Man o War
  • 105
  • 2
  • 7

2 Answers2

3

But the JTable stays empty; not even plain text is displayed.

You need a renderer that corresponds to your editor, as suggested in this example that contains both a ValueRenderer and a ValueEditor.

I just need to put a JComboBox in some cells of the JTable:

As discussed in How to Use Tables: Concepts: Editors and Renderers, "a single cell renderer is generally used to draw all of the cells that contain the same type of data."

I specified both row and column, but nothing is happening

Your code appears to work when I add the new JTable to a container.

TableTest

import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;

/** @see http://stackoverflow.com/questions/7200500 */
public class TableTest extends JPanel {

    public TableTest() {
        this.setPreferredSize(new Dimension(320, 240));
        final JComboBox myEditor = new JComboBox(
            new String[]{"yes", "no", "maybe"});
        String[][] data = new String[10][4];
        data[0][0] = "0,0";
        data[0][5] = "0,1";
        data[1][0] = "1,0";
        data[1][6] = "1,1";
        data[2][0] = "2,0";
        data[2][7] = "2,1";
        String[] columnNames = {"Nom", "Valeur"};
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        JTable table = new JTable(model) {

            DefaultCellEditor myCellEditor = new DefaultCellEditor(myEditor);

            @Override
            public TableCellEditor getCellEditor(int row, int column) {
                int modelColumn = convertColumnIndexToModel(column);
                int modelRow = convertRowIndexToModel(row);
                if (modelColumn == 1 && modelRow == 1) {
                    return myCellEditor;
                } else {
                    return super.getCellEditor(row, column);
                }
            }
        };
        this.add(table);
    }

    private void display() {
        JFrame f = new JFrame("TableTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TableTest().display();
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • in fact the table is already in a `JSplitPane`, and is showing proprieties of some `JTree` nodes, when they are highlighted. plain-text display was ok, but not since i added the `CellEditor`, so the table must be in a container already since it's visible. – Man o War Aug 26 '11 at 08:33
  • 2
    Absent your [sscce](http://sscce.org/), I am unable to reproduce the effect you describe; I've elaborated above. – trashgod Aug 26 '11 at 08:47
  • this is not working, i just added `System.out.println("just for testing");` under `int modelRow = convertRowIndexToModel(row);` line, but nothing is showing up! – Man o War Aug 26 '11 at 09:17
  • and what you got is just what i want – Man o War Aug 26 '11 at 09:21
  • ... but cant reach it myself! – Man o War Aug 26 '11 at 09:27
  • i was debugging the file, and created a preakpoint at the `public TableCellEditor getCellEditor(int row, int column) {` line, but it was never reached. why `getCellEditor()` is being ignored? – Man o War Aug 26 '11 at 09:48
  • 1
    @Man o War my guess (it's a guess only because you are not showing an sscce) is that the table on screen is not the table you added the breakpoint to ... – kleopatra Sep 13 '11 at 12:21
2

1) create JTable, TableModel, JComboBox, then put that together as example from official tutorial

2) for Boolean Component isn't required create own TableCellEditor, JTable by default supporting String, Integer, Double, Boolean, Date and Icon/ImageIcont in the TableCell

another examples for a.m.... here, here from this forum here, here, and here

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • in all those examples, the editor is set for the whole __column__, but in my case, i want to change the editor to a `jComboBox` only for some __cells__ – Man o War Aug 26 '11 at 07:28
  • "I want to change the editor to a `JComboBox` only for _some_ cells." You can use the `row` parameter to decide what component to return. – trashgod Aug 26 '11 at 07:42
  • @trashgod: this is exactly what i did; i specified both row and column, but nothing is happening, and table is always blank! – Man o War Aug 26 '11 at 07:44