我在更新/刷新表格中的數據時遇到問題。我正在使用DefaultTableModel。這裏是代碼,請分享您的知識和建議。最好的祝福。JTable和DefaultTableModel不能刷新表格數據
* 編輯1:* 原代碼有被更新表數據的功能,但由於構造函數是創建一遍又一遍新的JTable和新JScrollPane的的,證明是錯誤的。這就是爲什麼我創建了新的構造函數並修復了該錯誤。在我實現新的構造函數之後,表數據仍然沒有刷新。接下來,我嘗試調試構造函數接收的其他類(passDatatoTable類)的數據。在使用函數setValueAt()和System.out.println(getValueAt(i,1))之後,輸出爲空。這就是它 首先這裏是一個創建GUI主類:
import javax.swing.*;
public class Glavni {
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
gui Scanner = new gui();
Scanner.setVisible(true);
}
});
}
}
其次這裏是類,將數據發送到GUI
public class passDatatoTable {
public void passData(){
String str1,str2,str3,str4;
for (int i =0;i<=10;i++){
str1="Column 1 of row: "+i;
str2="Column 2 of row: "+i;
str3="Column 3 of row: "+i;
str4="Column 4 of row: "+i;
gui SendStringsToGUI = new gui(str1, str2, str3, str4);
}
}
}
這裏是GUI類。按鈕「添加數據」用於填充數據。這裏是代碼:
public class gui extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
String[][] data = new String[100][4];
String[] columnNames = new String[]{
"IP", "PC_NAME", "ttl", "db"
};
DefaultTableModel model = new DefaultTableModel(data,columnNames);
JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
int i=0;
public gui(String IP, String PC_NAME, String ttl, String gw) {
//Used this constructor to avoid creating new gui in other classes
model.setValueAt(IP, i, 0);
model.setValueAt(PC_NAME, i, 1);
model.setValueAt(ttl, i, 2);
model.setValueAt(gw, i, 3);
i++;
model.fireTableDataChanged();
table.repaint();
scrollPane.repaint();
}
gui(){
JButton addData= new JButton("Add Data");
JButton next = new JButton("next");
JButton prev = new JButton("prev");
addData.addActionListener(this);
next.addActionListener(this);
prev.addActionListener(this);
JPanel panel = new JPanel(new BorderLayout());
JPanel buttonPanel = new JPanel();
buttonPanel.add(addData);
buttonPanel.add(prev);
buttonPanel.add(next);
panel.add(buttonPanel, BorderLayout.SOUTH);
panel.add(table.getTableHeader(), BorderLayout.PAGE_START);
panel.add(scrollPane, BorderLayout.CENTER);
getContentPane().add(panel);
}
這裏是ActionListeners。 Next按鈕和prev用於尋呼和按鈕「添加數據」被用來創建類「passDatatoTable」,這是用來填充表的數據變量
這裏是代碼:
public void actionPerformed(ActionEvent e) {
if ("Add Data".equals(e.getActionCommand())){
passDatatoTable passSomeData = new passDatatoTable();
passSomeData.passData();
}
if ("next".equals(e.getActionCommand())) {
Rectangle rect = scrollPane.getVisibleRect();
JScrollBar bar = scrollPane.getVerticalScrollBar();
int blockIncr = scrollPane.getViewport().getViewRect().height;
bar.setValue(bar.getValue() + blockIncr);
scrollPane.scrollRectToVisible(rect);
}
if ("prev".equals(e.getActionCommand())) {
Rectangle rect = scrollPane.getVisibleRect();
JScrollBar bar = scrollPane.getVerticalScrollBar();
int blockIncr = scrollPane.getViewport().getViewRect().height;
bar.setValue(bar.getValue() - blockIncr);
scrollPane.scrollRectToVisible(rect);
}
}
您不妨花費一些精力來描述問題的細節。究竟發生了什麼?應該發生什麼?您採取了哪些措施來嘗試調試此問題? –
好吧我會更新我的問題,我會對我的問題做什麼 – ZhiZha
可能重複的[Jtable不刷新/更新數據](http://stackoverflow.com/questions/12646240/jtable-doesnt-refresh-更新數據) – kleopatra