如何刷新JTable

时间:2015-01-31 10:43:32

标签: java swing jtable

我制作了一个简单的节目,展示法国的Euro2016的球队,比赛和货架目标。 我在更改查询时遇到JTable的问题。 以下是发生的事情:

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

enter image description here

当我从一个表(例如)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);
   }
}

CLASS CONTROL

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);
    }
}
...

有人可以告诉我哪里错了吗?

1 个答案:

答案 0 :(得分:-1)

基本上告诉表刷新自己,你只需要调用它的表模型的方法fireTableDataChanged()。 因此,在您的示例中,运行查询后,您只需调用:

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

但我建议你停止使用默认表模型,并实现自己的表模型。工作起来容易得多。