在JFrame中刷新JPanel上的JScrollPane JTable数据

时间:2016-07-03 15:05:05

标签: java swing jtable jpanel abstracttablemodel

我想用我的数据库(MySQL)获得的新数据填充我的表。我得到所有数据并用它们创建一个新的模型,但是如果我想刷新特定的面板,那么它就不会被重新绘制。

public class PanelWest extends JPanel implements ActionListener {
private JButton but_selectBP;
private JButton but_selectBPAdr;
private JButton but_selectGerichte;
private GroupLayout layoutGroup;
private Connector stmtExecuter = new Connector();
//  private PanelCenter tableViewer = new PanelCenter();


public PanelWest() {
    layoutGroup = createLayout();
    this.setLayout(layoutGroup);
    createButtons();
}

private GroupLayout createLayout() {
    GroupLayout layout = new GroupLayout(this);
    layout.setAutoCreateGaps(true);
    layout.setAutoCreateContainerGaps(true);
    return layout;
}


void createButtons() {
    this.but_selectBP = new JButton("Kunden anzeigen");
    this.but_selectBP.addActionListener(this);
    this.but_selectBPAdr = new JButton("Gerichte anzeigen");
    this.but_selectBPAdr.addActionListener(this);
    this.but_selectGerichte = new JButton("Lieferanten anzeigen");
    this.but_selectGerichte.addActionListener(this);

    this.layoutGroup.setHorizontalGroup(layoutGroup.createParallelGroup().addComponent(but_selectBP).addComponent(but_selectBPAdr).addComponent(but_selectGerichte));
    this.layoutGroup.setVerticalGroup(layoutGroup.createSequentialGroup().addComponent(but_selectBP).addComponent(but_selectBPAdr).addComponent(but_selectGerichte));
}

@Override
public void actionPerformed(ActionEvent e) {
    Object src = e.getSource();

    if (src.equals(this.but_selectBP)) {
        String query = "SELECT * FROM Kunde";
        ResultSet rst = this.stmtExecuter.getResultDBData(query);
    //    this.tableViewer.setTableName("Kunde");
        new PanelCenter().createTable(fillHeader(rst), fillData(rst));
    }

    if (src.equals(this.but_selectBPAdr)) {
        String query = "SELECT * FROM Gericht";
        ResultSet rst = this.stmtExecuter.getResultDBData(query);
    //    this.tableViewer.createTable(fillHeader(rst), fillData(rst));
    }

    if (src.equals(this.but_selectGerichte)) {
        String query = "SELECT * FROM Lieferant";
        ResultSet rst = this.stmtExecuter.getResultDBData(query);
     //   this.tableViewer.createTable(fillHeader(rst), fillData(rst));
    }
}

private String[] fillHeader(ResultSet rst) {
    try {
        ResultSetMetaData rstMetaData = rst.getMetaData();
        String[] header = new String[rstMetaData.getColumnCount()];
        ArrayList<String> headerDetails = new ArrayList<>();
        for (int i = 1; i <= rstMetaData.getColumnCount(); i++) {
           headerDetails.add(rstMetaData.getColumnName(i));
        }
        int j = 0;
        for(String head : headerDetails){
            header[j] = head;
            j++;
        }
        return header;
    } catch (SQLException se) {
        se.printStackTrace();
    }
    return null;
}

private Object[][] fillData(ResultSet rst) {
    try {
        ResultSetMetaData rstMetaData = rst.getMetaData();
        int rowCount = 0;

        rst.last();
        rowCount = rst.getRow();
        System.out.println(rowCount + " Rows");
        rst.beforeFirst();

        Object[][] data = new Object[rowCount][rstMetaData.getColumnCount()];

        int row = 0;
        while (rst.next()) {
            for (int i = 0; i < rstMetaData.getColumnCount(); i++) {
                data[row][i] = rst.getObject(i + 1);
            }
            row++;
        }
        return data;
    } catch (SQLException se) {
        System.out.println("Hier bei Fill");
    }
    return null;
}
}

我使用删除,在我的jpanel上添加revalidate和重绘。

 void createTable(String[] header, Object[][] data) {
    this.tableData = new JTable();
    this.tableData.setModel(new MyTableModel(header, data));
    this.tableData.setFillsViewportHeight(true);
    this.tableData.addKeyListener(this);
    this.scrollPaneTable = new JScrollPane(tableData);
    this.scrollPaneTable.setSize(500, 500);
    this.remove(this.scrollPaneTable);
    this.add(this.scrollPaneTable);
    this.revalidate();
    this.repaint();
}

2 个答案:

答案 0 :(得分:1)

您不需要重新初始化表格,表格模型。

将一些全局变量置于顶部

private MyTableModel tableModel; //Your own table model
private JTable table;

在init

上初始化它们
public PanelWest() {
    layoutGroup = createLayout();
    this.setLayout(layoutGroup);
    createButtons();
    tableModel = new MyTableModel(header, data); //Your own tablemodel
    table = new JTable(tableModel); //Hook the model to your table
    this.add(table)
    //...Do other things else to your table
}

想要更新表格后,只需清除表格模型中的行并填写新行。

JTable通过调用

更新其数据
void createTable(String[] header, Object[][] data){
    int cols = header.length;
    int rows = data.length;

    //Remove all rows from model 
    tableModel.setRowCount(0); //(As said by HovercraftFullOfEels)

    Object[] row = new Object[cols];
    for (int j = 0; j < data.length; j++){
        for (int i = 0; i < cols; i++){
            row[i] = data[j][i];
        }
        tableModel.addRow(row);
    }
    tableModel.fireTableDataChanged();
}

希望它会有所帮助。

答案 1 :(得分:0)

感谢您的帮助。 我在createTable方法中添加了一个小测试。我创建一个新窗口来显示新的表数据,它的工作原理。我认为我的jpanel没有正确重新绘制,因为我的播放。

        this.tableData = new JTable();
    this.tableData.setModel(new MyTableModel(header, data));
    this.tableData.setFillsViewportHeight(true);
    this.tableData.addKeyListener(this);
    this.scrollPaneTable = new JScrollPane(tableData);
    this.scrollPaneTable.setSize(500, 500);

    this.layoutGroup.setVerticalGroup(layoutGroup.createSequentialGroup().addComponent(this.scrollPaneTable, 400, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(this.scrollPaneChanges).addComponent(this.but_user).addComponent(this.but_dataChange));


      //        JFrame fr = new JFrame("Hello");
      //        fr.setLayout(null);
     //        fr.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    //        fr.setVisible(true);
    //        fr.add(this.scrollPaneTable);