对于JTable的一列,如何在每一行中放置一个唯一的组合框编辑器?

时间:2013-10-23 07:58:01

标签: java swing jtable jcombobox tablecelleditor

这是一个想法:假设我有一个扩展TableModel的类,类似于List<List<String>> collection字段。在一个事件中,我想清除JTable并重新添加一个特定列是组合框的行;组合框n中的项目是我列表中List<String>的{​​{1}}中的项目。所以我正在迭代collection.get(n)添加行,我正在迭代每个collection添加组合框项目。

没有事件监听器。当单击一个单独的按钮时,只要我能在每个组合框上collection.get(n),我就可以计算出需要发生的事情。

我花了两个半小时将我的GUI转换为代码意大利面条,我几乎扩展了所有内容,而且几乎所有东西都有最大的耦合。我想我甚至可能有两个不相交的getSelectedIndex实例集合。我这样说是为了强调我发布我目前拥有的任何代码绝对没有用。

我已阅读the oracle tutorial on JTable all these strangely have没有{{3}}我能理解的解释。

所以基本上我只需要知道我需要什么,因为我找不到我找到的资源。

2 个答案:

答案 0 :(得分:1)

因此,基本上,您需要TableCelLEditor能够使用可用列表中的行播种JComboBox,例如......

enter image description here

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

public class TableCellEditor {

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

    public TableCellEditor() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                List<List<String>> values = new ArrayList<>(25);
                for (int row = 0; row < 10; row++) {

                    List<String> rowValues = new ArrayList<>(25);
                    for (int index = 0; index < 10; index++) {
                        rowValues.add("Value " + index + " for row " + row);
                    }
                    values.add(rowValues);

                }

                DefaultTableModel model = new DefaultTableModel(new Object[]{"Data"}, 10);
                JTable table = new JTable(model);
                table.setShowGrid(true);
                table.setGridColor(Color.GRAY);
                table.getColumnModel().getColumn(0).setCellEditor(new MyEditor(values));

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new JScrollPane(table));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class MyEditor extends DefaultCellEditor {

        private List<List<String>> rowValues;

        public MyEditor(List<List<String>> rowValues) {
            super(new JComboBox());
            this.rowValues = rowValues;
        }

        @Override
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {

            JComboBox cb = (JComboBox) getComponent();
            List<String> values = rowValues.get(row);
            DefaultComboBoxModel model = new DefaultComboBoxModel(values.toArray(new String[values.size()]));
            cb.setModel(model);

            return super.getTableCellEditorComponent(table, value, isSelected, row, column);

        }

    }

}

答案 1 :(得分:0)