如何在适配器类中创建JTable并在主类中调用它

时间:2015-09-15 07:31:21

标签: java swing jtable

我正在研究适配器类。 我在适配器类JTable中创建了TabCl,并在我的父类方法JTable中调用了TabClguiInt()并将其添加到容器con.add()而且它没有让它去做。

我是Java的新手,并且没有Java方面的熟练知识。请检查以下代码并提供一些帮助或可以使其工作的内容。 我的java版本是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();
    }
}

1 个答案:

答案 0 :(得分:2)

TabCl隐含地延伸Object,而add()则需要Component。而不是:

private class TabCl {…}

创建一个返回JPanel的工厂方法,如下所示:

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

并像这样使用它:

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

另请参阅JDBCAdapter,引用here