对于JTable的一列,如何在每一行中放置一个唯一的组合框编辑器? [英] For one column of a JTable, how do I put a unique combo box editor in each row?

查看:111
本文介绍了对于JTable的一列,如何在每一行中放置一个唯一的组合框编辑器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是一个主意:假设我有一个扩展TableModel的类,带有类似List<List<String>> collection的字段.在某个事件中,我想清除JTable并重新添加其中一个特定列为组合框的行;组合框n中的项目是我列表中collection.get(n)List<String>中的项目.因此,我要遍历collection添加行,并且遍历每个collection.get(n)添加组合框项目.

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.

没有事件侦听器.单击单独的按钮后,只要我可以在每个组合框上按getSelectedIndex键,我就可以确定需要发生的情况.

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.

我花了两个半小时将我的GUI变成代码面条,在其中我几乎扩展了所有内容,并且几乎在所有内容上都具有最大的耦合性.我想我甚至可能有两个不相交的JComboBox实例集合.我说这是为了强调,发布我目前拥有的任何代码绝对对我没有好处.

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.

我已阅读有关JTable的Oracle教程全部 这些 奇怪地 "我可以理解的解释.

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

所以基本上我只需要知道自己需要什么,因为我无法利用所找到的资源.

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

推荐答案

因此,基本上,您需要一个TableCelLEditor,该TableCelLEditor能够使用可用列表中的行作为JComboBox的种子,例如...

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

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

        }

    }

}

这篇关于对于JTable的一列,如何在每一行中放置一个唯一的组合框编辑器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆