简体   繁体   中英

A JComboBox in a specific cell in JTable

I would like to know how to set up a JComboBox in a particular cell in a JTable. I have seen people using TableColumn setCellEditor(new DefaultCellEditor(comboBox)) .

But this is for an entire column, I would like a specific cell. So maybe I should do a custom TableCellEditor that would fit my needs, but I am a little lost on how to do it...

The goal of this is to manage filters on parameters. There are two kinds of filters:

  1. The one that compares two values, for instance: number of balloons > 5
  2. The one that will say is a value is inside a range of value, for instance: parameter name is inside {"one", "two", "three", "seven"}.

screenshot of my JTable:

在此处输入图像描述

As we can see in the picture, when there is the "comparator" "is among", we would need a JComboBox in cell[0][2] to choose the values of the range within a complete set of fields. While cell[1][2] does not need a JComboBox , but just an editable cell.

I hope I have been clear and thank you for your help.

EDIT: I was able to display a JComboBox only to realize, I couldn't select multiple values on it. So now I am trying to display a JList instead of a ComboBox. But when I click on the cell, the JList is not displayed, I don't know why. Here is my code:

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

Any suggestions?

I have solved my problem. I have not been able to add multiple choice JComboBox , or a displayable JList on the Cell of the Jtable . Instead, I have used a JOptionPane that displayed a JList . Here's the code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM