Hello everybody!
I want to create a JTable (showing data from mysql..) on a Panel.
But I dont get it..nearly every method is unavailable.
Here is my code for creating it on a frame:
package gfx;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class TableDB extends JFrame
{
public static String sql_preview ;
public TableDB( String sql_preview){
Vector columnNames = new Vector();
Vector data = new Vector();
try
{
// Connect to the Database
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
String url = "jdbc:mysql://localhost/database";
String userid = "root";
String password = "root";
Class.forName( driver );
Connection connection = DriverManager.getConnection( url, userid, password );
// Read data from a table
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery( sql_preview );
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
// Get column names
for (int i = 1; i <= columns; i++)
{
columnNames.addElement( md.getColumnName(i) );
}
// Get row data
while (rs.next())
{
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++)
{
row.addElement( rs.getObject(i) );
}
data.addElement( row );
}
rs.close();
stmt.close();
}
catch(Exception e)
{
System.out.println( e );
}
// Create table with database data
JTable table = new JTable(data, columnNames)
{
public Class getColumnClass(int column)
{
return getValueAt(0, column).getClass();
}
};
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
JPanel buttonPanel = new JPanel();
getContentPane().add( buttonPanel, BorderLayout.SOUTH );
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args)
{
TableDB frame = new TableDB( sql_preview );
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}
private void jbInit() throws Exception {
this.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
this_mouseClicked(e);
}
});
}
}
everything is working fine, but on an external frame. i want to integrate it on a panel, but as an external Class.
Any ideas?
ty for helping me.
bye