【问题标题】:A JComboBox in a specific cell in JTableJTable 中特定单元格中的 JComboBox
【发布时间】:2021-06-20 14:48:11
【问题描述】:

我想知道如何在 JTable 的特定单元格中设置 JComboBox。 我见过有人使用TableColumn setCellEditor(new DefaultCellEditor(comboBox))

但这是针对整个列的,我想要一个特定的单元格。 所以也许我应该做一个适合我需要的自定义TableCellEditor,但我对如何做有点迷茫......

这样做的目的是管理参数过滤器。有两种过滤器:

  1. 比较两个值,例如:气球数 > 5
  2. 会说一个值在一个值范围内,例如:参数名称在 {"one", "two", "three", "seven"} 内。

我的 JTable 的屏幕截图:

正如我们在图片中看到的,当有“比较器”“在其中”时,我们需要cell[0][2] 中的JComboBox 来选择一组完整字段中的范围值。 而cell[1][2] 不需要JComboBox,而只是一个可编辑的单元格。

希望我已经说清楚了,感谢您的帮助。

编辑: 我能够显示一个 JComboBox 只是为了实现,我无法在其上选择多个值。所以现在我试图显示一个 JList 而不是一个 ComboBox。 但是当我点击单元格时,没有显示JList,我不知道为什么。 这是我的代码:

JTable tableParametersFilter = new JTable(modelParametersFilter){
    //  Determine editor to be used by row
    public TableCellEditor getCellEditor(int row, int column)
    {
        int modelColumn = convertColumnIndexToModel( column );
        int modelRow = convertRowIndexToModel( row );
        Parameter_Filter pf =  view.listParameter_Filter.get(modelRow);
        if(modelColumn == 2 && pf instanceof Parameter_Filter_To_List_Of_Fields) {
            Parameter_Filter_To_List_Of_Fields pftlof = (Parameter_Filter_To_List_Of_Fields)pf;
            
            JList<String> list = new JList<String>(pftlof.list_of_fields_total_names);
            list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION );
            list.setLayoutOrientation(JList.VERTICAL_WRAP);
            list.setVisibleRowCount(-1);
            
            return new TableCellEditor() {
                @Override
                public boolean stopCellEditing() {
                    return false;
                }
                @Override
                public boolean shouldSelectCell(EventObject anEvent) {
                    return false;
                }
                @Override
                public void removeCellEditorListener(CellEditorListener l) {
                }
                @Override
                public boolean isCellEditable(EventObject anEvent) {
                    return true;
                }
                @Override
                public Object getCellEditorValue() {
                    return list.getSelectedValuesList().toString();
                }
                @Override
                public void cancelCellEditing() {
                }
                @Override
                public void addCellEditorListener(CellEditorListener l) {
                }
                @Override
                public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
                    return list;
                }
            };
        }
        return super.getCellEditor(row, column);
    }
};

有什么建议吗?

【问题讨论】:

  • 您的问题令人困惑,您可能希望详细说明您当前的问题和愿望。为什么您希望组合框成为单个单元格而不是列的所有单元格的编辑器?这将是哪个单元格,列中的哪些单元格不会使用此编辑器,为什么?我不确定我是否理解这个用例。
  • 也许您对这个编辑器的工作方式感到困惑。是的,这样做会为整个列设置编辑器,但是您一次只能看到一个组合框,并且只有在您编辑单元格时才能看到。你知道这一点,对吧?
  • 是的,我知道我们只有在编辑单元格时才能看到 ComboBox。我已将我的帖子编辑得更具体。
  • 不知道如何使用组合框来选择一系列值。但是您可以通过覆盖表格的getCellEditor(...) 方法为单元格提供特定的编辑器。查看:stackoverflow.com/questions/4211452/… 为例。
  • 我刚刚编辑了我的问题,添加了我的解决方案尝试。

标签: java swing jtable jcombobox jlist


【解决方案1】:

我已经解决了我的问题。 我无法在Jtable 的单元格上添加多项选择JComboBox 或可显示的JList。 相反,我使用了显示JListJOptionPane。 代码如下:

tableParametersFilter.addMouseListener(new MouseAdapter() {
    public void mouseClicked(MouseEvent e) {
        JTable target = (JTable)e.getSource();
        int row = target.getSelectedRow();
        int column = target.getSelectedColumn();
        if(column == 2){
            Parameter_Filter pf = view.listParameter_Filter.get(row);
            if(pf instanceof Parameter_Filter_To_List_Of_Fields) {
                Parameter_Filter_To_List_Of_Fields pftlof = (Parameter_Filter_To_List_Of_Fields) pf;
                JList<String> jlist = new JList<String>(pftlof.list_of_fields_total_names);
                String StringOfIntArray = (String) tableParametersFilter.getValueAt( row, 2);
                int[] list_parameter_id = Statique.StringOfIntArrayToIntegerArray(StringOfIntArray);
                if(list_parameter_id.length < jlist.getModel().getSize()) {
                    int[] list_places = pftlof.getPlaceOfParameters(list_parameter_id);
                    for(int i = 0; i < list_places.length; i++) {
                        jlist.setSelectedIndices(list_places);
                    }
                }
                
                JScrollPane scrollPane = new JScrollPane(jlist);
                scrollPane.setPreferredSize( new Dimension( 500, 500 ) );
                JOptionPane.showMessageDialog( 
                        null, scrollPane, "Multi-Select Example", JOptionPane.PLAIN_MESSAGE);
                int[] SelectedIndices = jlist.getSelectedIndices();
                Integer[] listParametersId = new Integer[SelectedIndices.length];
                for(int i = 0; i < SelectedIndices.length; i++) {
                    int id = pftlof.list_of_fields_Total[SelectedIndices[i]].id;
                    try {
                        Parameter p = Parameter.getParameter(
                                id, 
                                Parameter_Filter_To_List_Of_Fields.getTotal_Parameter_In_Parameter_Filter_To_List_Of_Fields());
                        listParametersId[i] = p.id;
                    } catch (NoSuchFieldException e1) {
                        e1.printStackTrace();
                    }
                }
                System.out.println(Arrays.toString(listParametersId));
                tableParametersFilter.setValueAt(Arrays.toString(listParametersId), row, 2);
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2012-03-05
    • 2011-03-16
    • 2016-07-01
    • 2018-12-02
    • 2015-03-07
    • 1970-01-01
    • 2011-09-17
    • 1970-01-01
    • 2020-01-02
    相关资源
    最近更新 更多