当前位置: 技术问答>java相关
大家好,我是JAVA新手,请教大家一个关于JTable的问题,谢谢!
来源: 互联网 发布时间:2015-08-24
本文导语: 我用Vector变量来存储JTable的元素,我想请教大家的是如何在添加新元素之后,JTable动态显示表格更新,原小程序如下: import java.awt.*; import java.sql.*; import java.util.*; import java.awt.event.*; import javax.swing.*; import javax.swin...
我用Vector变量来存储JTable的元素,我想请教大家的是如何在添加新元素之后,JTable动态显示表格更新,原小程序如下:
import java.awt.*;
import java.sql.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import myjava.MyWindowListener;
public class TableRefresh extends JFrame implements ActionListener
{
protected JTable recordTable;
private JButton addButton;
private JTextField nameField,introduceField;
private Vector rowVector,colVector;
private JPanel tablePanel,inputPanel;
private FlowLayout layout=new FlowLayout(FlowLayout.CENTER);
private JScrollPane scrollPane;
public TableRefresh()
{
super("测试");
colVector=new Vector();
colVector.addElement("名称");
colVector.addElement("介绍");
rowVector=new Vector();
recordTable=new JTable(rowVector,colVector);
recordTable.setPreferredScrollableViewportSize(new Dimension(200, 100));
TableColumn column = null;
column=recordTable.getColumnModel().getColumn(0);
column.setPreferredWidth(100);
column=recordTable.getColumnModel().getColumn(1);
column.setPreferredWidth(200);
tablePanel=new JPanel(layout,true);
scrollPane=new JScrollPane(recordTable);
tablePanel.add(scrollPane);
nameField=new JTextField(20);
introduceField=new JTextField(30);
addButton=new JButton("添加");
inputPanel=new JPanel(new FlowLayout(FlowLayout.CENTER));
inputPanel.add(nameField);
inputPanel.add(introduceField);
inputPanel.add(addButton);
Container c=getContentPane();
addButton.addActionListener(this);
c.setLayout(new BorderLayout());
c.add(tablePanel,BorderLayout.NORTH);
c.add(inputPanel,BorderLayout.CENTER);
setSize(400,300);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
Vector cache=new Vector();
cache.addElement(nameField.getText());
cache.addElement(introduceField.getText());
rowVector.addElement(cache);
}
public static void main(String[] args)
{
TableRefresh app=new TableRefresh();
app.addWindowListener(new MyWindowListener());
}
};
希望众JAVA高手能帮我解决这个问题,每次在两行文本行输入新数据,按添加后能在表格中立即显示出来,再次谢谢了
import java.awt.*;
import java.sql.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import myjava.MyWindowListener;
public class TableRefresh extends JFrame implements ActionListener
{
protected JTable recordTable;
private JButton addButton;
private JTextField nameField,introduceField;
private Vector rowVector,colVector;
private JPanel tablePanel,inputPanel;
private FlowLayout layout=new FlowLayout(FlowLayout.CENTER);
private JScrollPane scrollPane;
public TableRefresh()
{
super("测试");
colVector=new Vector();
colVector.addElement("名称");
colVector.addElement("介绍");
rowVector=new Vector();
recordTable=new JTable(rowVector,colVector);
recordTable.setPreferredScrollableViewportSize(new Dimension(200, 100));
TableColumn column = null;
column=recordTable.getColumnModel().getColumn(0);
column.setPreferredWidth(100);
column=recordTable.getColumnModel().getColumn(1);
column.setPreferredWidth(200);
tablePanel=new JPanel(layout,true);
scrollPane=new JScrollPane(recordTable);
tablePanel.add(scrollPane);
nameField=new JTextField(20);
introduceField=new JTextField(30);
addButton=new JButton("添加");
inputPanel=new JPanel(new FlowLayout(FlowLayout.CENTER));
inputPanel.add(nameField);
inputPanel.add(introduceField);
inputPanel.add(addButton);
Container c=getContentPane();
addButton.addActionListener(this);
c.setLayout(new BorderLayout());
c.add(tablePanel,BorderLayout.NORTH);
c.add(inputPanel,BorderLayout.CENTER);
setSize(400,300);
setVisible(true);
}
public void actionPerformed(ActionEvent ae)
{
Vector cache=new Vector();
cache.addElement(nameField.getText());
cache.addElement(introduceField.getText());
rowVector.addElement(cache);
}
public static void main(String[] args)
{
TableRefresh app=new TableRefresh();
app.addWindowListener(new MyWindowListener());
}
};
希望众JAVA高手能帮我解决这个问题,每次在两行文本行输入新数据,按添加后能在表格中立即显示出来,再次谢谢了
|
public void actionPerformed(ActionEvent ae)
{
Vector cache=new Vector();
cache.addElement(nameField.getText());
cache.addElement(introduceField.getText());
rowVector.addElement(cache);
((DefaultTableModel)recordTable.getModel()).addRow(Vector rowData) ;
}
{
Vector cache=new Vector();
cache.addElement(nameField.getText());
cache.addElement(introduceField.getText());
rowVector.addElement(cache);
((DefaultTableModel)recordTable.getModel()).addRow(Vector rowData) ;
}