如何在 JFrame 中使用 ActionListener 刷新 JTable?
How do I refresh JTable with ActionListener in JFrame?
我想用 ActionListener
刷新 JFrame
中的二维数组 table。这是我程序中的一些代码:
public Students(){
.....
removeall = new JMenuItem("Remove All Students");
removeall.addActionListener(this);
.....
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(removeall)) {
removeAll();
table.repaint();
}
public void removeAll() {
for(int i=name1.size()-1;i>=0;i--)
{
lastname1.remove(i);
id1.remove(i);
quiz11.remove(i);
quiz21.remove(i);
midterm1.remove(i);
project1.remove(i);
final1.remove(i);
lettergrade1.remove(i);
average1.remove(i);
name1.remove(i);
tablemodel.removeRow(i);
}
JOptionPane.showMessageDialog(this,"All students are removed. ");
}
如您所见,我已经尝试了 repaint()
和 tableModel.fireTableDataChanged()
,但都没有用。我应该怎么做才能刷新我的 table?还有一张图片:
不刷新:
您的问题无法准确回答,因为您没有发布有效的 MCVE 供我们编译和 运行,但看起来您正在尝试更改 a 的状态用于在不更改实际模型本身的情况下初始化 JTable 模型的集合。
话虽如此,要从 JTable 中删除所有数据,请删除所有行。如果您使用的是 DefaultTableModel,那么这意味着只需在模型上调用 .setRowCount(0)
。
请注意,在这种情况下无需调用 repaint()
,因为通过更改模型,会自动调用重绘。此外,您的代码甚至不应该尝试在模型上调用 fireTableDataChanged()
,因为这种类型的调用只能由模型本身在内部调用。
比如我的MCVE:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Random;
import javax.swing.*;
import javax.swing.table.*;
public class TableExample {
private static void createAndShowGui() {
TablePanel tablePanel = new TablePanel();
JMenuItem fillTableItem = new JMenuItem(new FillTableAction(tablePanel));
JMenuItem clearTableItem = new JMenuItem(new ClearTableAction(tablePanel));
JMenu tableMenu = new JMenu("Table Actions");
tableMenu.setMnemonic(KeyEvent.VK_T);
tableMenu.add(fillTableItem);
tableMenu.add(clearTableItem);
JMenuBar menuBar = new JMenuBar();
menuBar.add(tableMenu);
JFrame frame = new JFrame("TableExample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(tablePanel);
frame.setJMenuBar(menuBar);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
@SuppressWarnings("serial")
class FillTableAction extends AbstractAction {
private TablePanel tablePanel;
public FillTableAction(TablePanel tablePanel) {
super("Fill Table");
putValue(MNEMONIC_KEY, KeyEvent.VK_F);
this.tablePanel = tablePanel;
}
@Override
public void actionPerformed(ActionEvent e) {
tablePanel.fillTable();
}
}
@SuppressWarnings("serial")
class ClearTableAction extends AbstractAction {
private TablePanel tablePanel;
public ClearTableAction(TablePanel tablePanel) {
super("Clear Table");
putValue(MNEMONIC_KEY, KeyEvent.VK_C);
this.tablePanel = tablePanel;
}
@Override
public void actionPerformed(ActionEvent e) {
tablePanel.clearTable();
}
}
@SuppressWarnings("serial")
class TablePanel extends JPanel {
private static final String[] COL_NAMES = { "Name", "Surname", "ID" };
private DefaultTableModel model = new DefaultTableModel(COL_NAMES, 0);
private JTable table = new JTable(model);
private Random random = new Random();
public TablePanel() {
setLayout(new BorderLayout());
add(new JScrollPane(table));
}
public void clearTable() {
model.setRowCount(0);
}
// fill it with random text and data
public void fillTable() {
int rows = random.nextInt(50) + 40;
for (int i = 0; i < rows; i++) {
StringBuilder sb = new StringBuilder();
char c = (char) ('A' + random.nextInt(26));
sb.append(c);
for (int j = 0; j < 4 + random.nextInt(3); j++) {
sb.append((char) ('a' + random.nextInt(26)));
}
String name = sb.toString();
sb.delete(0, sb.length());
sb.append((char) ('A' + random.nextInt(26)));
for (int j = 0; j < 6 + random.nextInt(4); j++) {
sb.append((char) ('a' + random.nextInt(26)));
}
String surName = sb.toString();
int id = random.nextInt(Integer.MAX_VALUE / 2);
Object[] rowData = { name, surName, id };
model.addRow(rowData);
}
}
}
我想用 ActionListener
刷新 JFrame
中的二维数组 table。这是我程序中的一些代码:
public Students(){
.....
removeall = new JMenuItem("Remove All Students");
removeall.addActionListener(this);
.....
}
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(removeall)) {
removeAll();
table.repaint();
}
public void removeAll() {
for(int i=name1.size()-1;i>=0;i--)
{
lastname1.remove(i);
id1.remove(i);
quiz11.remove(i);
quiz21.remove(i);
midterm1.remove(i);
project1.remove(i);
final1.remove(i);
lettergrade1.remove(i);
average1.remove(i);
name1.remove(i);
tablemodel.removeRow(i);
}
JOptionPane.showMessageDialog(this,"All students are removed. ");
}
如您所见,我已经尝试了 repaint()
和 tableModel.fireTableDataChanged()
,但都没有用。我应该怎么做才能刷新我的 table?还有一张图片:
不刷新:
您的问题无法准确回答,因为您没有发布有效的 MCVE 供我们编译和 运行,但看起来您正在尝试更改 a 的状态用于在不更改实际模型本身的情况下初始化 JTable 模型的集合。
话虽如此,要从 JTable 中删除所有数据,请删除所有行。如果您使用的是 DefaultTableModel,那么这意味着只需在模型上调用 .setRowCount(0)
。
请注意,在这种情况下无需调用 repaint()
,因为通过更改模型,会自动调用重绘。此外,您的代码甚至不应该尝试在模型上调用 fireTableDataChanged()
,因为这种类型的调用只能由模型本身在内部调用。
比如我的MCVE:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Random;
import javax.swing.*;
import javax.swing.table.*;
public class TableExample {
private static void createAndShowGui() {
TablePanel tablePanel = new TablePanel();
JMenuItem fillTableItem = new JMenuItem(new FillTableAction(tablePanel));
JMenuItem clearTableItem = new JMenuItem(new ClearTableAction(tablePanel));
JMenu tableMenu = new JMenu("Table Actions");
tableMenu.setMnemonic(KeyEvent.VK_T);
tableMenu.add(fillTableItem);
tableMenu.add(clearTableItem);
JMenuBar menuBar = new JMenuBar();
menuBar.add(tableMenu);
JFrame frame = new JFrame("TableExample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(tablePanel);
frame.setJMenuBar(menuBar);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
@SuppressWarnings("serial")
class FillTableAction extends AbstractAction {
private TablePanel tablePanel;
public FillTableAction(TablePanel tablePanel) {
super("Fill Table");
putValue(MNEMONIC_KEY, KeyEvent.VK_F);
this.tablePanel = tablePanel;
}
@Override
public void actionPerformed(ActionEvent e) {
tablePanel.fillTable();
}
}
@SuppressWarnings("serial")
class ClearTableAction extends AbstractAction {
private TablePanel tablePanel;
public ClearTableAction(TablePanel tablePanel) {
super("Clear Table");
putValue(MNEMONIC_KEY, KeyEvent.VK_C);
this.tablePanel = tablePanel;
}
@Override
public void actionPerformed(ActionEvent e) {
tablePanel.clearTable();
}
}
@SuppressWarnings("serial")
class TablePanel extends JPanel {
private static final String[] COL_NAMES = { "Name", "Surname", "ID" };
private DefaultTableModel model = new DefaultTableModel(COL_NAMES, 0);
private JTable table = new JTable(model);
private Random random = new Random();
public TablePanel() {
setLayout(new BorderLayout());
add(new JScrollPane(table));
}
public void clearTable() {
model.setRowCount(0);
}
// fill it with random text and data
public void fillTable() {
int rows = random.nextInt(50) + 40;
for (int i = 0; i < rows; i++) {
StringBuilder sb = new StringBuilder();
char c = (char) ('A' + random.nextInt(26));
sb.append(c);
for (int j = 0; j < 4 + random.nextInt(3); j++) {
sb.append((char) ('a' + random.nextInt(26)));
}
String name = sb.toString();
sb.delete(0, sb.length());
sb.append((char) ('A' + random.nextInt(26)));
for (int j = 0; j < 6 + random.nextInt(4); j++) {
sb.append((char) ('a' + random.nextInt(26)));
}
String surName = sb.toString();
int id = random.nextInt(Integer.MAX_VALUE / 2);
Object[] rowData = { name, surName, id };
model.addRow(rowData);
}
}
}