100分,求jtable数据更新!!!!!!!!!!!!!!!!!!!!!!!!!!!
如JTabletable=newJTable(a,b);....table.set(newDefaultTableModel(srcVector,cVector));当a...
如JTable table=new JTable(a,b);....
table.set(new DefaultTableModel(srcVector,cVector));
当a的中数据发生变化时怎么让表格更新数据?QQ158961055,大家帮帮忙,谢了
通过a,b传值到srcVector cVector中去 展开
table.set(new DefaultTableModel(srcVector,cVector));
当a的中数据发生变化时怎么让表格更新数据?QQ158961055,大家帮帮忙,谢了
通过a,b传值到srcVector cVector中去 展开
3个回答
展开全部
大概的思路如下:
1.利用Vector对象a b,创建DefaultTableModel对象(表格模型对象);
2.将该表格模型对象赋予给表格(jTable);
3.获取表格的表格模型、并利用其.fireTableStructureChanged()方法进行跟新显示。
我帮你编了一个最简单的例子,你看看就明白了(重点看按钮单击事件那里的代码):
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JTable;
import java.awt.Rectangle;
import javax.swing.JButton;
import java.util.*;
import javax.swing.table.*;
public class MyTable extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JTable jTable = null;
private JButton jButton = null;
/**
* This is the default constructor
*/
public MyTable() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(365, 286);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJTable(), null);
jContentPane.add(getJButton(), null);
}
return jContentPane;
}
/**
* This method initializes jTable
*
* @return javax.swing.JTable
*/
private JTable getJTable() {
if (jTable == null) {
jTable = new JTable();
jTable.setBounds(new Rectangle(17, 18, 284, 88));
}
return jTable;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(91, 163, 113, 36));
jButton.setText("更新表格数据");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
Vector v_head=new Vector();
v_head.add("学号");
v_head.add("名字");
Vector stud1=new Vector();
stud1.add("001");
stud1.add("junjie");
Vector stud2=new Vector();
stud2.add("002");
stud2.add("Conny");
Vector studs=new Vector();
studs.add(stud1);
studs.add(stud2);
DefaultTableModel tm=new DefaultTableModel(studs,v_head);
jTable.setModel(tm);
((DefaultTableModel)jTable.getModel()).fireTableStructureChanged();
}
});
}
return jButton;
}
public static void main(String args[]){
new MyTable();
}
} // @jve:decl-index=0:visual-constraint="10,10"
1.利用Vector对象a b,创建DefaultTableModel对象(表格模型对象);
2.将该表格模型对象赋予给表格(jTable);
3.获取表格的表格模型、并利用其.fireTableStructureChanged()方法进行跟新显示。
我帮你编了一个最简单的例子,你看看就明白了(重点看按钮单击事件那里的代码):
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import javax.swing.JTable;
import java.awt.Rectangle;
import javax.swing.JButton;
import java.util.*;
import javax.swing.table.*;
public class MyTable extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JTable jTable = null;
private JButton jButton = null;
/**
* This is the default constructor
*/
public MyTable() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(365, 286);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
System.exit(0);
}
});
this.setVisible(true);
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJTable(), null);
jContentPane.add(getJButton(), null);
}
return jContentPane;
}
/**
* This method initializes jTable
*
* @return javax.swing.JTable
*/
private JTable getJTable() {
if (jTable == null) {
jTable = new JTable();
jTable.setBounds(new Rectangle(17, 18, 284, 88));
}
return jTable;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(91, 163, 113, 36));
jButton.setText("更新表格数据");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
Vector v_head=new Vector();
v_head.add("学号");
v_head.add("名字");
Vector stud1=new Vector();
stud1.add("001");
stud1.add("junjie");
Vector stud2=new Vector();
stud2.add("002");
stud2.add("Conny");
Vector studs=new Vector();
studs.add(stud1);
studs.add(stud2);
DefaultTableModel tm=new DefaultTableModel(studs,v_head);
jTable.setModel(tm);
((DefaultTableModel)jTable.getModel()).fireTableStructureChanged();
}
});
}
return jButton;
}
public static void main(String args[]){
new MyTable();
}
} // @jve:decl-index=0:visual-constraint="10,10"
展开全部
不太明白你的意思
只是在程序中a的值变化的话,table重新set一下就是了
在页面中变化的话,每次onchange时触发action,进行数据更新就行
只是在程序中a的值变化的话,table重新set一下就是了
在页面中变化的话,每次onchange时触发action,进行数据更新就行
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
table.updateUI(); //在table中显示增加的内容
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询