gpt4 book ai didi

java - 使用自定义表模型从 JTable 中删除行

转载 作者:行者123 更新时间:2023-12-04 05:33:09 24 4
gpt4 key购买 nike

我有一个 JTable 的子类使用自定义表模型( AbstractTableModel 的实现)来管理数据。

当我尝试使用方法 deleteRow 删除一行时出现问题.我的表中的行被一个空字符串替换,但该行没有被删除。

这是代码:

public class LiveSearchTableModel extends AbstractTableModel 
{

private List<String> columnNames = new ArrayList<String>();
private Map<Point, Object> displayData = new HashMap<Point, Object>();
private Map<Point, Object> originalData = new HashMap<Point, Object>();

public LiveSearchTableModel(List<String> columnNames,
Map<Point, Object> tableData)
{
this.columnNames = columnNames;
this.displayData = tableData;
this.originalData.putAll(tableData);
}

@Override
public int getColumnCount() {
return columnNames.size();
}

@Override
public int getRowCount() {
return displayData.size() / columnNames.size();
}

@Override
public Object getValueAt(int rowIndex, int columnIndex) {
return displayData.get(new Point(rowIndex, columnIndex));
}

public void deleteRow (int row)
{
for (int cont = 0; cont < columnNames.size();cont++)
{
displayData.remove(new Point (row,cont));
}

this.fireTableRowsDeleted(row, row);
}

@Override
public void setValueAt(Object value, int rowIndex, int columnIndex)
{
this.displayData.put(new Point(rowIndex, columnIndex), value);
this.fireTableDataChanged();
}
}

最佳答案

sscce下面说明了一个潜在的问题:Map 的键不是有序的,而表的行是。在所示的方法中,必须随着数据的每次更改更新一组键。另见此相关 example .如果需要,您可以扩展 Point实现 Comparable ,如图 hereValue .

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

/**
* @see https://stackoverflow.com/q/12330167/230513
*/
public class TableModelTest extends JPanel {

public TableModelTest() {
super(new BorderLayout());
final MyModel model = new MyModel();
JTable table = new JTable(model);
this.add(new JScrollPane(table));
this.add(new JButton(new AbstractAction("Delete") {

@Override
public void actionPerformed(ActionEvent e) {
model.remove(0);
}
}), BorderLayout.SOUTH);
}

public class MyModel extends AbstractTableModel {

private List<String> names = new ArrayList<String>();
private Map<Point, Object> data = new HashMap<Point, Object>();
private Point[] keys;

public MyModel() {
this.names = Arrays.asList("Point", "Name");
data.put(new Point(1, 1), "One");
data.put(new Point(2, 2), "Two");
data.put(new Point(3, 3), "Three");
this.keys = data.keySet().toArray(new Point[data.size()]);
}

public void remove(int row) {
data.remove(keys[row]);
keys = data.keySet().toArray(new Point[data.size()]);
this.fireTableRowsDeleted(row, row);
}

@Override
public int getColumnCount() {
return names.size();
}

@Override
public String getColumnName(int col) {
return names.get(col);
}

@Override
public int getRowCount() {
return data.size();
}

@Override
public Object getValueAt(int row, int col) {
if (col == 0) {
return keys[row];
} else {
return data.get(keys[row]);
}
}
}

private void display() {
JFrame f = new JFrame("TableModelTest");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {

@Override
public void run() {
new TableModelTest().display();
}
});
}
}

关于java - 使用自定义表模型从 JTable 中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12330167/

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