简体   繁体   English

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

[英]Put a JComboBox to JTable and make getting an element from list instead of editing the cell

I would like to make one cell in JTable to be a JComboBox object and be able to use it. 我想使JTable中的一个单元格成为JComboBox对象并能够使用它。 It must be only one cell, not whole column. 它只能是一个单元格,而不是整个列。 The table can contain the specific cell but not always, its placement is not static. 该表可以包含特定单元格,但不能总是包含该单元格,它的位置不是静态的。 My problem lies in putting JComboBox in JTable which can be used. 我的问题在于将JComboBox放在可以使用的JTable中。 I attached a code I was able to do so far. 我附上了到目前为止我能做的代码。 Table contains the JComboBox but when I click it there is no list and turns cell's editor on. 表包含JComboBox,但是当我单击它时,没有列表,并打开单元格的编辑器。 I would like to make list show up. 我想显示清单。 What should I do or what should be added/modified in code to gain my goal? 为了达到我的目标,我应该怎么做或者应该在代码中添加/修改什么?

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

You have to implement a TableCellEditor not a TableCellRenderer and set it with JTable.setCellEditor(TableCellEditor) . 您必须实现TableCellEditor而不是TableCellRenderer,并使用JTable.setCellEditor(TableCellEditor)进行设置

Look at this example . 这个例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM