当frame / Jtable打开时,如何自动加载到Jtable中

时间:2015-02-17 21:42:09

标签: java swing jtable

尝试在加载Frame / Jtable_1时将SQL查询传递给Jtable。 看着论坛和网络我似乎结束了WindowOpened事件或一些抽象的例子,我已经注释掉了。 我是Java的新手,并认为它可能是Jtable setFocus事件或onload事件等,但现在我正在努力获取知识,尽管按钮Actionperformed事件是一种享受。 任何帮助,将不胜感激。

Eclipse IDE Luna 4.4.1 / SQLite / Windows8.1     在这里输入代码

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTabbedPane;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.sql.*;
import javax.swing.*;
import net.proteanit.sql.DbUtils;
import java.awt.event.ActionListener;


public class update extends JFrame {

    private JPanel contentPane;
    //private String x = Hardware.rowid;

    /**
     * Launch the application.
     */

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    update frame = new update();
                    frame.setVisible(true);

                    frame.addWindowListener(new WindowAdapter(){
                        public void windowOpened(WindowEvent e){
                            System.out.println("hello");

                            //Connection connection=null;
                            //connection=sqliteConnection.dbConnector(); //set new connection to sql
                            //String query="select id, Firstname, Surname, username, admin from users where id = "+ Hardware.rowid ; //set sql query statement to variable query
                            //PreparedStatement pst;
                        //try {
                        //pst=connection.prepareStatement(query);
                        //  ResultSet rs=pst.executeQuery(); //pass to rs resultset and execute
                        //table_1.setModel(DbUtils.resultSetToTableModel(rs)); //pass data to table
                        //} catch (SQLException e1) {
                            // TODO Auto-generated catch block
                        //  e1.printStackTrace();
                        //}

                    }
                    });


            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

}

Connection connection=null;

// WindowListener wl = new WindowListener(){
//     public void windowOpened(WindowEvent e)
//     {
//         try
//         {
//          System.out.println (Hardware.rowid);
//          }
//          catch (FileNotFoundException ex) {
//           System.out.println ("didnt work");
//         }
//     }
//     public void windowClosing(WindowEvent e) {}
//     public void windowClosed(WindowEvent e) {} 
//     public void windowIconified(WindowEvent e) {}
//     public void windowDeiconified(WindowEvent e) {}
//     public void windowActivated(WindowEvent e) {}
//     public void windowDeactivated(WindowEvent e) {}
//};
//super.getFrame().addWindowListener(wl);
//}


private JTable table;
//private static JTable table_1;
private JTable table_1;

/**
 * Create the frame.
 */


public update() {
    connection=sqliteConnection.dbConnector(); //set new connection to sql
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);


    JButton btn_update = new JButton("Update Database");
    btn_update.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
                String query="select id, Firstname, Surname, username, admin from users where id = "+ Hardware.rowid ; //set sql query statement to variable query
                PreparedStatement pst=connection.prepareStatement(query); //passs data to pst
                ResultSet rs=pst.executeQuery(); //pass to rs resultset and execute
                table_1.setModel(DbUtils.resultSetToTableModel(rs)); //pass data to table

                System.out.println (Hardware.rowid);

            } catch (Exception e) {
                e.printStackTrace();

            }
        }
    });

    btn_update.setBounds(270, 219, 154, 31);
    contentPane.add(btn_update);

    JLabel lbl_update = new JLabel("Selection HISTORY");
    lbl_update.setBounds(142, 11, 160, 14);
    contentPane.add(lbl_update);

    table_1 = new JTable();
    table_1.setBounds(20, 36, 404, 175);
    contentPane.add(table_1);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(246, 102, 2, 2);
    contentPane.add(scrollPane);

}


}

提前致谢。

0 个答案:

没有答案