hi,
im trying to get data from my database and add it to the JTable directly...
here is my code..
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.sql.*;
class JTableDatabase
{
public static JFrame f;
public static JButton b;
public void callFrame()
{
b=new JButton("back");
b.setBounds(400,600,100,25);
b.addActionListener(new CallAction());
Vector columnNames = new Vector();
Vector data = new Vector();
JPanel p=new JPanel();
p.add(b);
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbcdbc:leeSQL","sa","adminadmin");
String str = "Select * from payroll.dbo.emp";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(str);
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
for (int i = 1; i <= columns; i++)
{
columnNames.addElement( md.getColumnName(i) );
}
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);
}
JTable table = new JTable(data, columnNames);
// to be changed wen u get error
table = new JTable(data, columnNames)
{
public boolean isCellEditable(int rowIndex,int colIndex)
{
return false;
}
};
TableColumn col;
for (int i = 0; i < table.getColumnCount(); i++) {
col = table.getColumnModel().getColumn(i);
//col.setMaxWidth(1000);
}
JTableHeader header = table.getTableHeader();
header.setBackground(Color.yellow);
JScrollPane scrollPane = new JScrollPane( table );
p.add( scrollPane );
f=new JFrame("REPORT");
f.add(p);
table.setFillsViewportHeight(false);
// to be edited wen u get error.....
//till this...
f.setSize(2000,1500);
f.setVisible(true);
JTableDatabase jtdb=new JTableDatabase();
}
public void actionPerformed(ActionEvent ae){
adminfunc af=new adminfunc();
af.launchframe();
f.setVisible(false);
}
public static void main(String args[])throws Exception{
JTableDatabase j=new JTableDatabase();
j.callFrame();
}
}
finally my data from database appears in the JTable but im unable to change the column size since it looks stingy...so pls help me with it..
dboutput.jpg
as u can see the column size is small..so i need little help in changing the column size...pls help..thanks in advance...