0

我正在使用此代码添加JComboboxJTable,但是当我运行此代码时,没有组合框被添加到Jtable唯一的标题中显示在Jtable.

Private JTable ScrollPaneTable;

public JTable getScrollPaneTable()
    {
        if(ScrollPaneTable == null)
        {
            try
            {
                ScrollPaneTable = new JTable();
                ScrollPaneTable.setName("ScrollPaneTable");
                getJScrollPane1().setColumnHeaderView(ScrollPaneTable.getTableHeader());
                TableColumn sportColumn = ivjScrollPaneTable.getColumnModel().getColumn(0);
                sportColumn.setCellEditor(new DefaultCellEditor(getCombo1())); //getCombo1                      
                            returns the JCombo box reference whose items are already added.
                ScrollPaneTable.setBounds(0, 0, 200, 200);
            }
            catch(Throwable Exc)
            {
                handleException(Exc);
            }
        }
        return ScrollPaneTable;
    }

在这里,我只添加了一个组合,但我想在其他列中使用更多组合。

4

1 回答 1

1

发布SSCCE,这对我有用,

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTable;
import javax.swing.table.TableColumn;

public class TestJComboBox {

    public static void main(String args[]) {

        JFrame frame = new JFrame("Example of JCombobox in JTable");
        frame.setSize(450, 250);

        JTable table = new JTable(5,5);

        TableColumn testColumn = table.getColumnModel().getColumn(0);

        JComboBox comboBox = new JComboBox();
        comboBox.addItem("This");
        comboBox.addItem("is");
        comboBox.addItem("a");
        comboBox.addItem("Sample program");
        comboBox.addItem("for");
        comboBox.addItem("StackOverflow");
        testColumn.setCellEditor(new DefaultCellEditor(comboBox));

        frame.add(table);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        frame.setVisible(true);
    }
}
于 2012-05-07T08:08:45.630 回答