[英]NetBeans jTable not displaying
I am relatively new to java and netBeans. 我对java和netBeans比较陌生。 I have created table in a database ad want to display this table on a jTable.
我在数据库广告中创建了表格,希望在jTable上显示该表格。 I have written the following code to do so:
我已经编写了以下代码:
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
}
And I am calling this class in the main like so: 我主要这样称呼此类:
new scores().update_table();
But when I run my code the table won't pop up at all. 但是,当我运行代码时,该表将不会弹出。 I am getting no errors either, hence the confusion.
我也没有收到任何错误,因此很混乱。 Any ideas why this could be happening?
任何想法为什么会发生这种情况?
Based on you snippets of out-of-context code, it would appear that your either not adding scores
to a displayable container, like JFrame
or the one you are updating isn't the one which has been added to a displayable container. 根据您的上下文外代码片段,您似乎没有将
scores
添加到可显示容器(例如JFrame
或者您要更新的scores
不是已添加到可显示容器中的scores
。
Take a look at How to Make Frames (Main Windows) for more details 看一看如何制作框架(主窗口)以了解更多详细信息
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);
}
});
}
}
You might also like to have a read through Code Conventions for the Java TM Programming Language , it will make it easier for people to read your code and for you to read others 您可能还希望通读Java TM编程语言的代码约定 ,这将使人们更容易阅读您的代码,并使您阅读其他代码更容易
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.