1

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();
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
usman newton
  • 11
  • 1
  • 2

1 Answers1

2

TabCl implicitly extends Object, while add() expects a Component. Instead of this:

private class TabCl {…}

create a factory method that returns a JPanel like this:

private JPanel createTablePanel() {
    …
    jTPane.add(jsp);
    return jTPane;
}// end of method createTablePanel

and use it like this:

JPanel tlb = createTablePanel();
cont.add(flPanel, bl.NORTH);
cont.add(glPanel, bl.CENTER);
cont.add(tlb, bl.SOUTH);//// error at this point

See also JDBCAdapter, cited here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanx for your reply. work done.But will you please send me some link to read about this specific factory methods. its usage,what it is and where it can be used and some more explanation. Thanx for your help. – usman newton Sep 15 '15 at 11:05
  • thanx. work done. but will you please send me some link or something like that from where i can understand this "FACTORY METHOD" its usage and some more information. – usman newton Sep 15 '15 at 11:09
  • @usmannewton: The `…` above is your existing `TabCl` constructor code. – trashgod Sep 15 '15 at 15:53
  • @usmannewton; See also [*factory method pattern*](https://en.wikipedia.org/wiki/Factory_method_pattern). – trashgod Sep 15 '15 at 20:39