Search code examples
javaswingjtablejcomboboxtablecelleditor

How to display differnt values in JCombo in each row


For my project i just want to show a dialogue box with JTable. On that i want to show a JCombobox with available staff based on row index. I tried the following coding,

for(int i=0;i<n;i++)
{
Object obj[] = new Object[4];
obj[0]=2,
obj[1]=3;
obj[2]="";  //Here combo appear.
obj[3]=3;
JComboBox aa = new JComboBox();
for(int j=0;j<m;j++)
{
aa.addItem(rs.getString(1));
aa.addItem(rs.getString(2));
}
table.getcolumnModel.getcolumn(2).setcellEditor(new DefaultCellEditor(aa));
model.addRow(obj);
}

if i use this output generated. But last rows combo value is present in all previous rows combo. Those different values are not in that. its totally same. But all other text fields are correctly displayed. What should i do here. thanking you...

Note: Here

 aa.addItem(rs.getString(1));
 aa.addItem(rs.getString(2));

is just for example. Actually it will return many number of values based on id.


Solution

  • You try to set editor to each row, but it's wrong, editor can be set to whole column. Read Concepts: Editors and Renderers. Instead of that implement your logic in getTableCellEditorComponent() method of TableCellEditor.

    Simple example with different values for each row:

    import java.awt.Component;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    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;
    
    public class TestFrame extends JFrame {
    
        private DefaultComboBoxModel<String> model;
        private Map<String, List<String>> keyVal;
    
        public TestFrame() {
            init();
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            pack();
            setLocationRelativeTo(null);
            setVisible(true);
        }
    
        private void init() {
            keyVal = new HashMap<>();
            keyVal.put("1", Arrays.asList(new String[]{"a","b"}));
            keyVal.put("0", Arrays.asList(new String[]{"c","d"}));
            keyVal.put("2", Arrays.asList(new String[]{"e","f","g"}));
            JTable t = new JTable(3,3);
            t.getColumnModel().getColumn(0).setCellEditor(getEditor());
            add(new JScrollPane(t));
        }
    
        private TableCellEditor getEditor() {
    
            return new DefaultCellEditor(new JComboBox<String>(model = new DefaultComboBoxModel<String>())){
                @Override
                public Component getTableCellEditorComponent(JTable table,Object value, boolean isSelected, int row, int column) {
                    model.removeAllElements();
                    if(keyVal.containsKey(row+"")){
                        List<String> list = keyVal.get(row+"");
                        for(String s : list)
                            model.addElement(s);
                    }
                    return super.getTableCellEditorComponent(table, value, isSelected, row, column);
                }
            };
        }
    
        public static void main(String args[]) {
            new TestFrame();
        }
    
    }