Hi All! I have a table that there is a combo box inside the table. For example, the combo box consist of item codes. And then, whenever user choose item from combo box, the next column inside the table will show name of the item. Here is my example coding:

sql = "SELECT desc_bar FROM inv_item WHERE kode_bar = '" + cboBarang.getSelectedItem().toString() + "'";
stmt = Koneksi.getConnection().createStatement();
rs = stmt.executeQuery(sql);
if (rs.last()){
     tabel.setValueAt(rs.getString(1), 0, 1);
} else {
     tabel.setValueAt("", 0, 1);
}

My question is how if there is many rows in a table. I tried to use this, tabel.setValueAt(rs.getString(1), tabel.getSelectedRow(), 1).
But it showed me an error, which is: index out of bound. So, how to do this ( I hope you'll understand what I'm trying to do here because my english isn't good enough :$)? Can you help me? Thx before. :)

I already searched anywhere but still couldn't find the solution. Or maybe the right event for what I want is not actionPerformed?
Is there anyone can help me? I'm still stuck here. Thanx

I am not sure how the whole thing is supposed to work, but it being Java, indexes are 0 relative.
try this

if (rs.last()==false){
     tabel.setValueAt(rs.getString(0), 0, 1);
} else {
     tabel.setValueAt("", 0, 1);
}

Close the thread if this helps

Hi padtes, thx for your reply! :)
From I know, when you getString from resultSet and using an index, it begins from 1 (also I already tried from 0 and it got an error).
I tried this code in actionPerformed comboBox:

if (rs.last()==false){
     tabel.setValueAt(rs.getString(1), [B]tabel.getSelectedRow()[/B], 1)
// the problem is from the bold letters
} else {
     tabel.setValueAt("", tabel.getSelectedRow(), 1);
}

But it showed an error: index out of bound. I think it's because before I click the table, actionPerformed in comboBox run first. So, any idea how to do it? Thx.

Hi padtes, thx for your reply! :)
From I know, when you getString from resultSet and using an index, it begins from 1 (also I already tried from 0 and it got an error).
I tried this code in actionPerformed comboBox:

if (rs.last()==false){
     tabel.setValueAt(rs.getString(1), [B]tabel.getSelectedRow()[/B], 1)
// the problem is from the bold letters
} else {
     tabel.setValueAt("", tabel.getSelectedRow(), 1);
}

But it showed an error: index out of bound. I think it's because before I click the table, actionPerformed in comboBox run first. So, any idea how to do it? Thx.

Before, I set combobox as editor in JTable like this code below. But it can't work like I want just like I posted the problem in my first post above.

DefaultTableModel model;
    TableColumn comboboxColumn;
    final JComboBox cboBarang = new JComboBox();


tabel.setModel(model);
            comboboxColumn = tabel.getColumnModel().getColumn(0);
            isiCboBarang(cboBarang);
           comboboxColumn.setCellEditor(new DefaultCellEditor(cboBarang));

So, now I tried to use tableCellEditor, and this is my code:

tabel.setModel(model);
            comboboxColumn = tabel.getColumnModel().getColumn(0);
            comboboxColumn.setCellEditor(new MyComboBoxEditor());


public class MyComboBoxEditor extends JComboBox implements TableCellEditor, ItemListener {
        Vector editorListeners;
        public MyComboBoxEditor() {
           super();
           editorListeners = new Vector();
           System.out.println(listItem);
           for(int i =0; i < listItem.size(); i++){
                addItem(listItem.get(i));
           }
           addItemListener(this);
       }
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
           // Select the current value
           setSelectedItem(value);
           return this;
        }

        public Object getCellEditorValue() {
            return this.getSelectedItem();
        }

        public boolean isCellEditable(EventObject anEvent) {
            return true;
        }

        public boolean shouldSelectCell(EventObject anEvent) {
            return true;
        }

        public boolean stopCellEditing() {
            return true;
        }

        public void cancelCellEditing() {
            
        }

        public void addCellEditorListener(CellEditorListener l) {
            editorListeners.add(l);
        }

        public void removeCellEditorListener(CellEditorListener l) {
            editorListeners.remove(l);
        }

        public void itemStateChanged(ItemEvent e) {
          if(e.getStateChange()==ItemEvent.DESELECTED)
          {
            Statement stmt;
            ResultSet rs;
            try {
                //generate code
                sql = "SELECT desc_bar FROM inv_item WHERE kode_bar = '" +
                        this.getSelectedItem() + "'";
                stmt = Koneksi.getConnection().createStatement();
                rs = stmt.executeQuery(sql);

                if (rs.last()){
                    tabel.setValueAt(rs.getString(1), tabel.getSelectedRow(), 1);
                } else {
                    tabel.setValueAt("", tabel.getSelectedRow(), 1);
                }
                rs.close();
                stmt.close();
            } catch (SQLException ex) {
                Logger.getLogger(TrPOEntry.class.getName()).log(Level.SEVERE, null, ex);
            }
          }
        }
   }

With this method, it's running but not properly. For example, combobox's data are: "A", "B", "C". When I choose "A" (col 1, row 1), the second column in the first row (col 2, row1) will display "1". After I choose combobox in first row, next I'm going to combobox in second row (col 1, row 2). But apparently, the second column in first row (col 2, row 1) change the content according to the value in combobox second row (col 1, row 2).

So, I guess when I move on to different row (but still in the jcombobox column), combobox still in editing state. Because it didn't happen when I first click column 1 (combobox column), then I click different column (besides combobox column). So, how can I fix this and it stop editing after I move on to the next row? Please help me.. :confused:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.