JComboBox 作为自定义 TableCellEditor

2023-12-06

我有一张桌子。该表上的更改会更新数据库。该表中的一列由 JComboBox 编辑。单击该列中的任何单元格都会触发 tableChanged 事件。但是,在选择 JComboBox 的项目后需要触发它。如何让 tableChanged 在选择后发生?

public class JIDCellEditor extends AbstractCellEditor implements TableCellEditor {

    JComboBox jComboBox;

    @Override
    public Object getCellEditorValue() {
        return jComboBox.getSelectedItem();
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        Vector vector = new Vector();
        vector.add(0);
        for (int i = 0; i < table.getRowCount(); i++) {
            if (!vector.contains(table.getValueAt(i, 0)) && table.getValueAt(i, 3).toString().equals("Female")) {
                vector.add(table.getValueAt(i, 0));
            }
        }
        vector.remove(table.getValueAt(row, 0));
        jComboBox = new JComboBox(vector);
        jComboBox.setSelectedItem(value);
        return jComboBox;
    }
}

我强烈推荐使用SwingX其中有一个组合框单元格编辑器成分。它本质上是 Sun 的 Swing 组件应有功能的孵化器。我不知道该项目是否仍在积极开发中,但它已经成熟,并且我已经在很多项目中使用了它。

如果出于某种原因,您不能或不想使用外部库,这里是他们的代码(修改了部分以删除自定义 SwingX 功能),注释完好无损:

注意:该库是 GPL 代码。

/*
 * $Id: ComboBoxCellEditor.java 3738 2010-07-27 13:56:28Z bierhance $
 * 
 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, California 95054, U.S.A. All rights reserved.
 * 
 * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.util.EventObject;

import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;

/**
 * <p>
 * This is a cell editor that can be used when a combo box (that has been set up for automatic completion) is to be used in a JTable. The
 * {@link javax.swing.DefaultCellEditor DefaultCellEditor} won't work in this case, because each time an item gets selected it stops cell
 * editing and hides the combo box.
 * </p>
 * <p>
 * Usage example:
 * </p>
 * <p>
 * 
     * <pre>
 * <code>
 * JTable table = ...;
 * JComboBox comboBox = ...;
 * ...
 * TableColumn column = table.getColumnModel().getColumn(0);
 * column.setCellEditor(new ComboBoxCellEditor(comboBox));
 * </code>
 * </pre>
 * 
 * </p>
 */
public class ComboBoxCellEditor extends DefaultCellEditor {

    /**
     * Creates a new ComboBoxCellEditor.
     * 
     * @param comboBox the comboBox that should be used as the cell editor.
     */
    public ComboBoxCellEditor(final JComboBox comboBox) {
        super(comboBox);

        comboBox.removeActionListener(this.delegate);

        this.delegate = new EditorDelegate() {
            @Override
            public void setValue(final Object value) {
                comboBox.setSelectedItem(value);
            }

            @Override
            public Object getCellEditorValue() {
                return comboBox.getSelectedItem();
            }

            @Override
            public boolean shouldSelectCell(final EventObject anEvent) {
                if (anEvent instanceof MouseEvent) {
                    final MouseEvent e = (MouseEvent) anEvent;
                    return e.getID() != MouseEvent.MOUSE_DRAGGED;
                }
                return true;
            }

            @Override
            public boolean stopCellEditing() {
                if (comboBox.isEditable()) {
                    // Commit edited value.
                    comboBox.actionPerformed(new ActionEvent(ComboBoxCellEditor.this, 0, ""));
                }
                return super.stopCellEditing();
            }

            @Override
            public void actionPerformed(final ActionEvent e) {
                ComboBoxCellEditor.this.stopCellEditing();
            }
        };
        comboBox.addActionListener(this.delegate);
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JComboBox 作为自定义 TableCellEditor 的相关文章

随机推荐

  • std regex_search 仅匹配当前行

    我使用各种正则表达式逐行解析 C 源文件 首先我读取字符串中文件的所有内容 ifstream file stream commented cpp ifstream binary std string txt std istreambuf i
  • 如何将参数注入 TestCafé 测试?

    设想 我使用 API 运行用代码封装的 TestCaf 我有一个想要参数化的测试 使用不同的动态值进行测试 Problem Testcaf 不支持向测试发送参数 有没有办法注入值 您可以使用进程 env将参数从运行程序脚本传递给 TestC
  • 如何从 Guava MultiMap 中获取每个条目及其关联的相应值?

    我正在读取一个巨大的 csv 文件 其中包含重复的条目 我能够将整个 csv 文件读入Multimap 我还能够获取具有重复值的键集并将它们写入文件 我想获取与每个键关联的值并将其写入文件 但无法这样做 我似乎找不到任何可能对我有帮助的选项
  • 如何在 Rails 4 中将 PDF 转换为 Excel 或 CSV

    我已经搜索了很多 除非在这里问这个问题 否则我别无选择 你们知道有一个在线转换器有 API 或 Gem s 可以将 PDF 转换为 Excel 或 CSV 文件吗 我也不确定这里是否是问这个问题的最佳地点 我的应用程序是在 Rails 4
  • 有没有办法使用命令行 cURL 跟踪重定向?

    我知道在 php 脚本中 curl setopt ch CURLOPT FOLLOWLOCATION true 将遵循重定向 有没有办法使用命令行 cURL 跟踪重定向 使用位置标头标志 curl L
  • 为什么 re.sub('.*?', '-', 'abc') 返回 '-a-b-c-' 而不是 '--------'?

    这是python2 7的结果 gt gt gt re sub abc a b c 我以为的结果should如下 gt gt gt re sub abc 但事实并非如此 为什么 据我所知 对这种行为的最好解释来自regexPyPI 包 其目的
  • JSON.Stringify 在 Scripting.Dictionary 对象上失败

    我正在开发一个 ASP 经典项目 在该项目中我实现了 JScript JSON 类here 它能够与 VBScript 和 JScript 互操作 并且几乎与以下位置提供的代码完全相同 json org 我的团队经理要求我在这个项目中使用
  • 如何交错两个数组列表?

    我正在尝试开发一个程序 通过将一副牌分成两部分然后将它们交错来洗牌 班级甲板代表一副 52 张牌 有两种方法 牌组 int n and 抽卡卡 牌组 int n 是构造函数 该参数告诉我们应该洗牌多少轮 在每轮洗牌中 整副牌首先被分成两个子
  • 用于 dotnet 的 Google API v3;使用带有 API 密钥的日历

    我正在尝试使用 v3 API http code google com p google api dotnet client 读取我自己的 Google 日历 我最终想要的是通过自定义控件在我的网站上显示来自 Google 日历的我自己的日
  • 是否可以删除 .NET 中未使用的代码/程序集?

    我的应用程序中有一个控件库 对于我正在开发的应用程序类型来说有点大 该库超过 2Mb 我几乎不使用它的功能 我想说我使用了它所有功能的 5 到 10 有没有办法从库中删除我的应用程序从不使用的代码 P S 该库不是我开发的 也不是开源的 不
  • 如何在Linux中创建给定大小的文件?

    出于测试目的 我必须生成一定大小的文件 以测试上传限制 在 Linux 上创建特定大小的文件的命令是什么 对于小文件 dd if dev zero of upload test bs file size count 1 Where file
  • Kotlin 中 KProperty1 的通用扩展

    我有以下代码 import kotlin reflect KProperty1 infix fun
  • Webpack 文件加载器输出 [对象模块]

    我正在使用 webpackHtmlWebpackPlugin html loader and file loader 我有一个简单的项目结构 其中不使用任何框架 而仅使用打字稿 因此 我直接将 HTML 代码写入index html 我还使
  • XAML 中的 WPF 文本块绑定

    我正在更新一些现有的 WPF 代码 我的应用程序有许多定义如下的文本块
  • 用于复杂 ID 的 CSS 通配符

    如果我有一个 css 选择器 例如 subtab 1 subtab 2 etc 我可以这样制作通配符选择器div id subtab 但我不知道如何为选择器制作通配符 例如 subtab 1 sub1 subtab 1 sub2 subta
  • 如何在 Silverlight 3(或 4)中从 URL 播放 MP4(H.264 视频)? [关闭]

    Closed 这个问题需要多问focused 目前不接受答案 所以我有一些网址MP4文件 我想开发一个简单的Silverlight应用程序来玩它 怎么做 最好有示例代码 Ole Jak 以下是建议的分步步骤 步骤 1 使用 Visual S
  • 在 Ubuntu 上使用 GLFW 设置 OpenGL NetBeans 项目

    我正在尝试在 Ubuntu 上设置 OpenGL 开发环境 我安装了包括 GLFW 在内的所有库 因为我不想使用 GLUT GLEW 库也已安装 我正在尝试将其全部设置在 NetBeans 中 我从未使用过它之前和现在我得到 对 glfwI
  • 使用 bash 我需要删除文件名中文件扩展名之前的尾随空格

    我有数百个文件 它们看起来类似于 QDN34 Unit5 mark up Judy pdf QDN34 Unit7 mark up Judy pdf file with two character ext ai file with dot
  • 如何扩展 float3 或任何其他内置类型以符合 Codable 协议?

    在尝试使用基本 JSONEncoder 序列化 float3 对象数组时 发现 float3 不符合 Codable 协议 因此无法完成此操作 我尝试按照中的建议编写基本扩展编码和解码自定义类型如下所示 但是错误 self used bef
  • JComboBox 作为自定义 TableCellEditor

    我有一张桌子 该表上的更改会更新数据库 该表中的一列由 JComboBox 编辑 单击该列中的任何单元格都会触发 tableChanged 事件 但是 在选择 JComboBox 的项目后需要触发它 如何让 tableChanged 在选择