我对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();
但是当我运行我的代码时,表格根本不会弹出。我也没有错,因此混乱。任何想法为什么会发生这种情况?
答案 0 :(得分:1)
基于您的上下文代码片段,您可能会将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,这样可以让人们更轻松地阅读您的代码并让您阅读其他代码