Skip to main content

JTable in Swing

1 reply [Last post]
meena
Offline
Joined: 2005-04-04

hi,
Good Evening.
I used a Jtable in my project to store my records which are from MS-Access database.My coding is working well and I got all the datas in my jTable clearly.But after closing my JTable my DOS Environment is not working.So I press Ctrl+Alt+Delete keys to close.But I didn't have any
Exceptions.Is it any strong error in my coding?
My coding is:
import java.awt.*;
import java.util.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class myt extends JFrame
{
protected JTable table;
Vector datas;
Vector columnNames;
public myt()
{
super("Student Table");
setSize(600, 340);
JPanel contentpane=(JPanel)getContentPane();
try
{
Connection con;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:SERTS";
String user = "root";
String pw = "";
con = DriverManager.getConnection(url,user,pw);
Statement s = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
String tofirst="SELECT * from [Student-File]";
ResultSet rec;
rec = s.executeQuery(tofirst);
ResultSetMetaData md = rec.getMetaData();
int columns = md.getColumnCount();
columnNames=new Vector();
datas=new Vector();
for (int i = 1; i <= columns; i++)
{
columnNames.addElement( md.getColumnName(i) );
}

while (rec.next())
{
Vector row = new Vector(columns);
for (int i = 1; i <= columns; i++)
{
row.addElement( rec.getObject(i) );
}
datas.addElement( row );
}

rec.close();
s.close();
}
catch (SQLException s)
{
System.out.println("SQL Error: " + s.toString() + " "+ s.getErrorCode() + " " + s.getSQLState());
}
catch (Exception e)
{
System.out.println(" Error: " + e.toString() + e.getMessage());
}
if(table==null)
table = new JTable(datas, columnNames);
else
table.setModel(new DefaultTableModel(datas,columnNames));
JScrollPane scrollPane = new JScrollPane( table );
scrollPane.setPreferredSize(new Dimension(300, 300));
contentpane.add( scrollPane );
scrollPane.setBounds (115,350,800, 200);
table.getTableHeader().setReorderingAllowed(false);
table.getTableHeader().setResizingAllowed(false);

}
public static void main(String arg[]) throws Exception
{

myt mt=new myt();
mt.setVisible(true);
}

}

Thank you so much.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Anonymous

Just add Window Listener after the line:

table.getTableHeader().setResizingAllowed(false);

// add from here
// start window listener

addWindowListener( new WindowAdapter() {

public void windowClosing(WindowEvent we) { System.exit(0);

}});

// end window listener

--
Balakumar Muthu