1


I have been trying to send some data from an Editable JComboBox to a JXTable. The code for it goes like this :

private void selectTestActionPerformed(java.awt.event.ActionEvent evt) {                                           
    JTextField editorComponent = (JTextField) testName_cb.getEditor().getEditorComponent();
    System.out.println(editorComponent.getText());
    String data = editorComponent.getText();
    Object row = data; /* String to Object casting */
    DefaultTableModel model = (DefaultTableModel) testsSelected_table.getModel();
    model.addRow(row); /* Error : Cast row to Object or Vector */
}

But the last line of the method model.addRow(row); says Cast row to Object or Vector, which it already is.
I may be missing some conceptual or logical part as a beginner. So thought of posting a question here. Can anybody point out my mistake? I would gratefully accept any suggestion(s).

Thanks!!!

mustangDC
  • 945
  • 1
  • 12
  • 33
  • 1
    It should be an `Object[]`. You can do `row = new Object[] { data };` assuming all you want the row to have is one column or data. Otherwise you can use `model.setValueAt(value, row, col)` to set a single value, if that's what you're really trying to do – Paul Samsotha Jun 16 '15 at 16:27
  • See the API for [`DefaultTableModel`](http://docs.oracle.com/javase/7/docs/api/javax/swing/table/DefaultTableModel.html) – Paul Samsotha Jun 16 '15 at 16:30
  • What row show should be declared as ? If I use `row = new Object[] { data };` – mustangDC Jun 16 '15 at 16:42
  • @peeskillet. Holy Java :), there is an ocean to learn. Thanks!!!. I would like to accept it, if u like to post it as an answer – mustangDC Jun 16 '15 at 16:47

1 Answers1

0

It should be an Object[]. You can do Object[] row = new Object[] { data }; assuming all you want the row to have is one column or data.

Otherwise you can use model.setValueAt(value, row, col) to set a single value, if that's what you're really trying to do.

For general references, see

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720