简体   繁体   中英

netbeans: how to set value to control which is retrieved from database?

I want to fetch records from database & want to set on form. In this form I am using JDateChooser from JCalendar . I have written the following code for this:

public void showdata()

    int a=leaveView.getSelectedRow();
    int b=(Integer)leaveView.getValueAt(a, 0);
    String c=(String)leaveView.getValueAt(a, 1);
    String d=(String)leaveView.getValueAt(a, 2);
    String e=(String)leaveView.getValueAt(a, 3);
    String f=(String)leaveView.getValueAt(a, 4);
    String  g=(String)leaveView.getValueAt(a, 5);
    String h=(String)leaveView.getValueAt(a, 6);
    int i=(Integer)leaveView.getValueAt(a, 7);  
    String j = (String)leaveView.getValueAt(a, 8);
    String k = (String)leaveView.getValueAt(a, 9);


    AL.empid.setSelectedItem(b);
    AL.empname.setText(c);
    AL.empname.setEditable(true);
    AL.department.setText(d);
    AL.department.setEditable(true);
    AL.leavetype.setSelectedItem(e);
    AL.other.setText(f);
    AL.other.setEditable(true);
    AL.jDateChooser1.setDate(g);
    AL.jDateChooser2.setDate(h);

    AL.noofdays.setText(""+i);
    AL.noofdays.setEditable(true);
    AL.singleday.setSelected(true);
    AL.multipleday.setSelected(true);
}

but it's setting today's date to JDateChooser by default... it's not displaying the date which is retrieved from database... The above code is throwing an error at lines AL.jDateChooser1.setDate(g) and AL.jDateChooser2.setDate(h) for g & h...What can I do?

Assuming you are using this JCalendar API

From the javadocs setDate accepts a date object and not a string. First convert(parse) the dateString(g & h) to Date objects then set the date.

DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); //adjust this according to your requirement
Date gDate,hDate;
        try {
            gDate = df.parse(g);
            hDate = df.parse(h);
        } catch (ParseException e) {
            e.printStackTrace();
        }

AL.jDateChooser1.setDate(gDate);
AL.jDateChooser2.setDate(hDate);

for some example date formats,visit http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM