尽管从数据库中填充了JTable,但它不会出现

时间:2017-04-07 10:38:15

标签: java swing jtable

我从数据库中填充了JTable。但桌子没有出现。我无法弄清楚代码的问题。我无法理解问题是布局还是代码块从数据库中检索数据。此外,我没有得到任何异常消息。我在代码中添加了frame.getContentPane().add(scroll, BorderLayout.CENTER);,但仍然无法使用表格。请参阅附图,了解我的内容。enter image description here

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JFrame;
import javax.swing.JScrollPane;

import java.awt.BorderLayout;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import controller.DB_con;

public class Jtable {

    private JFrame frame;
    private JTable table;
    String[] columnNames = {"ID", "name", "username", "contact", "GENDER"};

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Jtable window = new Jtable();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println(e.toString());
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public Jtable() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BorderLayout(0, 0));

        table = new JTable();
        frame.getContentPane().add(table, BorderLayout.CENTER);
        DefaultTableModel model = new DefaultTableModel();
        model.setColumnIdentifiers(columnNames);

        table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
        table.setFillsViewportHeight(true);
        JScrollPane scroll = new JScrollPane(table);
        frame.getContentPane().add(scroll, BorderLayout.CENTER);
        scroll.setHorizontalScrollBarPolicy(
        JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroll.setVerticalScrollBarPolicy(
        JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

        try
        {
            Connection sqlCon = DB_con.getSQLConnection();
            PreparedStatement ps = sqlCon.prepareStatement("select id,name,username,contact,gender from temp_tbl");

            int i = 0;
            ResultSet rs = ps.executeQuery();
            while(rs.next())
            {
                String id = rs.getString("id");
                String name = rs.getString("name");
                String username = rs.getString("username");
                String contact = rs.getString("contact");
                String gender = rs.getString("gender");
                model.addRow(new Object[]{id, name, username, contact, gender});
                i++;
            }
        }
        catch(Exception ex)
        {
            System.out.println(ex.toString());
        }

    }

}

1 个答案:

答案 0 :(得分:2)

您将JTable添加到框架,然后将其添加到JScrollPane,但绝不会将JScrollPane添加到任何内容...

组件只能位于单个容器中,因此当您执行JScrollPane scroll = new JScrollPane(table);时,您将从框架中删除JTable

在您创建frame.getContentPane().add(scroll, BorderLayout.CENTER);

后添加JScrollPane

您也永远不会将TableModel应用于JTable

table.setModel(model);
frame.getContentPane().add(scroll, BorderLayout.CENTER);