TableCellEditor和JComboBox的问题

时间:2013-12-02 18:03:15

标签: java swing jcombobox tablecellrenderer tablecelleditor

我正在尝试使用jtable来包含其中一个列的组合框,但它不起作用,它只是作为普通的表格单元格显示。目前我正在关注oracle的例子:http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/TableRenderDemoProject/src/components/TableRenderDemo.java

我尝试了这里发布的关于这个和各种方法的所有主题,我让它工作一次,但在我尝试了不同的方法后丢失了它。 我究竟做错了什么? 我没有包括所有代码,它太长了,只是相关部分。 之前已将clientsTable声明为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 个答案:

答案 0 :(得分:0)

  

它只是作为普通的表格单元格出现。

它会发生,因为您的列具有默认渲染器。当您开始编辑时,列显示为JComboBox,因为您将DefaultCellEditor设置为JComboBox。如果您希望始终将单元格渲染为JComboBox,则可以为单元格的自定义视图实现TableCellRenderer,为此读取tutorial。这是一个简单的例子:

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);
    }
}

看起来像下一个:

enter image description here

答案 1 :(得分:0)

谢谢你的答案,我确实设法找到了什么问题。 我结束了使用tableCellEditor,你让我的组合框实现了tablecellrenderer,我之前尝试过,但我没做的主要是覆盖jtable getcellrendere / editor方法: 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);
        }

    };