JTable 自动提示抛出 .IllegalComponentStateException
JTable Autosuggest Throwing .IllegalComponentStateException
我已经为 JTextField
实施了 AutoSuggest。它在 JTextField
上运行良好。但是当我尝试将它用于 JTable
时CellEditor.It 的投掷
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());
}
还有一些需要修改的地方done.If谁想要就在这里Gist
我已经为 JTextField
实施了 AutoSuggest。它在 JTextField
上运行良好。但是当我尝试将它用于 JTable
时CellEditor.It 的投掷
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());
}
还有一些需要修改的地方done.If谁想要就在这里Gist