编辑整个帖子
我的问题是:此代码中的内容可以导致<BookDetail>
?
以及如何避免这种情况? IndexOutOfBoundsException
如下所示,当您点击ErrorStackTrace
按钮15-20次时,您可以获得相同的效果。我的问题是否真的与Fetch
有关?如果可以,请解释我。谢谢。
编辑,希望这次我做对了
convertRowIndexToModel()
我的import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.table.TableStringConverter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
GoodsView goodsView = new GoodsView();
goodsView.setLocationRelativeTo(null);
goodsView.setVisible(true);
}
});
}
}
class GoodsView extends JFrame {
private JTable table;
private List<GoodsPosition> positions;
private List<List<Object>> tempList;
private GoodsViewTableModel model;
private JButton button;
private TableUtils tableUtils;
private JTextField textField = new JTextField(15);
public GoodsView() {
positions = new ArrayList<>(25);
tempList = new ArrayList<>(25);
tableUtils = new TableUtils();
initGUI();
setSize(600, 400);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
private void initGUI() {
Container container = getContentPane();
container.setLayout(new BorderLayout());
container.add(createTableView(), BorderLayout.CENTER);
container.add(createButtonPanel(), BorderLayout.NORTH);
}
private JScrollPane createTableView() {
model = new GoodsViewTableModel(positions);
table = new JTable(model);
table.setRowSorter(tableUtils.createSorter(model, textField));
return new JScrollPane(table);
}
private JPanel createButtonPanel() {
button = new JButton(new AbstractAction("Fetch") {
@Override
public void actionPerformed(ActionEvent e) {
button.setEnabled(false);
runGoodsFetcher();
}
});
JPanel panel = new JPanel(new BorderLayout(5, 5));
panel.add(button, BorderLayout.EAST);
panel.add(textField, BorderLayout.WEST);
return panel;
}
private void runGoodsFetcher() {
new SwingWorker<Void, GoodsPosition>() {
String id, art;
@Override
protected Void doInBackground() throws Exception {
if (!tempList.isEmpty()) {
tempList.clear();
}
if (!positions.isEmpty()) {
positions.clear();
}
tempList = executeQuery();
for (List<Object> tempListItem : tempList) {
id = tempListItem.get(0) == null ? "" : tempListItem.get(0).toString();
art = tempListItem.get(1) == null ? "" : tempListItem.get(1).toString();
//Thread.sleep(5);
GoodsPosition position = new GoodsPosition(id, art);
publish(position);
}
return null;
}
@Override
protected void process(List<GoodsPosition> pos) {
model.addPositions(pos);
}
@Override
public void done() {
button.setEnabled(true);
}
}.execute();
}
public List<List<Object>> executeQuery() {
List<List<Object>> rows = new ArrayList<>();
for (int column = 0; column < 100000; column++) {
List<Object> newRow = new ArrayList<>();
for (int i = 0; i < 2; i++) {
newRow.add(0, column);
newRow.add(1, column + 1);
}
rows.add(newRow);
}
return rows;
}
private class GoodsViewTableModel extends AbstractTableModel {
private String[] columnNames =
{"id", "art"};
private List<GoodsPosition> goodsPositions;
GoodsViewTableModel(List<GoodsPosition> goodsPositions) {
this.goodsPositions = goodsPositions;
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public String getColumnName(int column) {
return columnNames[column];
}
@Override
public int getRowCount() {
return goodsPositions.size();
}
@Override
public Class<?> getColumnClass(int column) {
return String.class;
}
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
@Override
public Object getValueAt(int row, int column) {
GoodsPosition position = getPosition(row);
switch (column) {
case 0:
return position.getId();
case 1:
return position.getArt();
default:
return null;
}
}
@Override
public void setValueAt(Object value, int row, int column) {
GoodsPosition position = getPosition(row);
switch (column) {
case 0:
position.setId((String) value);
break;
case 1:
position.setArt((String) value);
break;
}
fireTableCellUpdated(row, column);
}
public GoodsPosition getPosition(int row) {
return goodsPositions.get(row);
}
public void addPosition(GoodsPosition position) {
insertPosition(getRowCount(), position);
}
public void addPositions(List<GoodsPosition> positionsList) {
int rowCount = getRowCount();
goodsPositions.addAll(positionsList);
fireTableRowsInserted(rowCount, getRowCount() - 1);
}
public void insertPosition(int row, GoodsPosition position) {
goodsPositions.add(row, position);
fireTableRowsInserted(row, row);
}
public void removePosition(int row) {
goodsPositions.remove(row);
fireTableRowsDeleted(row, row);
}
public void removeAllPositions() {
if (!goodsPositions.isEmpty()) {
goodsPositions.clear();
}
}
}
public class GoodsPosition {
private String oraId;
private String art;
public GoodsPosition(String oraId, String art) {
this.oraId = oraId;
this.art = art;
}
public String getId() {
return oraId;
}
public void setId(String oraId) {
this.oraId = oraId;
}
public String getArt() {
return art;
}
public void setArt(String art) {
this.art = art;
}
}
private class TableUtils {
private TableRowSorter<TableModel> createSorter(TableModel model, JTextField field) {
TableRowSorter<TableModel> sorter = new TableRowSorter<>(model);
sorter.setStringConverter(new TableStringConverter() {
@Override
public String toString(TableModel model, int row, int column) {
return model.getValueAt(row, column).toString().toLowerCase();
}
});
field.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
String searchText = field.getText().toLowerCase();
if (searchText.trim().length() == 0) {
sorter.setRowFilter(null);
} else {
sorter.setRowFilter(RowFilter.regexFilter(searchText));
}
}
@Override
public void removeUpdate(DocumentEvent e) {
String searchText = field.getText().toLowerCase();
if (searchText.trim().length() == 0) {
sorter.setRowFilter(null);
} else {
sorter.setRowFilter(RowFilter.regexFilter(searchText));
}
}
@Override
public void changedUpdate(DocumentEvent e) {
String searchText = field.getText().toLowerCase();
if (searchText.trim().length() == 0) {
sorter.setRowFilter(null);
} else {
sorter.setRowFilter(RowFilter.regexFilter(searchText));
}
}
});
return sorter;
}
}
}
ErrorStackTrace
再次感谢大家提前。如果我违反任何发布或破坏任何规则,我很抱歉。