Reputation: 51
I want to refresh two dimensional array table in my JFrame
with ActionListener
. Here is some codes from my program:
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. ");
}
As you see, I have tried repaint()
and tableModel.fireTableDataChanged()
and both of them didn't work. What should I do to refresh my table? And also here is a picture:
It doesn't refresh:
Upvotes: 0
Views: 534
Reputation: 285405
Your question is impossible to answer precisely since you haven't posted a valid MCVE for us to compile and run, but it looks like you're trying to change the state of a collection that was used to initialize a JTable's model without changing the actual model itself.
Having said that, to remove all the data from a JTable, remove all the rows. If you're using a DefaultTableModel, then this means simply calling .setRowCount(0)
on the model.
Note that there is no need to call repaint()
in this situation since by changing the model, repaint is automatically called. Also your code shouldn't even attempt to call fireTableDataChanged()
on the model, since this type of call should be called internally by the model itself only.
For example, my 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);
}
}
}
Upvotes: 1