【问题标题】:How to add JComboBox on JTable cell on MouseClick and based on other cells values from the Same JTable如何在鼠标单击时在 JTable 单元格中添加 JComboBox 并基于来自同一 JTable 的其他单元格值
【发布时间】:2016-06-10 05:44:47
【问题描述】:

JTable:

我在 NetBeans 中创建了一个 JTable,我将在其中从 database 的某些列中获取值,如 image 所示 我为 TESTNAME,UNITS,SPECIFICRANGE 列带来了值,但第二列 OBSERVED VALUE 我为 用户输入 保留为空,用户输入是这样的,每当用户点击 Color 前面的单元格时,他应该在第二列单元格中得到一个 JComboBox 我的意思是 JComboBox 前面的单元格在 MouseEvent 和其他单元格上使用strong>Color,我正在使用 editCellAt() 为了完成这一点,我编写了以下代码,当我点击颜色前面的单元格时,我得到 JComboBox,当我点击其他单元格时,我得到 JComboBox strong> 但我需要获得 editCellAt() 功能

我认为 DefaultCellEditor 正在为整列修复,但我只需要 MouseClick

上的特定单元格
if(table.getValueAt(table.getSelectedRow(),0).toString().equals("Color"))
{
   TableColumn colorColumn = table.getColumnModel().getColumn(1);
   JComboBox comboBox = new JComboBox();
   comboBox.addItem("Red");
   comboBox.addItem("Greyish");
   comboBox.addItem("Yellow");
   colorColumn.setCellEditor(new DefaultCellEditor(comboBox));
}               
else
{
   table.editCellAt(table.getSelectedRow(), 1);
}

【问题讨论】:

    标签: swing jtable mouseevent jcombobox tablecolumn


    【解决方案1】:

    这是一个展示如何动态返回自定义编辑器的示例:

    import java.awt.*;
    import java.util.List;
    import java.util.ArrayList;
    import javax.swing.*;
    import javax.swing.border.*;
    import javax.swing.table.*;
    
    public class TableComboBoxByRow extends JPanel
    {
        List<String[]> editorData = new ArrayList<String[]>(3);
    
        public TableComboBoxByRow()
        {
            setLayout( new BorderLayout() );
    
            // Create the editorData to be used for each row
    
            editorData.add( new String[]{ "Red", "Blue", "Green" } );
            editorData.add( new String[]{ "Circle", "Square", "Triangle" } );
            editorData.add( new String[]{ "Apple", "Orange", "Banana" } );
    
            //  Create the table with default data
    
            Object[][] data =
            {
                {"Color", "Red"},
                {"Shape", "Square"},
                {"Fruit", "Banana"},
                {"Plain", "Text"}
            };
            String[] columnNames = {"Type","Value"};
    
            DefaultTableModel model = new DefaultTableModel(data, columnNames);
            JTable table = new JTable(model)
            {
                //  Determine editor to be used by row
                public TableCellEditor getCellEditor(int row, int column)
                {
                    int modelColumn = convertColumnIndexToModel( column );
    
                    if (modelColumn == 1 && row < 3)
                    {
                        JComboBox<String> comboBox1 = new JComboBox<String>( editorData.get(row));
                        return new DefaultCellEditor( comboBox1 );
                    }
                    else
                        return super.getCellEditor(row, column);
                }
            };
    
            JScrollPane scrollPane = new JScrollPane( table );
            add( scrollPane );
        }
    
        private static void createAndShowUI()
        {
            JFrame frame = new JFrame("Table Combo Box by Row");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add( new TableComboBoxByRow() );
            frame.setSize(200, 200);
            frame.setLocationByPlatform( true );
            frame.setVisible( true );
        }
    
        public static void main(String[] args)
        {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    createAndShowUI();
                }
            });
        }
    }
    

    在您的情况下,您需要修改 getCellEditor(...) 方法以根据 TableModel 的第 0 列中的数据返回 then 组合框,否则返回默认编辑器。您可能还需要覆盖 editCellAt(...) 方法,以仅根据第 0 列中的数据使单元格可编辑。

    【讨论】:

    • 嗨,.. 因为我使用了 netbeans 中的 JTable,所以我通过自定义代码添加了 getCellEditor() 方法,但我没有得到任何组合框,我还需要在 if() 块中编码什么我的意思是我需要如何调用重写的 getCellEditor 方法
    • 嗨,..我试过了,我不成功,..我需要在鼠标事件上获取 JComboBox 我不知道我需要怎么做,。请帮忙。
    • 嗨,@camickr 你给出的例子是我在谷歌周围搜索时出现的,但是这需要如何为单个单元格实现,......我的意思是如何在鼠标事件中做到这一点...... .
    • @a 你没有使用 MouseEvent。 JTable 处理 MouseEvent 并调用编辑器。我给了你工作代码,并建议你只需要对 if 语句进行简单的更改。我猜不出你是如何修改这个工作代码的,所以我无能为力。关键是您确实通过对 if 语句的简单更改来修改此代码,然后当您了解其工作原理时,您将更改应用到您的实际代码。
    猜你喜欢
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 2015-03-07
    • 1970-01-01
    • 2012-12-30
    • 1970-01-01
    相关资源
    最近更新 更多