【问题标题】:NetBeans jTable not displayingNetBeans jTable 不显示
【发布时间】:2015-12-06 20:50:20
【问题描述】:

我对 java 和 netBeans 比较陌生。我在数据库广告中创建了表,希望在 jTable 上显示此表。为此,我编写了以下代码:

package db_con;

import java.sql.*;
import javax.swing.*;
import net.proteanit.sql.DbUtils;

public class scores extends javax.swing.JPanel {

    Connection conn = null;
    ResultSet re = null;
    PreparedStatement pst = null;
    public scores() {
        initComponents();
        conn = simple.db_connection();
        update_table();
    }

    public void update_table()
    {
        try
        {
            String sql = "SELECT * FROM SINGLE_MATCH";
            pst = conn.prepareStatement(sql);
            re = pst.executeQuery();
            myTable.setModel(DbUtils.resultSetToTableModel(re));
            myTable.getColumnModel().getColumn(0).setPreferredWidth(15);
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(null, e);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        myTable = new javax.swing.JTable();

        myTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(myTable);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(15, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(14, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 275, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>                        


    // Variables declaration - do not modify                     
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable myTable;
    // End of variables declaration                   
}

我主要这样称呼这个类:

new scores().update_table();

但是当我运行我的代码时,表格根本不会弹出。我也没有收到任何错误,因此很混乱。任何想法为什么会发生这种情况?

【问题讨论】:

标签: java sql swing netbeans jtable


【解决方案1】:

根据您对上下文外代码的 sn-ps,您可能没有将 scores 添加到可显示的容器中,例如 JFrame,或者您正在更新的容器不是具有已添加到可显示的容器中。

查看How to Make Frames (Main Windows)了解更多详情

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Main {

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

    public Main() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                scores theScores = new scores();
                theScores.updateTable();

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(scores);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

}

您可能还想通读Code Conventions for the Java TM Programming Language,它会让人们更容易阅读您的代码,也让您更容易阅读其他人

【讨论】:

    猜你喜欢
    • 2012-01-05
    • 1970-01-01
    • 2014-04-21
    • 2019-06-08
    • 2014-09-29
    • 1970-01-01
    • 2012-06-08
    相关资源
    最近更新 更多