M
mamta81
I am trying t make a jTable Using Abstract TableModel. But my JTable
doesnot appear on the frame.I have written two classes myTable and
jTable2 .My code is given below:
import javax.swing.*;
import java.awt.*;
public class myTable {
public static void main(String args[]){
JTable2 tbl2=new JTable2();
JTable aTbl=new JTable(tbl2);
aTbl.updateUI();
aTbl.setVisible(true);
JFrame frame=new JFrame("Jtable using AbstractTableModel");
JPanel pan=new JPanel();
JScrollPane scp=new JScrollPane();
scp.add(aTbl);
pan.add(scp);
frame.getContentPane().add(pan);
frame.setVisible(true);
frame.pack();
}
}
import javax.swing.*;
import java.sql.*;
import java.util.*;
import javax.swing.table.*;
public class JTable2 extends AbstractTableModel{
Connection con;
Statement stmt;
ResultSet rs;
int columns;
Vector allRows;
Vector row=new Vector();
String [] columnNames={"ID_CODE","NAME","SECTION"};
public JTable2(){
// connect to database
try{
db_connect();
getData();
}catch(Exception ex){
ex.printStackTrace();
}
}
void db_connect() throws SQLException{
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbcracle:thin158.144.71.242:1521:dbadp","payroll","sush");
System.out.println("Connected");
}catch(Exception ex){
ex.printStackTrace();
}
}
void getData() throws SQLException {
try{
stmt=con.createStatement();
rs=stmt.executeQuery("select idcode,id_name,sec_code from
employee");
ResultSetMetaData rsMetaData=rs.getMetaData();
columns=rsMetaData.getColumnCount();
allRows=new Vector();
while(rs.next()){
Vector newRow=new Vector();
for(int i=1;i<=columns;i++){
newRow.addElement(rs.getObject(i));
}
allRows.addElement(newRow);
}
}catch (Exception ex){
ex.printStackTrace();
}
}
public int getRowCount(){
return allRows.size();
}
public int getColumnCount(){
return columns;
}
public Object getValueAt(int aRow,int aColumn){
row=(Vector) allRows.elementAt(aRow);
return row.elementAt(aColumn);
}
public boolean isCellEditable(int row, int col){
return false;
}
}
doesnot appear on the frame.I have written two classes myTable and
jTable2 .My code is given below:
import javax.swing.*;
import java.awt.*;
public class myTable {
public static void main(String args[]){
JTable2 tbl2=new JTable2();
JTable aTbl=new JTable(tbl2);
aTbl.updateUI();
aTbl.setVisible(true);
JFrame frame=new JFrame("Jtable using AbstractTableModel");
JPanel pan=new JPanel();
JScrollPane scp=new JScrollPane();
scp.add(aTbl);
pan.add(scp);
frame.getContentPane().add(pan);
frame.setVisible(true);
frame.pack();
}
}
import javax.swing.*;
import java.sql.*;
import java.util.*;
import javax.swing.table.*;
public class JTable2 extends AbstractTableModel{
Connection con;
Statement stmt;
ResultSet rs;
int columns;
Vector allRows;
Vector row=new Vector();
String [] columnNames={"ID_CODE","NAME","SECTION"};
public JTable2(){
// connect to database
try{
db_connect();
getData();
}catch(Exception ex){
ex.printStackTrace();
}
}
void db_connect() throws SQLException{
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbcracle:thin158.144.71.242:1521:dbadp","payroll","sush");
System.out.println("Connected");
}catch(Exception ex){
ex.printStackTrace();
}
}
void getData() throws SQLException {
try{
stmt=con.createStatement();
rs=stmt.executeQuery("select idcode,id_name,sec_code from
employee");
ResultSetMetaData rsMetaData=rs.getMetaData();
columns=rsMetaData.getColumnCount();
allRows=new Vector();
while(rs.next()){
Vector newRow=new Vector();
for(int i=1;i<=columns;i++){
newRow.addElement(rs.getObject(i));
}
allRows.addElement(newRow);
}
}catch (Exception ex){
ex.printStackTrace();
}
}
public int getRowCount(){
return allRows.size();
}
public int getColumnCount(){
return columns;
}
public Object getValueAt(int aRow,int aColumn){
row=(Vector) allRows.elementAt(aRow);
return row.elementAt(aColumn);
}
public boolean isCellEditable(int row, int col){
return false;
}
}