Reputation: 187
I am relatively new to java and netBeans. I have created table in a database ad want to display this table on a 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?
Upvotes: 0
Views: 650
Reputation: 347294
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.
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
Upvotes: 1