gpt4 book ai didi

java - 删除一行后 JTable 未重新绘制

转载 作者:行者123 更新时间:2023-11-30 04:11:57 25 4
gpt4 key购买 nike

AdminFrame.java

package com.sorcerok;

import java.awt.Color;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.JTableHeader;

public class AdminFrame extends JFrame {

private JFrame frame;
private JPanel panel;
private JButton delete, change;
private JTable usersTable;
private static final int WIDTH = 510;
private static final int HEIGHT = 300;
private List<User> list;
private MyTableModel model;

public AdminFrame(JFrame frame) {
super("Просмотр пользователей");
setResizable(false);
this.frame = frame;
frame.setVisible(false);


panel = new JPanel();
frame.setResizable(false);
panel.setLayout(null);

delete = new JButton("Удалить");
delete.setBounds(310, 230, 121, 30);
change = new JButton("Изменить");
change.setBounds(50, 230, 121, 30);


list = new ArrayList<>(Authorisation.userMap.values());

model = new MyTableModel(list);

usersTable = new JTable(model);
JTableHeader header = usersTable.getTableHeader();
header.setForeground(Color.black);
header.setFont(new Font("Times New Roman", Font.BOLD, 20));
JScrollPane sp = new JScrollPane(usersTable);

sp.setBounds(0, 0, 500, 90);

panel.add(delete);
panel.add(change);
panel.add(sp);

add(panel);
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
}

public void dispose() {
frame.setVisible(true);
super.dispose();
}

public void deleteFromTable() {

delete.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
int row = usersTable.getSelectedRow();
if (getSelectedUser(row) != null) {
Authorisation.userMap.remove(getSelectedUser(row).getName());
list.remove(getSelectedUser(row));
System.out.println(list);
Registration.writeToFile();

model.removeRow(row);
}
}
});

}

public User getSelectedUser(int index) {
index = usersTable.getSelectedRow();
if (isSelectedUser(index)) {
String name = (String) usersTable.getValueAt(index, 1);
return getUser(name);
} else {
return null;
}
}

public boolean isSelectedUser(int index) {
if (index < 0) {
return false;
} else {
return true;
}
}

public User getUser(String name) {
for (User u : list) {
if (name.equals(u.getName())) {
return u;
}
}
return null;
}

public class MyTableModel extends AbstractTableModel {

private List<User> list;

private MyTableModel(List<User> list) {
this.list = list;
}

public void addTableModelListener(TableModelListener listener) {
}

public Class<?> getColumnClass(int columnIndex) {
return String.class;
}

public int getColumnCount() {
return 3;
}

public String getColumnName(int columnIndex) {

switch (columnIndex) {
case 0:
return "№";
case 1:
return "Логин";
case 2:
return "Пароль";
}
return "";
}

public int getRowCount() {
return list.size();
}

public Object getValueAt(int rowIndex, int columnIndex) {
User user = list.get(rowIndex);
switch (columnIndex) {
case 0:
return ++rowIndex;
case 1:
return user.getName();
case 2:
return user.getPassword();

}
return "";
}

public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}

public void removeTableModelListener(TableModelListener listener) {
}

public void setValueAt(Object value, int rowIndex, int columnIndex) {
}

public void removeRow(int row) {

fireTableRowsDeleted(row, row);
fireTableStructureChanged();
System.out.println("Deleted by AbstractTableModel Table Model");

}
}
}

并调用其他类中的方法

AdminFrame af = new AdminFrame(frame);
af.deleteFromTable();

当表中元素很多且有滚动时,即按删除一行时,该行立即从Arralist中删除,但并未从表中删除,如果使用滚动,该记录立即消失。没有此方法的相同功能 model.removeRow(row);出了什么问题?

最佳答案

您的表模型不支持TableModelListeners,这就是表无法观察模型变化的原因。

您的TableModel应该通知TableModelListeners当它发生变化时(当添加、删除或删除行时)刷新)。
当在 table 上设置模型时, table 会添加一个TableModelListener观察模型。一旦模型发生更改,它就会触发 TableModelEvent(通过在所有监听器上调用 tableChanged(TableModelEvent))。然后,JTable 将收到此通知并重新验证和重新绘制表内容以适应更改。

根据您的具体情况:您已重写 add|removeTableModelListener() 方法,因此无法注册监听器。只需删除模型上的这两个方法,然后让这些方法的 DefaultTableModel 实现来处理它即可。

关于java - 删除一行后 JTable 未重新绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19397607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com