填充后,Java JTable在我的Frame上不显示

时间:2015-06-23 10:11:09

标签: java mysql swing jtable

我有一个JTable应该从DB获取数据,将这些数据放入DefaultTableModel,然后将该模型放入JTable并显示它。听起来很简单,但我是编程和java的新手,我似乎无法找到解决方案为什么它没有显示,只是在框架中保持空白。我尝试了下面的代码和许多其他变体。

首先关闭Scrollpane,JTable,Frame等(setControl用于显示框架的控制器):

public class CustomerWindow extends JFrame {
    private JPanel panel;
    private JButton btnAllBuCust;
    private Control control;
    private JScrollPane scrollPane;
    private JTable tableCustomer;

public CustomerWindow(Control control) {
    setSize(new Dimension(600, 500));
    setLocation(new Point(100, 100));
    setPreferredSize(new Dimension(600, 500));
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    setTitle("Customer");
    setControl(control);
    setVisible(true);
    getContentPane().add(getPanelTable(), BorderLayout.CENTER);

}

private JPanel getPanelTable() {
    if (panelTable == null) {
        panelTable = new JPanel();
        panelTable.setLayout(new BorderLayout(0, 0));
        panelTable.add(getScrollPane(), BorderLayout.CENTER);
        panelTable.add(getTableCustomer(), BorderLayout.CENTER);
    }
    return panelTable;
}

    private JTable getTableCustomer() {
    if (tableCustomer == null) {
        tableCustomer = new JTable();
        tableCustomer.setBorder(new LineBorder(new Color(0, 0, 0)));
    }
    return tableCustomer;
}

private JScrollPane getScrollPane() {
    if (scrollPane == null) {
        scrollPane = new JScrollPane(tableCustomer);
        scrollPane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        scrollPane.setViewportBorder(new LineBorder(new Color(0, 0, 0)));
        scrollPane.setBackground(Color.WHITE);
        scrollPane.setVisible(true);

    }
    return scrollPane;
}

应该触发新JTable的按钮:

    private JButton getBtnAllBuCust() {
    if (btnAllBuCust == null) {
        btnAllBuCust = new JButton("All Business Customers");
        btnAllBuCust.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                DefaultTableModel model = control.getModelBuCust();
                tableCustomer = new JTable(model);
            }
        });
        btnAllBuCust.setPreferredSize(new Dimension(120, 25));
    }
    return btnAllBuCust;
}

我的控制器类中的getModelBuCust():

    public DefaultTableModel getModelBuCust() {
    Connection con = null;
    PreparedStatement stm = null;
    ResultSet result = null;
    ResultSetMetaData resultMD = null;
    String query;
    int numberOfColumns;
    Object[] rowData;
    DefaultTableModel dtm = new DefaultTableModel();

    try {
        con = Driver.connectionToMySQL();

        query = "SELECT companyname, employeefirstname, employeelastname FROM businesscustomer";

        stm = con.prepareStatement(query);

        result = stm.executeQuery();

        testBuCustResultSet(result);

        resultMD = result.getMetaData();

        numberOfColumns = resultMD.getColumnCount();

        while (result.next()) {
            rowData = new Object[numberOfColumns];
            for (int i = 0; i < rowData.length; ++i) {
                rowData[i] = result.getObject(i + 1);
            }
            dtm.addRow(rowData);
        }


    } catch (Exception e) {
        System.err.println(e);
        e.printStackTrace();
    } finally {
        Tools.cleanUp(con, stm, result);
    }
    return dtm;
}

其他方法:

public class Driver {


    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost/customer";


    static final String USER = "root";
    static final String PASS = "password";

    public static Connection connectionToMySQL() {

        try {

            Class.forName(JDBC_DRIVER);

            Connection con = DriverManager.getConnection(DB_URL, USER, PASS);

            return con;

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

        return null;
    }



}

public class Tools {

public static void cleanUp(Connection con, Statement stm, ResultSet rs) {
    try {
        if (rs != null) {
            rs.close();
        }

    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (stm != null) {
                stm.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
                // XXX Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}

方法cleanUp和connectionToMySQL被处理,它们工作。只是连接到DB并清理con,rs和stm。

当我点击按钮时,我也启动了testBuCustResultSet,它只是将ResultSet中的数据打印到控制台中,它确实显示了所有内容,它在ResultSet中的所有内容,但我的框架仍然保持空白。

非常感谢任何帮助!

0 个答案:

没有答案