簡體   English   中英

如何刷新JTable

[英]how to refresh JTable

我正在制作一個簡單的程序,用於顯示法國Euro2016的球隊,比賽和目標。 更改查詢時,JTable有一些問題。 這是發生了什么:

http://i60.tinypic.com/kcxe1c.png

在此處輸入圖片說明

當我從(例如)10行的表更改為僅包含5行的另一表時,它可以工作。 但是,如果我從一個包含5行的表更改為另一個10行的表,則該表沒有變化,它僅顯示5行。 這里的代碼:

public class Euro2016GUI extends JFrame {

private Container container;
private Sfondo pnlSfondo;
JTable table;
JPanel panel;

static Vector<Vector<String>> data = new Vector<Vector<String>>();
static Vector<String> headers = new Vector<String>();

public Euro2016GUI() {
    data.removeAll(data);
    headers.removeAll(headers);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    this.setSize(600, 400);
    this.setTitle("Euro2016");
    this.setLocationRelativeTo(null);
    pnlSfondo = new Sfondo();

    container = this.getContentPane();
    container.add(pnlSfondo);
}

public void createTable(String pQuery) {
    data.removeAll(data);
    headers.removeAll(headers);

    Control control = new Control();

    panel = new JPanel(new BorderLayout());
    panel.setSize(300, 300);
    panel.setBackground(Color.red);

    control.getData(pQuery);
    data = control.getData();
    headers = control.getHeaders();

    //this is the model which contain actual body of JTable
    DefaultTableModel model = new DefaultTableModel(data, headers);

    table = new JTable(model);
    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    table.setEnabled(false);
    table.setMaximumSize(new Dimension(100, 300));

    header_size();

    JScrollPane scroll = new JScrollPane(table);
    //scroll.setSize(600, 400);
    scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

    this.getContentPane().add(panel);
    panel.add(scroll, BorderLayout.CENTER);
}

public void header_size() {
    int colonne = table.getColumnModel().getColumnCount();
    TableColumn column;

    for (int i = 0; i < colonne; i++) {
        column = table.getColumnModel().getColumn(i);
        column.setPreferredWidth(200);
    }
}

public void cleanData() {

    if (table != null) {
        DefaultTableModel dm = (DefaultTableModel) table.getModel();
        dm.setRowCount(0);
        table.revalidate();
    }
    data.removeAll(data);
    headers.removeAll(headers);
   }
}

類控制

public class Control {

private static Vector<Vector<String>> data = new Vector<Vector<String>>();
private static Vector<String> headers = new Vector<String>();

public void getData(String pQuery) {

    // Enter Your MySQL Database Table name in below Select Query.
    String query = pQuery;
    Connection con = null;
    ResultSet rs;
    Statement st = null;
    int colonne = 0;

    data.removeAll(data);
    headers.removeAll(headers);

    try {
        con = DBConnectionPool.getConnection();
        st = con.createStatement();

        rs = st.executeQuery(query);

        ResultSetMetaData rsmd = rs.getMetaData();
        colonne = rsmd.getColumnCount();

        for (int i = 1; i <= colonne; i++) {
            headers.add(rsmd.getColumnName(i));
        }

        while (rs.next()) {
            Vector<String> d = new Vector<String>();

            for (int i = 1; i <= colonne; i++) {
                d.add(rs.getString(i));
            }
            data.add(d);
        }

    } catch (SQLException e) {
        e.printStackTrace();

    } finally {
        if (st != null) {
            try {
                st.close();
            } catch (SQLException ex) {
                Logger.getLogger(DataInJTable.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        if (con != null) {
            DBConnectionPool.releaseConnection(con);
        }
    }
}

public Vector<Vector<String>> getData() {
    return this.data;
}

public Vector<String> getHeaders() {
    return this.headers;
}

}

在菜單中查看動作指導:

...
 //----ROSE---//
 private class OnClickRose implements ActionListener {

    Sfondo sfondo;

    @Override
    public void actionPerformed(ActionEvent e) {
        String str = e.getActionCommand();
        str = str.replace("[", "");
        str = str.replace("]", "");

        String sx = "'";
        String dx = "'";
        String query = query2.concat(sx.concat(str.concat(dx)));
        //frame.cleanData();

        sfondo = frame.getPnlSfondo();
        if (sfondo.isVisible() && sfondo.getParent().isVisible()) {
            sfondo.setVisible(false);
        }

        frame.createTable(query);
    }
}

 //----CALENDARIO----//
private class OnClickCalendario implements ActionListener {

    Sfondo sfondo;

    @Override
    public void actionPerformed(ActionEvent e) {

        frame.cleanData();
        sfondo = frame.getPnlSfondo();

        if (sfondo.isVisible() && sfondo.getParent().isVisible()) {
            sfondo.setVisible(false);
        }

        frame.createTable(query4);
    }
}

//----CLASSIFICA MARCATORI----//
private class OnClickMarcatori implements ActionListener {

    Sfondo sfondo;

    @Override
    public void actionPerformed(ActionEvent e) {

        frame.cleanData();
        sfondo = frame.getPnlSfondo();

        if (sfondo.isVisible() && sfondo.getParent().isVisible()) {
            sfondo.setVisible(false);
        }

        frame.createTable(query3);
    }
}
...

誰能告訴我我哪里錯了?

基本上是告訴表刷新自身,您只需調用表模型的方法fireTableDataChanged()。 因此,在您的示例中,運行查詢后,您可以調用:

((DefaultTableModel)yourTable.getModel()).fireTableDataChanged();

但我建議您停止使用默認表模型,並實現自己的表模型。 它工作起來容易得多。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM