Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
513 views
in Technique[技术] by (71.8m points)

java - I can't get my JTable to show anything

I cant get my gui to show the Jtable, why i dont know and i dont get any error and when i print something to the screen i get 9 colum. so i get data. but what i'm doing wrong i have no idea about that.

My GUIOdreHandler looks like this

public GUIOrdreHandler(){

            KaldSQL ks = new KaldSQL();
            ResultSet rs;

        }

        public static DefaultTableModel buildTableModel(ResultSet rs)
                throws SQLException {

            java.sql.ResultSetMetaData metaData = rs.getMetaData();

            // names of columns
            Vector<String> columnNames = new Vector<String>();
            int columnCount = metaData.getColumnCount();
            for (int column = 1; column <= columnCount; column++) {
                columnNames.add(metaData.getColumnName(column));
                System.out.println(columnCount);
            }

            // data of the table
            Vector<Vector<Object>> data = new Vector<Vector<Object>>();
            while (rs.next()) {
                Vector<Object> vector = new Vector<Object>();
                for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
                    vector.add(rs.getObject(columnIndex));
                }
                data.add(vector);
            }

            return new DefaultTableModel(data, columnNames);

            }

And my GUIHentOrdre looks like this

public GUIHentOrdre(){

        try {
            con = ks.connectNow();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        JPanel info = new JPanel();
        info.setLayout(new BorderLayout());
        button = new JButton("button");
        info.add(button, BorderLayout.CENTER);
        add(button);
        ResultSet rs = ks.Hentalleordreliste(con);
        GUIOrdreHandler gh = new GUIOrdreHandler();

        try {
            table = new JTable(gh.buildTableModel(rs));

            System.out.println(table);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        info.add(table, BorderLayout.CENTER);
        add(table);
    }
}

I have tried anything google, book northing works, so please help me :D

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

only about mistakes in your code

JPanel info = new JPanel();
info.setLayout(new BorderLayout());
button = new JButton("button");
info.add(button, BorderLayout.CENTER);
add(button);
  • remove code line about add(button); to the (code not exactly talking about)

  • change info.add(button, BorderLayout.CENTER); to the NORTH or SOUTH

  • you not added JTable (in JScrollPane) to the JPanel correctly

pseudo code

JPanel info = new JPanel();
info.setLayout(new BorderLayout());
button = new JButton("button");
info.add(button, BorderLayout.SOUTH);
JTable table = new JTable (ClassOrVoidOrModelNameReturnsTableModel)
JScrollPane scroll = new JScrollPane(table)
info.add(scroll, BorderLayout.CENTER);
  • but this nothing above to solve something, because your issue should be some exception came from JDBC

    1. don't to create JComponents inside try - catch block, prepare this Object before, better could be as local variable

    2. don't to create XxxModel for JComponents inside try - catch block, prepare this Object before, better could be as local variable

    3. intialize XxxModel and its JComponent, then to load data from JDBC to the XxxModel

    4. add rs.close() to the finally block (try - catch - finally)

  • don't reinvent the wheel, use

    1. ResultSetTableModel

    2. Table From Database by @camickr


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...