private void updateSelection(Object element) { if (element != null) { selectElement(element, mySearchField.getText()); mySearchField.setForeground(Color.black); } else { mySearchField.setForeground(Color.red); } if (mySearchPopup != null) { mySearchPopup.setSize(mySearchPopup.getPreferredSize()); mySearchPopup.validate(); } fireStateChanged(); }
public SearchPopup(String initialString) { final Color foregroundColor = UIUtil.getToolTipForeground(); Color color1 = UIUtil.getToolTipBackground(); mySearchField = new SearchField(); final JLabel searchLabel = new JLabel(" " + UIBundle.message("search.popup.search.for.label") + " "); searchLabel.setFont(searchLabel.getFont().deriveFont(Font.BOLD)); searchLabel.setForeground(foregroundColor); mySearchField.setBorder(null); mySearchField.setBackground(color1.brighter()); mySearchField.setForeground(foregroundColor); mySearchField.setDocument( new PlainDocument() { public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { String oldText; try { oldText = getText(0, getLength()); } catch (BadLocationException e1) { oldText = ""; } String newText = oldText.substring(0, offs) + str + oldText.substring(offs); super.insertString(offs, str, a); if (findElement(newText) == null) { mySearchField.setForeground(Color.RED); } else { mySearchField.setForeground(foregroundColor); } } }); mySearchField.setText(initialString); setBorder(BorderFactory.createLineBorder(Color.gray, 1)); setBackground(color1.brighter()); setLayout(new BorderLayout()); add(searchLabel, BorderLayout.WEST); add(mySearchField, BorderLayout.EAST); Object element = findElement(mySearchField.getText()); updateSelection(element); }