I have no idea quite what it's doing in the ComboBoxModel section part with all the arrays of Object. I have tried what I thought should and have tried removing all the elements, hoping that would work, but it doesn't even remove them at all. They're still in there.
Worse, it seems I need to call init() to show the new items, but, everything I've tried has made it so that the old items for the previous directories I've visited, still are there and I don't want that.
import javax.swing.JFileChooser; import java.io.File; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JOptionPane; import javax.swing.JComboBox; import javax.swing.event.DocumentListener; import javax.swing.event.DocumentEvent; import java.awt.BorderLayout; import java.awt.Component; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Window; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.util.ArrayList; import java.util.List; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JTextField; import javax.swing.MutableComboBoxModel; import javax.swing.SwingUtilities; import javax.swing.plaf.basic.BasicComboBoxEditor; import javax.swing.plaf.basic.BasicComboBoxRenderer; import javax.swing.plaf.basic.BasicComboPopup; import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import javax.swing.JPanel; import javax.swing.JCheckBox; import javax.swing.BorderFactory; public class FileChooserWithDelete extends JFileChooser { private JIntelligentComboBox comboBox; private DefaultComboBoxModel dcm; public FileChooserWithDelete() { super("./"); dcm = new DefaultComboBoxModel(); java.io.File f = getCurrentDirectory(); java.io.File[] files = f.listFiles(); for (int i =0; i < files.length; i++) { dcm.addElement(new Object[] {files[i].getName(), "", 0}); } comboBox = new JIntelligentComboBox(dcm); addPropertyChangeListener( new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(evt.getPropertyName())) { JFileChooser chooser = (JFileChooser) evt.getSource(); java.io.File oldDir = (java.io.File) evt.getOldValue(); java.io.File newDir = (java.io.File) evt.getNewValue(); java.io.File curDir = chooser.getCurrentDirectory(); System.out.println(curDir.getName()); //dcm.removeAllElements(); comboBox.removeAllItems(); dcm.removeAllElements(); java.io.File[] moreFiles = curDir.listFiles(); for (int i =0; i < moreFiles.length; i++) { dcm.addElement(new Object[] {moreFiles[i].getName(), "", 0}); } // comboBox.init(); } } }); java.awt.Container cont = (java.awt.Container) (getComponents()[3]); java.awt.Container cont2 = (java.awt.Container) (cont.getComponents()[3]); java.awt.Container cont3 = (java.awt.Container) (cont.getComponents()[0]); cont3.remove(1); cont3.add(comboBox, 1); } protected class JIntelligentComboBox extends JComboBox { private List<Object> itemBackup = new ArrayList<Object>(); public JIntelligentComboBox(MutableComboBoxModel aModel) { super(aModel); init(); } private void init() { this.setRenderer(new searchRenderer()); this.setEditor(new searchComboBoxEditor()); this.setEditable(true); int size = this.getModel().getSize(); Object[] tmp = new Object[this.getModel().getSize()]; for (int i = 0; i < size; i++) { tmp[i] = this.getModel().getElementAt(i); itemBackup.add(tmp[i]); } this.removeAllItems(); this.getModel().addElement(new Object[]{"", "", 0}); for (int i = 0; i < tmp.length; i++) { this.getModel().addElement(tmp[i]); } final JTextField jtf = (JTextField) this.getEditor().getEditorComponent(); jtf.addKeyListener( new KeyAdapter() { @Override public void keyReleased(KeyEvent e) { searchAndListEntries(jtf.getText()); } }); } @Override public MutableComboBoxModel getModel() { return (MutableComboBoxModel) super.getModel(); } private void searchAndListEntries(Object searchFor) { List<Object> found = new ArrayList<Object>(); for (int i = 0; i < this.itemBackup.size(); i++) { Object tmp = this.itemBackup.get(i); if (tmp == null || searchFor == null) { continue; } Object[] o = (Object[]) tmp; String s = (String) o[0]; if (s.startsWith((String) searchFor)) found.add(new Object[] {((Object[]) tmp) [0], searchFor, ((Object[]) tmp) [2]});} this.removeAllItems(); this.getModel().addElement(new Object[]{searchFor, searchFor, 0}); for (int i = 0; i < found.size(); i++) { this.getModel().addElement(found.get(i)); } this.setPopupVisible(true); // [url=http://stackoverflow.com/questions/7605995]java - Set Size of JComboBox PopupMenu - Stack Overflow[/url] BasicComboPopup popup = (BasicComboPopup) this.getAccessibleContext().getAccessibleChild(0); Window popupWindow = SwingUtilities.windowForComponent(popup); Window comboWindow = SwingUtilities.windowForComponent(this); if (comboWindow.equals(popupWindow)) { Component c = popup.getParent(); Dimension d = c.getPreferredSize(); c.setPreferredSize(d); } else { popupWindow.pack(); } } class searchRenderer extends BasicComboBoxRenderer { @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { if (index == 0) { setText(""); return this; } Object[] v = (Object[]) value; String s = (String) v[0]; String lowerS = s.toLowerCase(); String sf = (String) v[1]; String lowerSf = sf.toLowerCase(); List<String> notMatching = new ArrayList<String>(); if (!sf.equals("")) { int fs = -1; int lastFs = 0; while ((fs = lowerS.indexOf(lowerSf, (lastFs == 0) ? -1 : lastFs)) > -1) { notMatching.add(s.substring(lastFs, fs)); lastFs = fs + sf.length(); } notMatching.add(s.substring(lastFs)); } String html = ""; if (notMatching.size() > 1) { html = notMatching.get(0); int start = html.length(); int sfl = sf.length(); for (int i = 1; i < notMatching.size(); i++) { String t = notMatching.get(i); html += "<b style=\"color: black;\">" + s.substring(start, start + sfl) + "</b>" + t; start += sfl + t.length(); } } this.setText("<html><head></head><body style=\"color: gray;\">" + html + "</body></head>"); return this; } } class searchComboBoxEditor extends BasicComboBoxEditor { public searchComboBoxEditor() { super(); } @Override public void setItem(Object anObject) { if (anObject == null) { super.setItem(anObject); } else { Object[] o = (Object[]) anObject; super.setItem(o[0]); } } @Override public Object getItem() { return new Object[]{super.getItem(), super.getItem(), 0}; } } } }
I found some of this online and had to use it as is. I'm not sure what the itemBackup object is doing. I'm wondering if that's part of the reason it's not ever removing anything.
In the PropertyChangeListener, I thought I told it to remove all the items and then add the elements from the current directory, but it's not adding the new ones and the old ones are still there.
I've tried both calling removeAllElements() from the model and removeAllItems() on the combo box itself, but the old stuff is still there.