【问题标题】:Action event for a combobox inserted into a JTable Cell in java swing在 java swing 中插入 JTable 单元的组合框的动作事件
【发布时间】:2016-04-24 04:23:11
【问题描述】:

我正在向表格的第三列添加一个组合框,所以..每次添加新行时,它都会创建一个新的组合框并添加来自 vector1 中的项目

TableColumn ProfileCol = Table.getColumnModel().getColumn(3);
ProfileCol.setCellEditor(new tableList(vector1));

这里的tableList是一个下面提到的扩展DefaultcellEditor的类,它的构造方法是

public tableList(java.util.Vector v) {
    super(new JComboBox(v));

我的问题是如果我写一个动作,甚至像

Table.addMouseListener(new java.awt.event.MouseAdapter() {
    @Override    
    public void mouseClicked(java.awt.event.MouseEvent evt) {
        int row = Table.rowAtPoint(evt.getPoint());
        int col = Table.columnAtPoint(evt.getPoint());
        if (row >= 0 && col ==3) {

        }
    }
});

它没有被触发..

  1. 我需要一个代码,该代码将为组合框中的每个项目选择触发
  2. 我需要一个允许我动态更新插入到表格中的组合框内容的代码

请帮忙。

【问题讨论】:

  • 为什么.....,有什么问题吗,问题出在哪里,你不需要代码...,你不需要MouseListener,尽快获得更好的帮助发布 SSCCE / MCVE / MCTRE,简短,可运行,可在局部变量中使用 JTable / XxxTableModel 的硬编码值进行编译,然后在这里阅读官方 Oracle 教程 - 如何使用表,部分 ComboBox 作为编辑器,用于工作代码示例,提示覆盖模型中的 setValueAt()
  • 您必须编辑您的问题以获得1)I need a code that will fire for every selection of item in the combo box ??? 的潜在答案-->此事件在JTable 或2)I need a code that will allow me to dynamically update the contents of the combobox that is inserted in the table 中结束的内容、地点、方式--> 你的意思是可编辑的JComboBox或
  • 如果我单击除第三列以外的任何单元格,我编写的侦听器代码能够触发,我在其中插入了组合框..我的实际目的是..如果我选择存在于组合框(插入表的一列中)我必须从数据库中获取一些与之相关的其他数据......所以......我需要转到一个函数,如果我在组合框中选择一个项目,它将被触发。 .我希望你现在明白了
  • 如果我点击第三列以外的任何单元格,我编写的侦听器代码能够触发 == ListSelectionListener
  • Table.addMouseListener(new java.awt.event.MouseAdapter() { @Override public void mouseClicked(java.awt.event.MouseEvent evt) { int row = Table.rowAtPoint(evt.getPoint()); int col = Table.columnAtPoint(evt.getPoint()); if (row >= 0 && col >=0) { } } });

标签: java swing combobox jtable netbeans-platform


【解决方案1】:

每次我在表格中添加一个新行时,都会生成一个新的组合框..那么我怎样才能为它们编写一个动作监听器

您不应该使用 ActionListener。

您有三个选择:

  1. 重写 TableModel 的 setValue(...) 方法来进行处理
  2. 将 TableModelListener 添加到 TableModel。然后在生成 TableModelEvent 时进行处理。
  3. 在您的桌子上使用Table Cell Listener 来处理处理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-15
    • 1970-01-01
    相关资源
    最近更新 更多