【问题标题】:Specific comboBox on a specific column number in a table [closed]表中特定列号上的特定组合框[关闭]
【发布时间】:2016-09-22 15:36:11
【问题描述】:

我有一个大约 17 列的 JTable。对于其中一些列,我想要组合框,对于其他列,我不想要。一些代码:

public final JTable table;

void setCellEditors(){

    setBooleanCellEditor (table); // comboBox for boolean values
    setIntCellEditor (table); // comboBox for int values
    setTypeCellEditor (table); 
    setAnotherTypeCellEditor (table);
    // .. and so on, for all types I need comboboxes
}

大多数类型的 cellEditor 函数如下所示:

private void setTypeCellEditor (JTable jt) {
    DefaultCellEditor dce = new DefaultCellEditor (Type.buildComboBox ());
    jt.setDefaultEditor (Type.class, dce);
}

这很好用,因为该类型对于该表来说是唯一的,换句话说,我只有一列是布尔类型,一列是 Int,一列是 AnotherType 等。现在的问题是两列都有字符串值,但需要不同的组合框。意思是,上面的代码不起作用,因为它们都是 String.class。

当然,我尝试通过说“在第 10 列我想要这个 ComboBox”来解决这个问题:

private void setYetAnotherTypeCellEditor (JTable jt) {
    DefaultCellEditor dce = new DefaultCellEditor (YetAnotherType.buildComboBox ());
    if (jt.getColumnModel ().getColumnCount() > 0) {
        jt.getColumnModel ().getColumn (9).setCellEditor (dce);
    }
}

但是,这似乎不起作用,我不知道为什么。我也试过this guide,但这没有帮助。基本上,我认为 setCellEditor 出于某种原因没有设置单元格编辑器。

很难更具体,因为这背后有很多代码。

【问题讨论】:

标签: java swing jtable jcombobox tablecelleditor


【解决方案1】:

很难更具体,因为这背后有很多代码。

Using a Combo Box as an Editor 说明按列设置单元格编辑器。如下所示的Minimal, Complete, and Verifiable example 将允许您单独研究问题。

import java.awt.EventQueue;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;

/**
 * @see https://stackoverflow.com/a/37435196/230513
 */
public class Test {

    private void display() {
        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JTable table = new JTable(1, 2);
        table.getColumnModel().getColumn(0).setCellEditor(Type1.buildComboBox());
        table.getColumnModel().getColumn(1).setCellEditor(Type2.buildComboBox());
        f.add(new JScrollPane(table));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private static class Type1 {

        private static TableCellEditor buildComboBox() {
            return new DefaultCellEditor(new JComboBox(
                new DefaultComboBoxModel<>(new String[]{"A", "B", "C"})));
        }
    }

    private static class Type2 {

        private static TableCellEditor buildComboBox() {
            return new DefaultCellEditor(new JComboBox(
                new DefaultComboBoxModel<>(new String[]{"X", "Y", "Z"})));
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Test()::display);
    }
}

【讨论】:

    【解决方案2】:

    我看不到您在哪里初始化名为tableJTable,我也看不到它与传递给setYetAnotherTypeCellEditor()JTable 与名为jt 的参数相同。我猜jt 不是null;但没有任何列,jt.getColumnModel().getColumnCount() &gt; 0 可能是false。试试

    private void setYetAnotherTypeCellEditor (JTable jt) {
        DefaultCellEditor dce = new DefaultCellEditor (YetAnotherType.buildComboBox());
        jt.getColumnModel ().getColumn (9).setCellEditor (dce);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-22
      • 1970-01-01
      • 1970-01-01
      • 2019-11-17
      相关资源
      最近更新 更多