0

I want to fill all the fields when I click on a table row. If I click a row in table it fills all the JTextField & JComboBox components, but not the JDateChooser.

How could I fill date chooser field when I select a row in table?

This is the code:

 private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {                                     
    int SelectedRowIndex=jTable1.getSelectedRow();

    jComboBox2.addItem((String)jTable1.getValueAt(SelectedRowIndex, 0));
    jComboBox2.setSelectedItem((String)jTable1.getValueAt(SelectedRowIndex, 0));
    jComboBox3.addItem((String)jTable1.getValueAt(SelectedRowIndex, 1));
    jComboBox3.setSelectedItem((String)jTable1.getValueAt(SelectedRowIndex, 1));
    jTextField2.setText((String)jTable1.getValueAt(SelectedRowIndex, 2));
    jComboBox5.addItem((String)jTable1.getValueAt(SelectedRowIndex, 3));
    jComboBox5.setSelectedItem((String)jTable1.getValueAt(SelectedRowIndex, 3));
    jComboBox6.addItem((String)jTable1.getValueAt(SelectedRowIndex, 4));

    jComboBox6.setSelectedItem((String)jTable1.getValueAt(SelectedRowIndex, 4));
    jTextField6.setText((String)jTable1.getValueAt(SelectedRowIndex, 5));
    jComboBox14.addItem((String)jTable1.getValueAt(SelectedRowIndex, 6));
    jComboBox14.setSelectedItem((String)jTable1.getValueAt(SelectedRowIndex, 6));
    jTextField5.setText((String)jTable1.getValueAt(SelectedRowIndex, 7));
    jComboBox13.addItem((String)jTable1.getValueAt(SelectedRowIndex, 8));
    jComboBox13.setSelectedItem((String)jTable1.getValueAt(SelectedRowIndex, 8));
    jComboBox4.addItem((String)jTable1.getValueAt(SelectedRowIndex, 9));
    jComboBox4.setSelectedItem((String)jTable1.getValueAt(SelectedRowIndex, 9));
    jTextField3.setText((String)jTable1.getValueAt(SelectedRowIndex, 10));
    java.sql.Date date1 = new java.sql.Date(jDateChooser2.getDate().getTime());
    date1.setDate((int)jTable1.getValueAt(SelectedRowIndex, 11));
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Yusuf Mohamed
  • 65
  • 1
  • 7
  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson Jun 16 '17 at 20:52
  • 1
    BTW - the last two lines of that code seem completely illogical. Firstly while most of the code is getting values from the table, those two lines initially get the date from the date chooser. Secondly that date is **then** set to the value from the table, then immediately discarded! Probably best to create an SQL date from the value in the table, then **set** that date in the date chooser. – Andrew Thompson Jun 16 '17 at 20:56
  • 1
    I don't know why you would be casting the date value to an int. You should be storing a Date Object in the TableModel. – camickr Jun 17 '17 at 00:27
  • 1
    JTable model with a Date column : https://stackoverflow.com/a/11503696/3992939 – c0der Jun 17 '17 at 04:26
  • 1
    Start with this [example](https://stackoverflow.com/a/14880675/230513) to prepare your [mcve]. – trashgod Jun 17 '17 at 08:05

0 Answers0