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

时间:2021-09-20 07:20:01

Here's the idea: Let's say I have a class extending TableModel, with something like a List<List<String>> collection field. On an event, I want to clear out a JTable and re-add rows where one specific column is a combo box; the items in combo box n are the items in the List<String> from collection.get(n) in my list. So I'm iterating over collection adding rows, and I'm iterating over each collection.get(n) adding combo box items.

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

There are no event listeners. When a separate button is clicked I can work out what needs to happen as long as I can getSelectedIndex on each combo box.

没有事件监听器。当单击一个单独的按钮时,只要我可以在每个组合框上获得getSelectedIndex,我就可以计算出需要发生的事情。

I have spent two and a half hours turning my GUI into code spaghetti where I've extended virtually everything and I have maximum coupling on practically everything. I think I might even have two disjoint collections of JComboBox instances. I say this to stress that it would do absolutely no good for me to post any code I currently have.

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

I have read the oracle tutorial on JTable, all of these that strangely don't have an explanation I can understand.

我已经阅读了关于JTable的oracle教程,所有这些奇怪地没有我能理解的解释。

So basically I just need an idea of what I need, because I'm not getting anywhere with the resources I've found.

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

2 个解决方案

#1


1  

So, basically, you need a TableCelLEditor that is capable of seeding a JComboBox with the rows from the available list, for example...

所以,基本上,你需要一个能够用可用列表中的行为JComboBox播种的TableCelLEditor,例如......

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

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

        }

    }

}

#2


0  

May any of this link help you:

可以通过以下链接为您提供帮助:

http://www.java2s.com/Code/Java/Swing-JFC/Atablethatallowstheusertopickacolorfromapulldownlist.htm

http://www.java2s.com/Code/Java/Swing-JFC/Atablethatallowstheusertopickacolorfromapulldownlist.htm

or

要么

http://www.java2s.com/Code/Java/Swing-Components/ComboBoxTable.htm

http://www.java2s.com/Code/Java/Swing-Components/ComboBoxTable.htm

#1


1  

So, basically, you need a TableCelLEditor that is capable of seeding a JComboBox with the rows from the available list, for example...

所以,基本上,你需要一个能够用可用列表中的行为JComboBox播种的TableCelLEditor,例如......

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

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

        }

    }

}

#2


0  

May any of this link help you:

可以通过以下链接为您提供帮助:

http://www.java2s.com/Code/Java/Swing-JFC/Atablethatallowstheusertopickacolorfromapulldownlist.htm

http://www.java2s.com/Code/Java/Swing-JFC/Atablethatallowstheusertopickacolorfromapulldownlist.htm

or

要么

http://www.java2s.com/Code/Java/Swing-Components/ComboBoxTable.htm

http://www.java2s.com/Code/Java/Swing-Components/ComboBoxTable.htm