levyuk
asked on
Dynamically creating jtables
Hi,
I have a problem, hence me being here, I would like to create an application that can connect to a database, retrive the data and then display this data in a table.
I have two problems, the first is, how do I display this data in a table, and the 2nd is how do I change the amount of columns in the table to reflect the ammount of columns in the database table?
ANy assistance would be great.
Jon
I have a problem, hence me being here, I would like to create an application that can connect to a database, retrive the data and then display this data in a table.
I have two problems, the first is, how do I display this data in a table, and the 2nd is how do I change the amount of columns in the table to reflect the ammount of columns in the database table?
ANy assistance would be great.
Jon
You must get ResultSet from database connection after executing Statement.execQuery, from that ResultSet You get all the data You need - column count & names, the databse data... Then you make a JTable just like that:
=============
import java.awt.*;
import javax.swing.*;
public class B extends JFrame {
B() {
setDefaultCloseOperation(J Frame.EXIT _ON_CLOSE) ;
Container cp = getContentPane();
cp.add(new JTable(5,4));
setSize(300,300);
setVisible(true);
}
public static void main(String[] args)
{
B b = new B();
}
}
===========
and depending on the data You manage to get from ResultSet and ResultSetMetaData fill in the table.
Good luck.
=============
import java.awt.*;
import javax.swing.*;
public class B extends JFrame {
B() {
setDefaultCloseOperation(J
Container cp = getContentPane();
cp.add(new JTable(5,4));
setSize(300,300);
setVisible(true);
}
public static void main(String[] args)
{
B b = new B();
}
}
===========
and depending on the data You manage to get from ResultSet and ResultSetMetaData fill in the table.
Good luck.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#data