Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

When i create 2 vectors (data, columnnames)and use them in a JTable to display rows with a column header it just displays the rows and not the column header.

In a JOptionPane they the column headers show up fine. I assume where bolded is where its going wrong as its not getting column names properly there.

public void displayLettingProperties() throws SQLException{

    Connection conn = null;
    try {       
        conn = DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD);
        System.out.println("Connected to database.");       

         // The Connection is obtained

        Statement Stmt = (Statement) conn.createStatement();
    //  Stmt.execute(createPropertyTable);

        ResultSet rs = Stmt.executeQuery("select * from PropertyLettings ORDER BY propertyBedrooms ASC");

        // It creates and displays the table
        **JTable table = new JTable(buildTableModel(rs));**


     // Set Column Widths

        table.getColumnModel().getColumn(0).setPreferredWidth(100);
        table.getColumnModel().getColumn(1).setPreferredWidth(50);
        table.getColumnModel().getColumn(2).setPreferredWidth(350);
        table.getColumnModel().getColumn(3).setPreferredWidth(100);
        table.getColumnModel().getColumn(4).setPreferredWidth(100);
        table.getColumnModel().getColumn(5).setPreferredWidth(350);
        table.getColumnModel().getColumn(6).setPreferredWidth(100);
        table.getColumnModel().getColumn(7).setPreferredWidth(130);



     //   JOptionPane.showMessageDialog(null, new JScrollPane(table));

        final JPanel panelOne = new JPanel();
        panelOne.setVisible(true);
        panelOne.setBackground(Color.LIGHT_GRAY);
        // JFRAME
        final JFrame topFrame = new JFrame();
        topFrame.setSize(1550, 300);
        topFrame.setLocationRelativeTo ( null );
        topFrame.setVisible(true);
        topFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


        // PUT TOGETHER
        topFrame.add(panelOne);

        panelOne.add(table);


        panelOne.revalidate();
        panelOne.repaint();


    } catch (SQLException e) {
        System.err.println("Cannot connect to database." + e);


    } finally {

    if(conn != null){
        conn.close();

      }

    } 
See Question&Answers more detail:os

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

1 Answer

As you were advised to do in your previous question with this code, you need to add the table into a JScrollPane before adding to the UI.

panelOne.add(new JScrollPane(table));

Take a look at How to use scroll panes and How to use tables for more details...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...