当前位置: 首页 > 知识库问答 >
问题:

如何在鼠标单击的JTable单元格上添加JComboBox,以及基于同一JTable的其他单元格值

公孙阳文
2023-03-14

JTable:

我在NetBeans中创建了一个JTable,其中我从数据库中的某些列中获取值,如图中所示,我为TESTNAME、单位、SPECIFICRANGE列带来值,但第二列OBSERVED VALUE我为用户输入保留了空值,用户输入是这样的每当用户点击颜色前面的单元格时,他应该在第二列单元格中得到一个JComboBox,我的意思是鼠标事件上的颜色前面的单元格,对于其他单元格,我使用editCellAt()为了完成这个,我写了下面的代码,当我点击颜色前面的单元格时,我得到了JComboBox,当我点击其他单元格时,我得到了JComboBox,但我需要获得editCellAt()功能

我认为DefaultCellEditor正在为整个专栏修复,但我只需要为鼠标点击上的特定单元格使用它

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

共有1个答案

邢修明
2023-03-14

下面是一个演示如何动态返回自定义编辑器的示例:

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(…)method返回然后基于TableModel第0列中的数据的组合框,否则返回默认编辑器。您可能还需要覆盖到editCellAt(…)method,仅基于第0列中的数据使单元格可编辑。

 类似资料:
  • 我有一个JTable对象,我会在一个列上添加5个不同的JComboBox。 我已经尽力了: 但这会将相同的JComboBox添加到该列的所有单元格中。如何在同一列中添加不同的内容? 非常感谢。

  • 我想要一个有4列的jtable。一列必须是组合框。其他列是字符串。 只要找到问题:在注释语句jcb.seteditable(true)时;,如果我在comboxcell上单击一次,它就会打开这个单元格。但我不知道为什么效果更好。此外,我希望combox可编辑。 我怎么能对其他细胞有同样的行为。 再次您好,我已经更新了代码,以便使-如果我通过重写方法在单元格上单击一次,单元格可以编辑-如果我通过重写

  • 问题内容: 什么是空指针异常(),什么原因导致它们? 可以使用哪些方法/工具确定原因,以阻止异常导致程序过早终止? 问题答案: 声明引用变量(即对象)时,实际上是在创建指向对象的指针。考虑以下代码,您在其中声明基本类型的变量: 在此示例中,变量是an ,Java会为您初始化它。在第二行为其分配值时,您的值将写入所指的存储位置。 但是,当您尝试声明引用 类型时 ,会发生一些不同的事情。采取以下代码:

  • 我有一个jtable保存账本文件记录并将它们发送出去。

  • 问题内容: 我遇到的情况是,右键单击JTable时会创建一个弹出菜单。创建弹出菜单的标准方法: 现在,在注册动作之后,我无法找出右键单击哪个单元以使该弹出菜单出现。 有关如何执行此操作的任何想法? 问题答案: @MadProgrammer对getPopupLocation的建议看起来很有希望,但是我无法弄清楚如何在表和actionEvent之间获取信息。 我通过确保在右键单击该行时选择了该行来解决

  • 我在这里看了几篇文章,但找不到答案。 我已经成功地将JComboBox添加到Jtable单元格中。但是,所选项目没有被“记住”。 更具体地说: 澄清一下,信息并没有丢失。虽然组合框没有直观地显示选择了什么设备,但选择是保存的。然而,我希望用户能够直观地看到他们刚刚选择的内容,而不必再次选择行。 这使我相信我使用的自定义呈现器一定有错误 呈现器: