繁体   English   中英

无法在JFrame中显示JTable

[英]Cannot show JTable in JFrame

我正在尝试在JFrame中显示SQL查询“ SELECT * FROM ...”的结果。 经过一番挖掘,我在下面的代码中找到了我使用的一些代码。 我的问题是它没有在我的JFrame中显示Jtable。 我是这个方面的新手(这是我第一次尝试将sql和java合并为gui),因此任何帮助将不胜感激...

框架代码:

import java.awt.*;

import javax.swing.*;
import javax.swing.border.Border;

public class Frame  extends JFrame {

    public Frame() throws HeadlessException {
        super();
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        Border loweredetched = null;
        Font font = new Font("monospaced", Font.PLAIN, 11);

        JPanel panel = new JPanel(new BorderLayout());
        JPanel leftPanel = new JPanel();
        JPanel rightPanel = new JPanel();
        JPanel lowerPanel = new JPanel();
        panel.add(leftPanel, BorderLayout.WEST);
        panel.add(rightPanel, BorderLayout.EAST);


        leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));

        //JScrollPane scrollPane = new JScrollPane(GetSongs.table);


        /*JTextArea text = new JTextArea(15, 3);
        text.setMargin(new Insets(5, 5, 5, 5));
        text.setBackground(Color.darkGray);
        text.setForeground(Color.white);
        text.setFont(font);
        text.setEditable(false);*/

        JButton button = new JButton("Update");
        lowerPanel.add(button);

        leftPanel.setBorder(BorderFactory.createTitledBorder(loweredetched, "Songs"));
        leftPanel. add( new JScrollPane( GetSongs.table ), BorderLayout.CENTER );;
        leftPanel.add(lowerPanel);


        JTextArea textR = new JTextArea(1, 3);
        textR.setMargin(new Insets(5, 5, 5, 5));
        textR.setBackground(Color.darkGray);
        textR.setForeground(Color.white);
        textR.setFont(font);
        textR.setEditable(false);

        rightPanel.setBorder(BorderFactory.createTitledBorder(loweredetched, "ToBuy"));
        rightPanel.add(textR);


        getContentPane().add(panel, BorderLayout.NORTH);
        pack();
    }

    public static void main(String[] args){
        new Frame();
        }
}

连接到数据库并进行查询的类的CODE:

import java.sql.*;
import java.util.Vector;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import java.awt.*;

public class GetSongs extends JFrame {

// MAKES A QUERY TAKES THE RESULT SET AND PRODUCES A JTABLE
    public static JTable table;

    public GetSongs() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
        String connectionURL = "jdbc:mysql://localhost:3306/songs";
        Connection connection = null;
        Statement statement = null;

        //do not use in production!!
        String dbuser = "root";
        String dbpass = "";

        ResultSet rs = null;

        Class.forName("com.mysql.jdbc.Driver").newInstance(); //
        connection = DriverManager.getConnection(connectionURL, dbuser, dbpass);
        statement = connection.createStatement();

        String query = "SELECT * FROM songs JOIN purchases WHERE id = song_id and user_id =2;";
        rs = statement.executeQuery(query);

        table = new JTable(buildTableModel(rs));
        JScrollPane scrollPane = new JScrollPane(table);
        table.setFillsViewportHeight(true);
    }

    public static DefaultTableModel buildTableModel(ResultSet rs)
            throws SQLException {

        ResultSetMetaData metaData = rs.getMetaData();

        // names of columns
        Vector<String> columnNames = new Vector<String>();
        int columnCount = metaData.getColumnCount();
        System.out.println(columnCount);
        for (int column = 1; column <= columnCount; column++) {
            columnNames.add(metaData.getColumnName(column));
        }

        // data of the table
        Vector<Vector<Object>> data = new Vector<Vector<Object>>();
        while (rs.next()) {
            Vector<Object> vector = new Vector<Object>();
            for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
                vector.add(rs.getObject(columnIndex));
            }
            data.add(vector);
        }

        return new DefaultTableModel(data, columnNames);
    }

}

或者,您可以使用SwingWorker。

您将在doInBackground()方法中检索所有数据库信息,然后在done()方法中填充表。

由于无法向您解释全部内容,因此这里有更多信息:

http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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