Ich benutze Swings in Netbeans. Ich habe die Schaltfläche "view" erstellt, wenn ich auf die Schaltfläche "view" klicke, um die Datenbanktabelle abzurufen und die Tabelle unter der Schaltfläche "view" auf derselben Seite anzuzeigen.Swing Tischansicht in Netbeans
Mein Code:
private void viewActionPerformed(java.awt.event.ActionEvent evt) {
showData(); }
public void showData()
{
Connection con=null;
Statement st=null;
ResultSet rs=null;
String s;
try
{
con=DriverManager.getConnection("jdbc:mysql://localhost/testm","root","root");
st=con.createStatement();
s="select * from smpl";
rs=st.executeQuery(s);
ResultSetMetaData rsmt= rs.getMetaData();
int c=rsmt.getColumnCount();
Vector column=new Vector(c);
for(int i=1;i<=c;i++)
{
column.add(rsmt.getColumnName(i));
}
Vector data= new Vector();
Vector row= new Vector();
while(rs.next())
{
row=new Vector(c);
for(int i=1;i<=c;i++)
{
row.add(rs.getString(i));
}
data.add(row);
}
JFrame smp=new JFrame();
smp.setSize(1000,500);
// smp.setLocation(0, 300);
//smp.setUndecorated(true);
smp.setLocationRelativeTo(null);
smp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel=new JPanel();
// paneldes.setLocation(20, 100);
JTable table= new JTable(data,column);
JScrollPane jsp= new JScrollPane(table);
panel.setLayout(new BorderLayout());
//paneldes.setLocation(100, 200);
panel.add(jsp,BorderLayout.CENTER);
// paneldes.add(jsp);
// jsp.setLocation(100, 200);
this.setContentPane(panel);
this.setVisible(true);
SwingUtilities.getAncestorOfClass(null, jsp);
}
catch(Exception e)
{JOptionPane.showMessageDialog(null,"ERROR");
}finally{
try{
st.close();
rs.close();
con.close();
}catch(Exception e){
JOptionPane.showMessageDialog(null,"ERROR CLOSE");
}}}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new smplvi().setVisible(true);
}
});
}
Dank im Voraus
Was ist es zur Zeit tun? – Shane
Es zeigt das Ergebnis über dem Formular –
Vielleicht sollten Sie mit [Wie Sie Tabellen verwenden] starten (http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) – MadProgrammer