如何在鼠标单击时并基于同一 JTable 中的其他单元格值在 JTable 单元格中添加 JComboBox

How to add JComboBox on JTable cell on MouseClick and based on other cells values from the Same JTable

JTable:

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

下面是一个示例,展示了如何动态地return自定义编辑器:

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(...) 方法修改为 return 然后组合框基于 TableModel 第 0 列中的数据,否则 return 默认编辑器。您可能还需要重写 editCellAt(...) 方法,以仅根据第 0 列中的数据使单元格可编辑。