简体   繁体   English

带有JComboBox的Java JTable

[英]Java JTable with JComboBox

I'm trying to place a JComboBox inside a certain column of a JTable. 我试图将JComboBox放在JTable的特定列中。 I have this code, and it is working: 我有此代码,并且可以正常工作:

    model = new DefaultTableModel();
    JComboBox<String> optionComboCell = new JComboBox<String>();
    optionComboCell.addItem("Option 1");
    optionComboCell.addItem("Option 2");
    optionComboCell.setSelectedIndex(1);


    table = new JTable(model);
    // Adding here all the columns, removed for clarity
    model.addColumn("Options");
    TableColumn optionsColumn = table.getColumn("Options");
    optionsColumn.setCellEditor(new DefaultCellEditor(optionComboCell));

My problem with this, is that it doesn't show as JComboBox until a cell in that column is selected. 我的问题是,在选择该列中的单元格之前,它不会显示为JComboBox。 When the JFrame is loaded, the whole table looks the same, as if all the cells where only text. 加载JFrame时,整个表看起来相同,就好像所有单元格都只有文本一样。 When clicked, it shows the combo box's arrow and options, but again when deselected, it looks like a regular cell. 单击时,它显示组合框的箭头和选项,但是再次取消选中时,它看起来像常规单元格。

Any way to get around that? 有办法解决这个问题吗?

Yes, use a JComboBox to render your cells: 是的,请使用JComboBox渲染单元格:

import java.awt.Component;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;

public class Test4 {

    private static class ComboBoxCellRenderer extends JComboBox implements TableCellRenderer {

        public ComboBoxCellRenderer(int column) {
            for (int i = 0; i < 10; i++) {
                addItem("Cell (" + i + "," + column + ")");
            }
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
            setSelectedItem(value);
            return this;
        }
    }

    protected void initUI() {
        JFrame frame = new JFrame("test");
        frame.add(getTable());
        frame.pack();
        frame.setVisible(true);
    }

    private Component getTable() {
        Vector<Vector<String>> data = new Vector<Vector<String>>();
        for (int i = 0; i < 10; i++) {
            Vector<String> row = new Vector<String>();
            for (int j = 0; j < 3; j++) {
                row.add("Cell (" + i + "," + j + ")");
            }
            data.add(row);
        }
        Vector<String> columns = new Vector<String>();
        columns.add("Column 1");
        columns.add("Column 2");
        columns.add("Column 3");
        DefaultTableModel model = new DefaultTableModel(data, columns);
        JTable table = new JTable(model);
        table.setRowHeight(20);
        int i = 0;
        Enumeration<TableColumn> c = table.getColumnModel().getColumns();
        while (c.hasMoreElements()) {
            TableColumn column = c.nextElement();
            column.setCellRenderer(new ComboBoxCellRenderer(i));
            i++;
        }
        JScrollPane scroll = new JScrollPane(table);
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        return scroll;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test4().initUI();
            }
        });
    }
}

You will need to define your own renderer for displaying components on your table, since CellEditors are only needed for editing the value within a tablecell (which is why it only reacts, when you click on a cell). 您将需要定义自己的渲染器以在表上显示组件,因为仅需要CellEditor才能编辑表单元格中的值(这就是为什么单击单元格时它仅会起作用的原因)。

Maybe have a look at Java Tutorials to learn more about the concept of renderers and editors for JTables. 也许看看Java教程,以了解有关JTables的渲染器和编辑器概念的更多信息。

尝试同时设置单元格渲染器。

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

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