行选择后的JTable更新

时间:2013-12-07 16:37:20

标签: java swing jtable

以下是JTextField的自选完成JTable的SSCCE。

它可以正常工作,直到我想在选择行时执行某些操作。问题是,只要选择了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 个答案:

答案 0 :(得分:1)

问题是当选择表行(或col / cell)时,选择事件会被触发两次。第一次使用索引-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时可以忽略列表选择事件。事实上,使用此功能优于上述功能,因为它使动作代码更具体。