gpt4 book ai didi

java - JTable 自动建议抛出 .IllegalComponentStateException

转载 作者:太空宇宙 更新时间:2023-11-04 12:40:29 24 4
gpt4 key购买 nike

我已经为 JTextField 实现了 AutoSuggest。它对于 JTextField 工作正常。但是当我尝试将它用于 JTable CellEditor 时。它抛出了

Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location
at java.awt.Component.getLocationOnScreen_NoTreeLock(Component.java:2048)
at java.awt.Component.getLocationOnScreen(Component.java:2022)
at javax.swing.JPopupMenu.show(JPopupMenu.java:942)

AutoSuggestTable.java

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import javax.swing.AbstractAction;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.plaf.basic.BasicComboPopup;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

/**
*
* @author Phystem
* @param <T>
*/

public class AutoSuggestTable<T> extends JTextField {

private List<T> items;

private final JComboBox combobox = new JComboBox();

private final DefaultComboBoxModel comboBoxModel;

private final BasicComboPopup suggestPopup;

public AutoSuggestTable() {
this(new ArrayList<T>());
}

public AutoSuggestTable(List<T> items) {
this.items = items;
Collections.sort(items, new Compare());
comboBoxModel = new DefaultComboBoxModel();
combobox.setModel(comboBoxModel);
suggestPopup = new BasicComboPopup(combobox);
install();
}

private void addToSuggestList(List<Object> list) {
comboBoxModel.removeAllElements();
if (list.isEmpty()) {
suggestPopup.hide();
} else {
for (Object elm : list) {
comboBoxModel.addElement(elm);
}
suggestPopup.show(this, 0, getHeight());
suggestPopup.setPopupSize(getWidth(), suggestPopup.getHeight());
}
}

public void hidePopup() {
suggestPopup.hide();
}

/**
* The value to search for I'm checking for case insensitive contains Modify
* it if you want
*
* @param value
*/
private void filter(String value) {
List<Object> tempList = new ArrayList<>();
for (Object item : items) {
if (item.toString().toLowerCase().contains(value.toLowerCase())) {
tempList.add(item);
}
}
addToSuggestList(tempList);
}

public List<T> getItems() {
return items;
}

public void withItems(List<T> items) {
this.items = items;
Collections.sort(items, new Compare());
}

public void addItem(T item) {
items.add(item);
}

public void clearItems() {
items.clear();
}

private void install() {
setBorder(null);
setFocusTraversalKeysEnabled(false);
getDocument().addDocumentListener(new DocumentListener() {

@Override
public void insertUpdate(DocumentEvent e) {
onTextChange(e);
}

@Override
public void removeUpdate(DocumentEvent e) {
onTextChange(e);
}

@Override
public void changedUpdate(DocumentEvent e) {
onTextChange(e);
}

});
getInputMap().put(KeyStroke.getKeyStroke("ENTER"), "commit");
getActionMap().put("commit", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
setSelectedItemFromList();
}
});
getInputMap().put(KeyStroke.getKeyStroke("DOWN"), "traversedown");
getActionMap().put("traversedown", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
traversedown();
}
});
getInputMap().put(KeyStroke.getKeyStroke("UP"), "traverseup");
getActionMap().put("traverseup", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
traverseup();
}
});
}

private void setSelectedItemFromList() {
if (comboBoxModel.getSize() > 0) {
setText(suggestPopup.getList().getSelectedValue().toString());
suggestPopup.hide();
}
}

private void traverseup() {
if (comboBoxModel.getSize() > 0) {
int index = suggestPopup.getList().getSelectedIndex() - 1;
if (index >= 0) {
suggestPopup.getList().setSelectedIndex(index);
suggestPopup.getList().ensureIndexIsVisible(index);
}
}
}

private void traversedown() {
if (comboBoxModel.getSize() > 0) {
int index = suggestPopup.getList().getSelectedIndex() + 1;
if (index < comboBoxModel.getSize()) {
suggestPopup.getList().setSelectedIndex(index);
suggestPopup.getList().ensureIndexIsVisible(index);
}
}
}

private void onTextChange(DocumentEvent e) {
Document source = e.getDocument();
int length = source.getLength();
try {
filter(source.getText(0, length));
} catch (BadLocationException be) {
System.out.println("Contents: Unknown");
}
}

private static class Compare implements Comparator {

@Override
public int compare(Object o1, Object o2) {
return Objects.toString(o1, "").compareTo(Objects.toString(o2, ""));
}

}
}

MCVE

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultCellEditor;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
*
* @author Phystem
*/
public class TestClass {

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

@Override
public void run() {
JFrame frame = new JFrame("AutoSuggest Test");
JTable table = getTable();
AutoSuggestTable autoSuggest = new AutoSuggestTable(getSampleList());
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JScrollPane(table), BorderLayout.CENTER);
panel.add(autoSuggest, BorderLayout.NORTH);
frame.add(panel);
table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(autoSuggest));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}

private static List<String> getSampleList() {
List<String> list = new ArrayList<>();
list.add("Apples");
list.add("Bananas");
list.add("Grapes");
list.add("Oranges");
list.add("PineApples");
list.add("Watermelon");
list.add("Lemon");
return list;
}

private static JTable getTable() {
DefaultTableModel model = new DefaultTableModel();
model.addColumn("Test1");
model.addColumn("Test2");
for (int i = 0; i < 5; i++) {
model.addRow(new Object[]{"xx" + i, "yy" + i});
}
JTable table = new JTable(model);
return table;
}
}

对于 JTable 它在第一次编辑时抛出上述错误,在第二次编辑时工作正常。我的意思是,如果我双击第一列,它会抛出该错误,但当我再次双击时,它会显示自动建议。

最佳答案

我已经使用了isShowing在显示弹出窗口之前检查编辑器是否可见的方法,它解决了问题。

      if (isShowing()) {
suggestPopup.show(this, 0, getHeight());
suggestPopup.setPopupSize(getWidth(), suggestPopup.getHeight());
}

还有一些修改需要完成。如果有人想要的话,这里是 Gist

关于java - JTable 自动建议抛出 .IllegalComponentStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36880062/

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