我有生成两个表的Java代码;一个用于传入订单,另一个用于服务订单。这些表应该在运行时自动更新。我有计时器的代码,但我不知道在哪里放置它。请注意,我是JTables的新手。请帮忙。这是我的代码:
package marvelin;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.Timer;
import javax.swing.WindowConstants;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
public class OrderMaking extends JFrame{
MessManagement mm=new MessManagement();
String foodorder1;
String tableno1;
String ordertime1;
String orderdate1;
String incomingordersid;
private static final long serialVersionUID = 1L;
JFrame customer = new JFrame("Orders Made");
public void frmViewOrders() throws InterruptedException{
Toolkit toolkit = getToolkit();
Dimension size = toolkit.getScreenSize();
int width=size.width*70/100;
int height=size.height*80/100;
int xlocation=(size.width-width)/2;
customer.setLocation(xlocation,0);
customer.setSize(width,height);
customer.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JTabbedPane tabbedPane = new JTabbedPane(SwingConstants.TOP);
tabbedPane.addTab("Incoming Orders",incomingOrders());
tabbedPane.addTab("Served Orders",servedOrders());
customer.getContentPane().add(tabbedPane);
customer.setVisible(true);
customer.setAlwaysOnTop(false);
customer.setResizable(false);
}
public Component incomingOrders() throws InterruptedException{
/* ActionListener taskPerformer = new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
//...Perform a task...
System.out.println("Reading SMTP Info.");
JOptionPane.showMessageDialog(null, "mndnd");
}
};
Timer timer = new Timer(1000 ,taskPerformer);
timer.setRepeats(true);
timer.start();
Thread.sleep(5000);*/
String qry = "SELECT * FROM incomingorders";
String fields[] = {"Table Number","Order ID","Food Order","Order Date","Order Time","Served"};
int rowset=0;
try{
mm.dbConnect.createConnection();
ResultSet rs=mm.stmt.executeQuery(qry);
while(rs.next()){
rowset++;
}
mm.dbConnect.closeConection();
}
catch(SQLException e){
mm.dbConnect.closeConection();
}
DefaultTableModel table=new DefaultTableModel(fields,rowset){
private static final long serialVersionUID = 1L;
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
final JTable tbe=new JTable(table);
tbe.setRowSelectionAllowed(true);
tbe.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
tbe.setValueAt("Yes", tbe.getSelectedRow(), 5);
tableno1=(String) tbe.getValueAt(tbe.getSelectedRow(), 0);
incomingordersid=(String) tbe.getValueAt(tbe.getSelectedRow(), 1);
foodorder1=(String) tbe.getValueAt(tbe.getSelectedRow(), 2);
orderdate1=(String) tbe.getValueAt(tbe.getSelectedRow(), 3);
ordertime1=(String) tbe.getValueAt(tbe.getSelectedRow(), 4);
String insertQ = "INSERT into servedorders(tablenumber, incomingordersid, foodorder, orderdate, ordertime) VALUES('"+tableno1+"','"+incomingordersid+"','"+foodorder1+"','"+orderdate1+"','"+ordertime1+"')";
Statement stmt = mm.dbConnect.createStatement();
try {
stmt.executeUpdate(insertQ);
} catch (SQLException ex) {
Logger.getLogger(FoodPriceEntry.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
JScrollPane pane=new JScrollPane(tbe);
try{
int rows=0;
mm.dbConnect.createConnection();
ResultSet rs=mm.stmt.executeQuery(qry);
while(rs.next()){
String incomingordersId=rs.getString("orderid");
String tableno=rs.getString("tablenumber");
String foodorder=rs.getString("foodorder");
String orderdate=rs.getString("orderdate");
String ordertime=rs.getString("ordertime");
tbe.setValueAt(tableno, rows, 0);
tbe.setValueAt(incomingordersId, rows, 1);
tbe.setValueAt(foodorder, rows, 2);
tbe.setValueAt(orderdate, rows, 3);
tbe.setValueAt(ordertime, rows, 4);
tbe.setValueAt("No", rows, 5);
rows++;
}
}
catch(SQLException e){
}
return pane;
}
public Component servedOrders(){
//(tablenumber, foodorder, orderdate, ordertime)
String qry = "SELECT * FROM servedorders";
String fields[] = {"Table Number","Food Order","Order Date","Order Time"};
int rowset=0;
try{
mm.dbConnect.createConnection();
ResultSet rs=mm.stmt.executeQuery(qry);
while(rs.next()){
rowset++;
}
mm.dbConnect.closeConection();
}
catch(SQLException e){
mm.dbConnect.closeConection();
}
DefaultTableModel table=new DefaultTableModel(fields,rowset){
private static final long serialVersionUID = 1L;
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
JTable tbe=new JTable(table);
tbe.setRowSelectionAllowed(true);
JScrollPane pane=new JScrollPane(tbe);
try{
int rows=0;
mm.dbConnect.createConnection();
ResultSet rs=mm.stmt.executeQuery(qry);
while(rs.next()){
String tableno=rs.getString("tablenumber");
String foodorder=rs.getString("foodorder");
String orderdate=rs.getString("orderdate");
String ordertime=rs.getString("ordertime");
tbe.setValueAt(tableno, rows, 0);
tbe.setValueAt(foodorder, rows, 1);
tbe.setValueAt(orderdate, rows, 2);
tbe.setValueAt(ordertime, rows, 3);
//table.fireTableDataChanged();
rows++;
tbe.repaint();
}
}
catch(SQLException e){
}
return pane;
}
public static void main(String [] ars) throws InterruptedException{
OrderMaking oMake = new OrderMaking();
oMake.frmViewOrders();
}
}