Trouble with TableCellEditor and JComboBox

342 views Asked by At

I'm trying to get a jtable to contain combo boxes for one of it's columns but it doesn't work, it just appears as a normal table cells. at the moment i'm following oracle's example: http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TableRenderDemoProject/src/components/TableRenderDemo.java

I tried all the topics posted here about this and various methods as well, i got it to work once but lost it after i tried a different method. What am i doing wrong? I'm not including all the code, it's way too long, just the relevant part. clientsTable has been declared before as a jTable.

    // Define Table model for clients table
    class ClientsTableModel extends DefaultTableModel {

        public ClientsTableModel(Vector<Vector<String>> clientsDataVector,
                Vector<String> clientColumNamesVector) {
            super(clientsDataVector, clientColumNamesVector);
        }

        @Override
        public int getColumnCount() {
            return clientColumNames.length;
        }

        @Override
        public int getRowCount() {
            return clientsDataVector.size();
        }

        @Override
        public String getValueAt(int row, int column) {
            return clientsDataVector.get(row).get(column);
        }

        @Override
        public void setValueAt(Object aValue, int row, int column) {
            clientsDataVector.get(row).set(column, (String) aValue);
        }
        @Override
        public Class<?> getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

    }

    // create table model and add to clients table
    clientColumNames = new String[] { "ID", "Name", "Type", "Address",
            "Email", "Phone", "Comment" };
    clientColumNamesVector = new Vector<String>(
            Arrays.asList(clientColumNames));
    clientsDataVector = new Vector<Vector<String>>(1, 1);
    clientsTableModel = new ClientsTableModel(clientsDataVector,
            clientColumNamesVector);
    clientsTableModelEvent = new TableModelEvent(clientsTableModel);
    clientsTableModel.addTableModelListener(new TableModelListener() {
        @Override
        public void tableChanged(TableModelEvent arg0) {
        }
    });

    // create clients table and set type column to be combo box
    String[] clientTypes = { "REGULAR", "GOLD", "PLATINUM" };
    clientsTable = new JTable(clientsTableModel);
    clientsTable.setAutoCreateRowSorter(true);
    clientsTable.setFillsViewportHeight(true);
    JComboBox clientsTypeComboBox = new JComboBox(clientTypes);
    TableColumn clientsTypeColumn = clientsTable.getColumnModel().getColumn(2);
    clientsTypeColumn.setCellEditor(new DefaultCellEditor(clientsTypeComboBox));
    DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer();


    // create client scroll pane
    JScrollPane clientsScrollPane = new JScrollPane(clientsTable);
    GridBagConstraints gbc_clientsScrollPane = new GridBagConstraints();
    gbc_clientsScrollPane.insets = new Insets(0, 0, 5, 0);
    gbc_clientsScrollPane.fill = GridBagConstraints.BOTH;
    gbc_clientsScrollPane.gridx = 0;
    gbc_clientsScrollPane.gridy = 0;
    viewClientsPanel.add(clientsScrollPane, gbc_clientsScrollPane);
2

There are 2 answers

0
ido flax On

thank you for your answers, i actually managed to find what was wrong. i endded up using tableCellEditor and u made my combobox to implement tablecellrenderer, which i tried before but the main thing that i didn't do is to override jtable getcellrendere/editor methods: clientsTable = new JTable(clientsTableModel){

        @Override
        public TableCellEditor getCellEditor(int row, int col) {
            if (col==2) return clientsTypeCellEditor;
            return super.getCellEditor(row, col);
        }

        @Override
        public TableCellRenderer getCellRenderer(int arg0, int arg1) {
            // TODO Auto-generated method stub
            return super.getCellRenderer(arg0, arg1);
        }

    };
1
alex2410 On

it just appears as a normal table cells.

It occures, because you have default renderer for your column. When you start to edit, column shows as JComboBox, because you set DefaultCellEditor with JComboBox. If you want to render cells as JComboBox always, you can implement a TableCellRenderer for custom view of cell, read tutorial for that. Here is simple example for you:

import java.awt.Component;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

public class Example extends JFrame {

    public Example() {
        DefaultTableModel m = new DefaultTableModel(new Object[][]{{"2",2,3},{"1",4,5}},new Object[]{1,2,3});
        JTable t = new JTable(m);
        t.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(new JComboBox(new Object[]{"1","2"})));
        t.getColumnModel().getColumn(0).setCellRenderer(getCellRenderer());
        t.setRowHeight(25);
        getContentPane().add(new JScrollPane(t));
        pack();
    }


    private TableCellRenderer getCellRenderer() {
        return new TableCellRenderer() {

            JComboBox box = new JComboBox(new Object[]{"1","2"});
            @Override
            public Component getTableCellRendererComponent(JTable arg0, Object arg1,boolean arg2, boolean arg3, int arg4, int arg5) {
                box.setSelectedItem(arg1);
                return box;
            }
        };
    }


    public static void main(String[] args) throws InterruptedException {
        Example ex = new Example();
        ex.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ex.setVisible(true);
    }
}

and its look like next:

enter image description here