我现在做的东西就是:在一个界面中有一个按钮,点这个按钮,出现在一个JFileChooser对话框,然后我随便选择一个文件;我现在想做的就是想把这个文件的文件名放到JTable中,我想动态的增加,以后我再选择一个文件时又可以增加到JTable中去
这个怎么做呀~~
我看了很多例子,那JTable中的数据都是预先定义好了的,我想动态增加,请问各位高手给小弟一点建议,指点一下,谢谢~~

解决方案 »

  1.   

    在TableModel中提供添加方法就行了,
    http://community.csdn.net/Expert/topic/4707/4707075.xml?temp=.7663233
      

  2.   

    JTable中的数据主要是由列名,和一个表示数据的二维数组确定的,你要添加数据的话,可以先将这个表格释放掉,然后改变二维数组的值,再重新创建
      

  3.   

    不要尝试插到jtable里面只要把数据插入到tablemodel里面,jtable就自动更新了
      

  4.   

    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;import javax.swing.*;
    import javax.swing.table.*;/**
     * JTable的例子
     */
    public class Test {
    private JFrame frame = null; private JTable table = null; private Table_Model model = null; private JScrollPane s_pan = null; private JButton button_1 = null, button_2 = null; public Test() {
    frame = new JFrame("Test");
    button_1 = new JButton("清除现有的数据");
    button_1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    removeData();
    }
    });
    button_2 = new JButton("再次添加文件");
    button_2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    JFileChooser f = new JFileChooser();
    f.showOpenDialog(frame);
    addData(f.getSelectedFile().getPath(), f.getSelectedFile().getName());
    }
    });
    model = new Table_Model(20);
    table = new JTable(model);
    table.setBackground(Color.white); s_pan = new JScrollPane(table); frame.getContentPane().add(s_pan, BorderLayout.CENTER);
    frame.getContentPane().add(button_1, BorderLayout.NORTH);
    frame.getContentPane().add(button_2, BorderLayout.SOUTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(300, 200);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true); } private void addData(String path, String name) {
    model.addRow(path, name);
    table.updateUI(); // 刷新,将改变后的内容显示出来
    } private void removeData() {
    model.removeRows(0, model.getRowCount());
    table.updateUI(); // 刷新,将改变后的内容显示出来
    } public static void main(String args[]) {
    new Test();
    }}class Table_Model extends AbstractTableModel { private Vector content = null; private String[] title_name = { "ID", "路径", "文件" }; public Table_Model() {
    content = new Vector();
    } public Table_Model(int count) {
    content = new Vector(count);
    } public void addRow(String path, String file) {
    Vector v = new Vector(4);
    v.add(0, new Integer(content.size()));
    v.add(1, path);
    v.add(2, file);
    content.add(v);
    } public void removeRow(int row) {
    content.remove(row);
    } public void removeRows(int row, int count) {
    for (int i = 0; i < count; i++) {
    if (content.size() > row) {
    content.remove(row);
    }
    }
    } public String getColumnName(int col) {
    return title_name[col];
    } public int getColumnCount() {
    return title_name.length;
    } public int getRowCount() {
    return content.size();
    } public Object getValueAt(int row, int col) {
    return ((Vector) content.get(row)).get(col);
    }}
      

  5.   

    写一个类extends AbstractTableModel,例:
    ArayList listData = new ArrayList();
    public void addData(Object yourObject)
    {
       //对yourObject 或 listData的出错判断
       listData.add(yourObject);
       fireTableDataChanged();   
    }
    ...
    public Object getValueAt(int row, int col) 
    {
       //对yourObject 或 listData的出错判断
       yourObject obj = (yourObject)listData.get(row);
       if(col == 0)
         obj.getFileName();
       else if(col == 1)
         obj.getXXX();
       else if(col == 2)
         obj.getXXX();
       ...
    }//yourObject 自写的一个数据封装类,喜欢添加什么方法就添加什么方法。主要是针对属性多了,容易处理,如果你只有几个属性那就看你方便吧。