-1

I've a JComboBox in a JTable, but when I insert a new row with new values for the JComboBox, all the JComboBoxs of all rows have that value. How do I have different values for JComboBox for each row in the 3rd column? I've tried this code:

cc = new JComboBox();
cc.addItem(jComboBox5.getSelectedItem()+"/"+jComboBox6.getSelectedItem()+"/"+jComboBox7.getSelectedItem()+" "+jComboBox1.getSelectedItem()+"."+jComboBox2.getSelectedItem());                         
DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
renderer.setToolTipText("CLICCA PER LE DATE");
Object[][] stringa = {{jTextField1.getText(), jTextField2.getText(),jTextField3.getText()}};
model.addRow(stringa[0]);
      int n=model.getRowCount(); 
             for(int i=0; i<n;i++){
             if(i==n-1){
                //HERE HOW CAN I CHOOSE THE ROW WHERE PUT THIS JCOMBOBOX? BECAUSE WITH THIS CODE I CHANGE THE VALUES OF ALL COMBOBOXES WITH NEW VALUES
                 jTable1.getColumnModel().getColumn(3).setCellEditor(new DefaultCellEditor(cc));
              jTable1.getColumnModel().getColumn(3).setCellRenderer(renderer);}
             }

However I'm sorry for the shout.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 2
    NO NEED TO SHOUT. But regarding your question, the details are in the code. The combo box and its contents will be defined in your table column's cell editor, something you probably need to show us. It appears that the contents of the combo box in the editor will depend on the row, yes? and if so, the editor will have to change the data held by the combo box depending on the row, but luckily that information is available in the cell editor's `getTableCellEditorComponent(...)` method. You just need to use it. – Hovercraft Full Of Eels Apr 01 '13 at 21:49
  • 1
    "How do I have different values for JComboBox for each row in the 3rd column?' Answered yesterday when you posted a similar question on this topic. – camickr Apr 02 '13 at 01:21

1 Answers1

2

As per my comment:

  • You would need to set the data in the JComboBox in your TableCellEditor, the one added to the appropriate TableColumn.
  • You haven't told us what criteria you would use to help you decide on how you would change the objects displayed in the combo box. Is it based on the table row number?, or perhaps based on the data held in the row?
  • The row number and data can be obtained via the getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) method. Yours would return a JComboBox whose items have been filled depending on your criteria, whatever that currently is.
  • Please avoid using ALL-UPPER CASE in your question or its title as that is equivalent to SHOUTING and is not looked kindly on.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • +1 Examples [here](http://stackoverflow.com/a/7356518/230513) and [here](http://stackoverflow.com/a/3256602/230513). – trashgod Apr 02 '13 at 00:55
  • @FrancescoStranieri: You're not overriding the key method of your TableCellEditor, and changing the model will have no effect on the editor. For better help, consider creating and posting an [sscce](http://sscce.org). – Hovercraft Full Of Eels Apr 02 '13 at 10:59