0

2 つのベクトル (データ、列名) を作成し、それらを JTable で使用して列ヘッダーのある行を表示すると、列ヘッダーではなく行のみが表示されます。

JOptionPane では、列ヘッダーが正常に表示されます。太字の場所は、列名が適切に取得されていないため、間違っている場所だと思います。

public void displayLettingProperties() は 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();

      }

    } 
4

1 に答える 1

2

前の質問でこのコードを使用するようにアドバイスされたようJScrollPaneに、UI に追加する前にテーブルを に追加する必要があります。

panelOne.add(new JScrollPane(table));

詳細については、スクロール ペインの使用方法とテーブルの使用方法を参照してください。

于 2013-10-15T00:17:58.997 に答える