gpt4 book ai didi

java - 行选择后 JTable 更新

转载 作者:行者123 更新时间:2023-12-01 13:44:39 26 4
gpt4 key购买 nike

这是一个 JTextField 的 SSCCE,它自动完成 JTable

它工作得很好,直到我想在选择一行时执行某些操作。问题是,每当选择 JTable 中的一行时,更改 JTextField 文本都会引发异常:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.Vector.elementAt(Vector.java:430)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:632)
at javax.swing.JTable.getValueAt(JTable.java:2681)
at fr.ensicaen.si.client.AnimalAutoComplete$1.valueChanged(AnimalAutoComplete.java:73)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:167)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:147)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:194)
at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:388)
at javax.swing.DefaultListSelectionModel.changeSelection(DefaultListSelectionModel.java:398)
at javax.swing.DefaultListSelectionModel.removeSelectionIntervalImpl(DefaultListSelectionModel.java:559)
at javax.swing.DefaultListSelectionModel.clearSelection(DefaultListSelectionModel.java:403)
at javax.swing.JTable.clearSelection(JTable.java:2075)
at fr.ensicaen.si.client.AnimalAutoComplete$AutoCompleteListener.fill(AnimalAutoComplete.java:97)
at fr.ensicaen.si.client.AnimalAutoComplete$AutoCompleteListener.insertUpdate(AnimalAutoComplete.java:93)

事实上,选择一行并执行某些操作是有效的。但是,当修改 JTextField 时,ListSelectionListener 再次被触发(但不应该?),我不明白为什么!

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import java.awt.GridLayout;

public class AnimalAutoComplete extends JFrame {

private JPanel contentPane;
private JScrollPane animalPane;
private JTable animalTable;
private JPanel panel;
private JTextField animalField;

/** Create the frame. */
public AnimalAutoComplete() {
/* Frame structure */
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 710, 471);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
animalPane = new JScrollPane();
contentPane.add(animalPane, BorderLayout.CENTER);
panel = new JPanel();
contentPane.add(panel, BorderLayout.NORTH);
panel.setLayout(new GridLayout(1, 0, 0, 0));

/* JTable model */
DefaultTableModel animalModel = new DefaultTableModel();
animalTable = new JTable(animalModel);
animalPane.setViewportView(animalTable);
animalModel.addColumn("Animal");

/* Initially fills the JTable */
List<String> animals = new ArrayList<String>();
animals.add("Dog");
animals.add("Cat");
animals.add("Fish");

for (String a : animals) {
animalModel.addRow( new Object[] {a} );
}

/* Text fields */
animalField = new JTextField();
panel.add(animalField);
animalField.setColumns(10);

/* This listener updates the JTable depending on the JTextField */
animalField.getDocument().addDocumentListener(new AutoCompleteListener(animalTable, animals, animalField));

/* This listener will do something useful when an animal is selected*/
animalTable.getSelectionModel().addListSelectionListener(
new ListSelectionListener(){
public void valueChanged(ListSelectionEvent event) {
System.out.println(animalTable.getValueAt(animalTable.getSelectedRow(), 0));
}
}
);

}

private final class AutoCompleteListener implements DocumentListener {
private final JTable animalTable;
private final List<String> animals;
private JTextField searchedAnimal;

private AutoCompleteListener(JTable animalTable,
List<String> animals, JTextField textAnimal) {
this.animalTable= animalTable;
this.animals = animals;
this.searchedAnimal = textAnimal;
}

public void changedUpdate(DocumentEvent arg0) {fill();}
public void insertUpdate(DocumentEvent arg0) {fill();}
public void removeUpdate(DocumentEvent arg0) {fill();}

public void fill() {
animalTable.clearSelection();
DefaultTableModel model = (DefaultTableModel) animalTable.getModel();
model.setRowCount(0);
for (String a : animals) {
if (a.startsWith(this.searchedAnimal.getText())) {
model.addRow(new Object[] {a});
}
}
}
}

public static void main(String[] args) {
AnimalAutoComplete frame = new AnimalAutoComplete();
frame.setVisible(true);
}
}

最佳答案

问题是当选择表格行(或列/单元格)时,选择事件会被触发两次。第一次使用索引 -1,第二次使用正确的索引。因此,请检查 animalTable.getSelectedRow() 是否返回 -1 的条件:

        public void valueChanged(ListSelectionEvent event) {
int selRow = animalTable.getSelectedRow()
if(selRow >= 0)
{
System.out.println(animalTable.getValueAt(selRow, 0));
// or do other things
}
}

正如 @camickr 在下面所建议的,您还可以使用 event.getValueIsAdjusting()方法:如果选择仍在更改,则返回 true。许多列表选择监听器只对选择的最终状态感兴趣,并且当此方法返回 true 时可以忽略列表选择事件。事实上,使用此函数比上面提到的函数更可取,因为它使操作代码更加具体。

关于java - 行选择后 JTable 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20443764/

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