将JComboBox放到JTable中并从列表中获取元素而不是编辑单元格

时间:2012-08-27 10:16:23

标签: java swing jtable jcombobox renderer

我想在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");
        }
    }
}

1 个答案:

答案 0 :(得分:3)

您必须实现TableCellEditor而不是TableCellRenderer并将其设置为JTable.setCellEditor(TableCellEditor)

请看this example