Java, Python, Database, Flutter, Matlap, Micorcontroller, Tutorials, Swing Framework

Hancie e-Learning Studio

Learn Java, Learn HTML, CSS, PHP, Javascript, Python Tutorials || Download program source codes || Java Project and Source code available here || All types error troubleshooting tips available here

How To Get Date From JTable using MySQL database And Set It Into JDateChooser In Java Swing?

 

package Trial;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import com.toedter.calendar.JDateChooser;
import Libs.BookingLibs2;
import Libs.JDBCBooking;

public class Demo implements MouseListener {
JTable table;
JDateChooser checkin,checkout1;
 
public Demo() {
JFrame frame=new JFrame();
frame.setSize(950,550);
frame.setLayout(null);;
checkin=new JDateChooser();
    checkin.setDateFormatString("yyyy-MM-dd");
    checkin.setBounds(10,10,200,35);
    checkin.setBorder(BorderFactory.createLineBorder(Color.white,1));
checkin.setFont(new Font("Verdana",Font.PLAIN,18));
frame.add(checkin);
checkout1=new JDateChooser();
    checkout1.setDateFormatString("yyyy-MM-dd");
    checkout1.setBounds(10,50,200,35);
    checkout1.setBorder(BorderFactory.createLineBorder(Color.white,1));
    checkout1.setFont(new Font("Verdana",Font.PLAIN,18));
frame.add(checkout1);
DefaultTableModel dm=new DefaultTableModel();
dm.addColumn("Customer ID");
dm.addColumn("Name");
dm.addColumn("Booking ID");
dm.addColumn("Check In");
dm.addColumn("Check Out");
dm.addColumn("Booking Type");
dm.addColumn("Booking Status");
table=new JTable(dm);
table.addMouseListener(this);
table.setToolTipText("Booking Status");
table.getTableHeader().setBackground(Color.black);
table.getTableHeader().setForeground(Color.white);
table.setFont(new Font("Tahoma", Font.PLAIN, 16));
table.setFillsViewportHeight(true);
table.setAutoCreateRowSorter(true);
table.setSelectionBackground(Color.black);
table.setSelectionForeground(Color.white);
table.setRowHeight(23);
JDBCBooking jdbc=new JDBCBooking();
ArrayList a5=jdbc.ReceptionViewAllCustomer();
if(a5.size()>0) {
for(int i=0; i<a5.size(); i++) {
BookingLibs2 bio=(BookingLibs2) a5.get(i);
Object [] tmp= {bio.getCustomer_ID(),
bio.getName(),
bio.getBooking_ID(),
bio.getCheckIn(),
bio.getCheckOut(),
bio.getBooking_Type(),
bio.getBooking_Status()
};
dm.addRow(tmp);
}
}

JScrollPane scroll=new JScrollPane(table);
scroll.setBounds(400,10,500,400);
frame.add(scroll);
frame.setVisible(true);
}
public void mouseClicked(MouseEvent ae) {
if (ae.getSource() == table) {
try {
int h2=table.getSelectedRow();
TableModel model=table.getModel();
Date date = new SimpleDateFormat("yyyy-MM-dd").parse((String)model.getValueAt(h2, 3));
checkin.setDate(date);
Date date2 = new SimpleDateFormat("yyyy-MM-dd").parse((String)model.getValueAt(h2, 4));
checkout1.setDate(date2);
}
catch(Exception ex) {
System.out.println("Error"+ex.getMessage());
}
}
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
new Demo();
}
}

Output
Welcome all to Hancie e-learning studio
Friends,
I have brought an e-learning platform for you from where you can teach internet, website, programming language, error troubleshooting, etc. Blogger, WordPress templates, themes are available for free on this website, which are not charged for downloading, so that you can earn money by blogging using such templates and this is not a difficult task and this You can do it now. You keep uploading new posts by us and keep taking advantage of this website. The aim of which is to spread the knowledge related to internet, career, web designing and technology to the people and contribute to the development of the country.