I am working on adapter classes.
I made JTable
in an adapter class TabCl
and called that JTable
class TabCl
in my parent class method guiInt()
and adding it to container con.add()
and its not letting it to do.
I am new to Java and don't have sound knowledge in Java. Please check the following code and give some help or something that can make it work. My java version is 1.6.
import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
public class PersonProject {
JFrame myFrame;
Container cont;
BorderLayout bl;
GroupLayout gl;
FlowLayout fl;
JTable tl;
JScrollPane jsp;
JPanel flPanel, glPanel, jTPane;
JLabel lId;
JTextField tId;
// Db variable Declaration
Connection dbCon;
String url;
String con;
String sql;
ResultSet rs;
ResultSetMetaData rsmd;
PreparedStatement pstmt;
Vector columnName, data, row;
int columnCount;
public PersonProject() {
guiInt();
}
public void guiInt() {
myFrame = new JFrame();
glPanel = new JPanel();
cont = new Container();
bl = new BorderLayout();
fl = new FlowLayout();
flPanel = new JPanel();
flPanel.setLayout(fl);
cont = myFrame.getContentPane();
cont.setLayout(bl);
gl = new GroupLayout(glPanel);
glPanel.setLayout(gl);
gl.setAutoCreateGaps(true);
gl.setAutoCreateContainerGaps(true);
lId = new JLabel("Id");
tId = new JTextField();
// /////////
GroupLayout.SequentialGroup hGroup = gl.createSequentialGroup();
hGroup.addGroup(gl.createParallelGroup().addComponent(lId));
hGroup.addGroup(gl.createParallelGroup().addComponent(tId));
gl.setHorizontalGroup(hGroup);
GroupLayout.SequentialGroup vGroup = gl.createSequentialGroup();
vGroup.addGroup(gl.createParallelGroup(Alignment.BASELINE)
.addComponent(lId).addComponent(tId));
gl.setVerticalGroup(vGroup);
// ///////
TabCl tlb = new TabCl();// / class for making jatable
cont.add(flPanel, bl.NORTH);
cont.add(glPanel, bl.CENTER);
cont.add(tlb, bl.SOUTH);//// error at this point
myFrame.pack();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.setVisible(true);
myFrame.setSize(600, 300);
}// end of method guiinit
private class TabCl {// start of adapter class for making jtable
public TabCl() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
url = "jdbc:odbc:personDSN";
dbCon = DriverManager.getConnection(url);
sql = "select * from emp";
pstmt = dbCon.prepareStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rs = pstmt.executeQuery();
rsmd = rs.getMetaData();
columnCount = rsmd.getColumnCount();
columnName = new Vector(columnCount);
for (int i = 1; i <= columnCount; i++) {
columnName.add(rsmd.getColumnName(i));
}// end of for loop
System.out.println(columnName.get(1));
data = new Vector();
row = new Vector();
while (rs.next()) {
row = new Vector(columnCount);
for (int i = 1; i <= columnCount; i++) {
row.add(rs.getString(i));
}
data.add(row);
}// end of while loop
dbCon.close();
} catch (Exception ex) {
System.out.println(ex + ":Sql Exception at table level");
}
tl = new JTable(data, columnName);
jsp = new JScrollPane(tl);
jTPane = new JPanel();
tl.setPreferredScrollableViewportSize(new Dimension(500, 90));
tl.setVisible(true);
tl.setFillsViewportHeight(true);
jTPane.add(jsp);
}// end of method tabmethod
}// end of class TabCl
public static void main(String[] args) {
// TODO Auto-generated method stub
PersonProject perProj = new PersonProject();
}
}