How to Include date in JTable?

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 3874 Views - Last Post: 11 March 2012 - 03:09 PM Rate Topic: -----

#1 RDane  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 08-March 12

How to Include date in JTable?

Posted 11 March 2012 - 10:31 AM

I cant get the date from my sql to be displayed in my JTable. how to do it?
Btw, I have used jCalendar as my DateChooser.

Heres my code:

private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        int row = jTable1.getSelectedRow();
        updateid.setText(jTable1.getModel().getValueAt(row, 0).toString());
        updatelast.setText(jTable1.getModel().getValueAt(row, 1).toString());
        updatefirst.setText(jTable1.getModel().getValueAt(row, 2).toString());
        updateinitial.setText(jTable1.getModel().getValueAt(row, 3).toString());
        updateaddress.setText(jTable1.getModel().getValueAt(row, 4).toString());
        updatecourse.setText(jTable1.getModel().getValueAt(row, 5).toString());
        updateyear.setText(jTable1.getModel().getValueAt(row, 6).toString());
        updatecontact.setText(jTable1.getModel().getValueAt(row, 7).toString());
        updateequip.setText(jTable1.getModel().getValueAt(row, 8).toString());
        updatequantity.setText(jTable1.getModel().getValueAt(row, 9).toString());
        updatedateborrowed.setText(jTable1.getModel().getValueAt(row, 10).toString());

    }//GEN-LAST:event_jTable1MouseClicked


 private void cmdaddMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cmdaddMouseClicked
        // TODO add your handling code here:
        String sql = "INSERT INTO student (Student_ID, Student_LN, Student_FN, Student_MI, Address, Course, Year, Contact_No, Equipment_Name, Quantity, Date_Borrowed) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
            
         
        try {
          
            pst = conn.prepareStatement(sql);
            pst.setString(1, txtid.getText());
            pst.setString(2, txtlast.getText());
            pst.setString(3, txtfirst.getText());
            pst.setString(4, txtinitial.getText());
            pst.setString(5, txtaddress.getText());
            pst.setObject(6, itemcourse.getSelectedItem());
            pst.setObject(7, itemyear.getSelectedItem());
            pst.setString(8, txtcontact.getText());
            pst.setObject(9, comboequip.getSelectedItem());
            pst.setObject(10, itemquantity.getSelectedItem());
            pst.setDate(11, new java.sql.Date(System.currentTimeMillis()));
             pst.execute();
            UpdateJTable();
            JOptionPane.showMessageDialog(null, "Information Saved!");
} catch (Exception e) {
            JOptionPane.showMessageDialog(null, e);
        }
            
    }



Quote

updatedateborrowed.setText(jTable1.getModel().getValueAt(row, 10).toString());


what is the correct syntax for it?

Is This A Good Question/Topic? 0
  • +

Replies To: How to Include date in JTable?

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3485
  • View blog
  • Posts: 15,763
  • Joined: 20-September 08

Re: How to Include date in JTable?

Posted 11 March 2012 - 11:49 AM

Your code quotations are confusing. The first one gets data out of a table but next one is actually showing an insert ..?
Was This Post Helpful? 0
  • +
  • -

#3 RDane  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 08-March 12

Re: How to Include date in JTable?

Posted 11 March 2012 - 12:24 PM

The 1st above code is the one who display in the Table
The 2nd is the "add" inserting the date into the sql, I just showed here that I used

Quote

pst.setObject(10, itemquantity.getSelectedItem());
pst.setDate(11, new java.sql.Date(System.currentTimeMillis()));


My problem is, I cant get the date displayed in my Jtable.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3485
  • View blog
  • Posts: 15,763
  • Joined: 20-September 08

Re: How to Include date in JTable?

Posted 11 March 2012 - 12:32 PM

Quote

updateyear.setText(jTable1.getModel().getValueAt(row, 6).toString());


So you're saying that the above doesn't work? If so, what text does it produce?
Was This Post Helpful? 0
  • +
  • -

#5 RDane  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 08-March 12

Re: How to Include date in JTable?

Posted 11 March 2012 - 12:51 PM

It works for all except for the date.
I used the same code "updatedateborrowed.setText(jTable1.getModel().getValueAt(row, 10).toString());"

but the date doesnt comes out in the jTable
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3485
  • View blog
  • Posts: 15,763
  • Joined: 20-September 08

Re: How to Include date in JTable?

Posted 11 March 2012 - 12:52 PM

Make sure you set it properly in the first place
Was This Post Helpful? 0
  • +
  • -

#7 RDane  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 08-March 12

Re: How to Include date in JTable?

Posted 11 March 2012 - 01:00 PM

What is the proper syntax used for date, so that t would appear in my jTable?
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3485
  • View blog
  • Posts: 15,763
  • Joined: 20-September 08

Re: How to Include date in JTable?

Posted 11 March 2012 - 01:03 PM

What date format are you attempting to use?

This post has been edited by g00se: 11 March 2012 - 01:04 PM

Was This Post Helpful? 0
  • +
  • -

#9 RDane  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 08-March 12

Re: How to Include date in JTable?

Posted 11 March 2012 - 01:13 PM

YYYY-MM-DD
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3485
  • View blog
  • Posts: 15,763
  • Joined: 20-September 08

Re: How to Include date in JTable?

Posted 11 March 2012 - 01:17 PM

For date string 'x':

pst.setDate(fieldNum, java.sql.Date.valueOf(x));

Was This Post Helpful? 0
  • +
  • -

#11 RDane  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 08-March 12

Re: How to Include date in JTable?

Posted 11 March 2012 - 01:24 PM

That is for the insert right? Insert works correctly as it is.


What I mean is to display in my JTable. The date that was added in sql shall be displayed in the jTable.
Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3485
  • View blog
  • Posts: 15,763
  • Joined: 20-September 08

Re: How to Include date in JTable?

Posted 11 March 2012 - 02:01 PM

What's it displaying at the moment?
Was This Post Helpful? 0
  • +
  • -

#13 RDane  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 08-March 12

Re: How to Include date in JTable?

Posted 11 March 2012 - 02:09 PM

nothing is displayed.
Was This Post Helpful? 0
  • +
  • -

#14 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3485
  • View blog
  • Posts: 15,763
  • Joined: 20-September 08

Re: How to Include date in JTable?

Posted 11 March 2012 - 02:11 PM

So if you run a normal query on the database, you get data in the date fields that have been created by the insert?

This post has been edited by g00se: 11 March 2012 - 02:11 PM

Was This Post Helpful? 0
  • +
  • -

#15 RDane  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 67
  • Joined: 08-March 12

Re: How to Include date in JTable?

Posted 11 March 2012 - 02:16 PM

Yes..

I have read this article Article on cell renderer, It must use cell renderer for the date to be displayed in JTable. So my question is how to render?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2