java-如何在运行时更新jtable?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-如何在运行时更新jtable?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2508字,纯文字阅读大概需要4分钟。
内容图文
![java-如何在运行时更新jtable?](/upload/InfoBanner/zyjiaocheng/656/4305f441589546b3b0d53e0b2fb1db13.jpg)
我是JTable的新手.
我想在运行时在按钮按下事件中更新jtable数据.
这是我的代码.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.*;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
public class SetEditableTableCell extends JPanel {
JPanel top = new JPanel();
JPanel bottom = new JPanel();
JButton update = new JButton("Update");
JTable table;
Vector<String> rowOne;
Vector<String> rowTwo;
DefaultTableModel tablemodel;
public SetEditableTableCell() {
this.setLayout(new BorderLayout());
rowOne = new Vector<String>();
rowOne.addElement("Row1-1");
rowOne.addElement("Row1-2");
rowOne.addElement("Row1-3");
rowTwo = new Vector<String>();
rowTwo.addElement("Row2-2");
rowTwo.addElement("Row2-3");
rowTwo.addElement("Row2-4");
Vector<Vector> rowData = new Vector<Vector>();
rowData.addElement(rowOne);
rowData.addElement(rowTwo);
Vector<String> columnNames = new Vector<String>();
columnNames.addElement("Column One");
columnNames.addElement("Column Two");
columnNames.addElement("Column Three");
tablemodel = new DefaultTableModel(rowData, columnNames);
table = new JTable(tablemodel);
// table.setValueAt("aa", 0, 0);
update.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
updatedata();
}
private void updatedata() {
rowOne = new Vector<String>();
rowOne.addElement("updated Row1-1");
rowOne.addElement("updated Row1-2");
rowOne.addElement("updated Row1-3");
rowTwo = new Vector<String>();
rowTwo.addElement("updated Row2-2");
rowTwo.addElement("updated Row2-3");
rowTwo.addElement("updated Row2-4");
// tablemodel.addRow(rowTwo);
tablemodel.fireTableDataChanged();
table.setModel(tablemodel);
System.out.println("button pressed");
// table.setValueAt("aa", 0, 0);
}
});
JScrollPane scrollPane = new JScrollPane(table);
top.add(scrollPane);
bottom.add(update);
add(top, BorderLayout.NORTH);
add(bottom, BorderLayout.SOUTH);
}
public static void main(String args[]) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SetEditableTableCell obj = new SetEditableTableCell();
frame.add(obj);
//frame.setSize(400, 300);
frame.pack();
frame.setVisible(true);
}
}
但是按更新按钮后它不会更新.
谁能解决我的问题?
提前致谢..
解决方法:
它不会更新,因为您不修改TableModel中的值.通过在updateData方法中将新的Vector分配给rowOne和rowTwo,您将更改其他Vector实例,然后更改TableModel知道的实例.
一个可能的解决方案是重建数据向量并使用setDataVector方法
内容总结
以上是互联网集市为您收集整理的java-如何在运行时更新jtable?全部内容,希望文章能够帮你解决java-如何在运行时更新jtable?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。