按钮单击时将JTable和JscrollPane添加到JPanel

时间:2016-01-14 17:55:26

标签: java swing jtable jscrollpane

我在做什么:

Searching Panel

点击搜索航班时,数据来自数据库并显示在另一个JFrame上,如下所示:

Result Panel

我希望这些数据显示在搜索面板的正下方,而不是打开新框架。类似这样的内容:

Desired Panel

我的代码如下:

listingFlight.java

public class listingFlight extends javax.swing.JPanel implements TableCellRenderer {
    public listingFlight() {
        initComponents();
    }
    @SuppressWarnings("unchecked")
    //Netbeans autogenerated componnent code
    @Override
    public Component getTableCellRendererComponent(JTable jTable, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        this.jLabel3.setText(jTable.getModel().getValueAt(row, column).toString()+" ("+jTable.getModel().getValueAt(row, column+1).toString()+")");
        this.jLabel4.setText(jTable.getModel().getValueAt(row, column+2).toString()+" ("+jTable.getModel().getValueAt(row, column+3).toString()+")");
        this.jLabel6.setText(jTable.getModel().getValueAt(row, column+9).toString());

        this.jLabel8.setText(jTable.getModel().getValueAt(row, column+6).toString());
        this.jLabel11.setText(jTable.getModel().getValueAt(row, column+8).toString());
        this.jLabel12.setText(jTable.getModel().getValueAt(row, column+7).toString());
        this.jLabel15.setText(jTable.getModel().getValueAt(row, column+4).toString());
        this.jLabel16.setText(jTable.getModel().getValueAt(row, column+5).toString());

        return this;

    }

}

(点击按钮"搜索航班",我执行此操作。)

try {
                stmt = conn.createStatement();
                rs = stmt.executeQuery("select s.airportName,s.cityName,d.airportName,d.cityName,f.srcTime,f.destTime,f.flightNumber,f.availability,f.price,a.airlineName,f.flightID from flights f inner join airlines a on(a.airlineID=f.flightName) inner join cityinfo s on(s.cityID=f.src) inner join cityinfo d on(d.cityID=f.dest) where f.src=" + jComboBox1.getSelectedIndex() + " and f.dest=" + jComboBox2.getSelectedIndex());
                while (rs.next()) {
                    i++;
                }
                rs.beforeFirst();
                data = new Object[i][11];
                i = 0;
                while (rs.next()) {
                    data[i][0] = rs.getString(1);
                    data[i][1] = rs.getString(2);
                    data[i][2] = rs.getString(3);
                    data[i][3] = rs.getString(4);
                    data[i][4] = rs.getString(5);
                    data[i][5] = rs.getString(6);
                    data[i][6] = rs.getString(7);
                    data[i][7] = rs.getString(8);
                    data[i][8] = rs.getString(9);
                    data[i][9] = rs.getString(10);
                    data[i][10] = rs.getString(11);
                    i++;
                }

                table = new JTable(data, columnNames) {
                    @SuppressWarnings("override")
                    public TableCellRenderer getCellRenderer(int row, int column) {
                        return new listingFlight();
                    }
                };
                showFrame(table);
                table.setRowHeight(82);
                conn.close();
            }

现在showFrame()是:

  private void showFrame(JTable table) {
        JFrame f = new JFrame("Search Result");
        f.setSize(800, 700);
        f.add(new JScrollPane(table));
        f.setVisible(true);

    }

但我正在尝试将jScrollPane添加到现有JPanel,而不是创建新的JFrame并向其显示数据

  private void showFrame(JTable table) {


        this.jPanel1.add(new JScrollPane(table));

    }

此showFrame未将JScrollPane添加到现有的jPanel1。

0 个答案:

没有答案