1

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:

enter image description here

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?

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
shlomite
  • 11
  • 2
  • Your question is confusing and you may wish to tell the details of your current problem and desires. Why would you want the combobox to be the editor for just a single cell and not all the cells of the column? Which cell would this be and which cells in the column would not use this editor and why? I'm not sure that I'd understand the use-case for this. – Hovercraft Full Of Eels Mar 23 '21 at 21:51
  • Maybe you're confused at how this editor works. Yes, doing this will set the editor for the entire column, but you only see the combobox one cell at a time, and only when you are editing the cell. You do know this, right? – Hovercraft Full Of Eels Mar 23 '21 at 21:52
  • Yes I know that we see the ComboBox only when editing the cell. I have edited my post to be more specific. – shlomite Mar 23 '21 at 22:01
  • Don't know how using a combo box will allow you to choose a range of values. But you can provide a specific editor for a cell by overriding the `getCellEditor(...)` method of the table. Check out: https://stackoverflow.com/questions/4211452/how-to-add-unique-jcomboboxes-to-a-column-in-a-jtable-java/4211552#4211552 for an example. – camickr Mar 24 '21 at 01:09
  • I just edited my question, adding my attempts of resolutions. – shlomite Mar 24 '21 at 16:24

1 Answers1

0

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);
            }
        }
    }
}
shlomite
  • 11
  • 2