栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在JTable中刷新数据我正在使用TableModel

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在JTable中刷新数据我正在使用TableModel

您已经完成了艰辛的工作。

首先,您是直接从实现的

TableModel
,其次您没有实现监听器的要求…

相反,尝试从相反扩展

AbstractTableModel
,后者已经包括侦听器注册和通知的实现。

您将需要提供一种方法,该方法将允许您向表模型添加一行。在此方法中,您需要使用该

fireTableRowsInserted
方法,该方法将通知使用该模型的任何表,已添加新行…

更新示例

这是非常非常非常基本的例子。唯一的目的是演示的使用

fireTableRowsInserted
。它使用Swing
Timer
每125毫秒添加一个新行,直到您杀死它;)

import java.awt.BorderLayout;import java.awt.EventQueue;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.ArrayList;import java.util.List;import javax.swing.Jframe;import javax.swing.JScrollPane;import javax.swing.JTable;import javax.swing.Timer;import javax.swing.UIManager;import javax.swing.UnsupportedLookAndFeelException;import javax.swing.table.AbstractTableModel;public class DynamicTable {    public static void main(String[] args) {        new DynamicTable();    }    public DynamicTable() {        EventQueue.invokeLater(new Runnable() { @Override public void run() {     try {         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());     } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {     }     final MyTableModel model = new MyTableModel();     JTable table = new JTable(model);     Jframe frame = new Jframe("Testing");     frame.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);     frame.setLayout(new BorderLayout());     frame.add(new JScrollPane(table));     frame.pack();     frame.setLocationRelativeTo(null);     frame.setVisible(true);     Timer timer = new Timer(125, new ActionListener() {         @Override         public void actionPerformed(ActionEvent e) {  model.addRow();         }     });     timer.start(); }        });    }    public class MyTableModel extends AbstractTableModel {        private List<String[]> rows;        public MyTableModel() { rows = new ArrayList<>(25);        }        @Override        public int getRowCount() { return rows.size();        }        @Override        public int getColumnCount() { return 4;        }        @Override        public Class<?> getColumnClass(int columnIndex) { return String.class;        }        @Override        public Object getValueAt(int rowIndex, int columnIndex) { String[] row = rows.get(rowIndex); return row[columnIndex];        }        public void addRow() { int rowCount = getRowCount(); String[] row = new String[getColumnCount()]; for (int index = 0; index < getColumnCount(); index++) {     row[index] = rowCount + "x" + index; } rows.add(row); fireTableRowsInserted(rowCount, rowCount);        }     }         }

用另一个例子更新

因为您的表模型是由其自己支持的

List
,所以它与工厂没有任何关系。它不知道何时添加或删除对象。这意味着您有责任更新模型:

public class MyTableModel extends AbstractTableModel {    private List<Staff> staffs;    public MyTableModel(List<Staff> staffs){        this.staffs = staffs;    }    @Override    public int getRowCount() {        return staffs.size();    }    @Override    public int getColumnCount() {        return 5;    }    public void add(Staff staff) {        int size = getSize();        staffs.add(staff);        fireTableRowsInserted(size, size);    }    public void remove(Staff staff) {        if (staffs.contains(staff) { int index = stafff.indexOf(staff); staffs.remove(staff); fireTableRowsDeleted(index, index);        }    }    @Override    public Object getValueAt(int rowIndex, int columnIndex) {        Staff staff = staffs.get(rowIndex);        switch (columnIndex){ case 0:     return staff.getName(); case 1:     return staff.getSurname(); case 2:     return staff.getDate(); case 3:     return staff.getPosition(); case 4:     return staff.getSalary();        }        return "";    }}

而你的

actionPerformed

@Overridepublic void actionPerformed(ActionEvent e) {    Staff staff = new Staff();    staff.setName(JOptionPane.showInputDialog("Enter First Name"));    staff.setSurname(JOptionPane.showInputDialog("Enter Second Name"));    staff.setDate(JOptionPane.showInputDialog("Enter Date"));    staff.setPosition(JOptionPane.showInputDialog("Enter Position"));    staff.setSalary(JOptionPane.showInputDialog("Enter Salary"));    try {        Factory.getInstance().getStaffDAO().addStaff(staff);        ((MyTableModel)model).add(staff);    } catch (SQLException e1) {        e1.printStackTrace();      }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/569267.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号