0

我想让 JTable 中的一个单元格成为 JComboBox 对象并能够使用它。它必须只有一个单元格,而不是整列。表格可以包含特定的单元格,但并非总是如此,它的位置不是静态的。我的问题在于把 JComboBox 放在可以使用的 JTable 中。我附上了到目前为止我能够做到的代码。表包含 JComboBox 但当我单击它时没有列表并打开单元格的编辑器。我想让列表显示出来。我应该做什么或应该在代码中添加/修改什么来实现我的目标?

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;

public class CustomCellRenderer {
    JTable table;
    TableColumn tcol;

    public static void main(String[] args) {
        new CustomCellRenderer();
    }

    public CustomCellRenderer() {
        JFrame frame = new JFrame();
        JPanel panel = new JPanel();
        Object data[][] = { { "Vinod", "Computer", "3" },
                { "Rahul", "History", "2" }, { "Manoj", "Biology", "4" },
                { "Sanjay", "PSD", "5" } };
        String col[] = { "Name", "Course", "Year" };
        DefaultTableModel model = new DefaultTableModel(data, col);
        table = new JTable(model);
        tcol = table.getColumnModel().getColumn(0);
        tcol.setCellRenderer(new CustomTableCellRenderer());
        tcol = table.getColumnModel().getColumn(1);
        tcol.setCellRenderer(new CustomTableCellRenderer());
        tcol = table.getColumnModel().getColumn(2);
        tcol.setCellRenderer(new CustomTableCellRenderer());
        JTableHeader header = table.getTableHeader();
        JScrollPane pane = new JScrollPane(table);
        panel.add(pane);
        frame.add(panel);
        frame.setSize(500, 150);
        frame.setUndecorated(true);
        frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public class CustomTableCellRenderer extends DefaultTableCellRenderer implements ActionListener {
        public Component getTableCellRendererComponent(JTable table,
                Object obj, boolean isSelected, boolean hasFocus, int row,
                int column) {
            Component cell = super.getTableCellRendererComponent(table, obj,
                    isSelected, hasFocus, row, column);

            if (((String) obj).equals("History")) {
                JComboBox comboBox = new JComboBox();
                comboBox.addItem("History");
                comboBox.addItem("English");
                comboBox.addItem("Biology");
                comboBox.addItem("PE");
                comboBox.addItem("None of the above");
                comboBox.addActionListener(this);

                return comboBox;
            }
            return cell;
        }

        public void actionPerformed(ActionEvent e) {
            System.out.println("Perform some action");
        }
    }
}
4

1 回答 1

3

您必须实现TableCellEditornot a TableCellRenderer 并使用JTable.setCellEditor(TableCellEditor)进行设置。

看看这个例子

于 2012-08-27T12:13:48.133 回答