Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing a JComboBox on a JFileChooser to list all files that begin with text of first row of combo box
    primarykey
    data
    text
    <p>I managed to get a delete button on a <code>JFileChooser</code>. I figured out where to put the button. In the process, I figured out, by messing with the <code>getComponents()</code> method, where the <code>JTextField</code> where the file is searched for and replaced it with a <code>JComboBox</code>. The plan is to get it to list all the files that begin with the text in the first row, the only one that can be edited on the <code>JComboBox</code>, otherwise it's uneditable, and it could select an item in another row and it would set the text of the item at the first row to the text of that item. (And also update the <code>JCombobBox</code>, though I don't think I implemented that part yet, though a simple method call should do that, but, anyway, I tried <a href="http://www.javaprogrammingforums.com/whats-wrong-my-code/34289-using-jcombobox-jfilechooser-having-problems.html" rel="nofollow">posting</a> this on Java Programming Forums. </p> <p>It's showing the items in there, but it's not letting it update with the typing. It is, instead, showing all the items in the directory. Also, when I go to select an item, it removes the first row and sets the text of the first row to the item. However, it now makes the first row uneditable I think or does something wacky. </p> <p>It is removing the first row because the brilliant people who developed the JComboBox class couldn't bother to make a <code>setItmeAt(Object item, int index)</code> method, only a <code>getItemAt(int index)</code>. So I had to get the text of the item, put it in a variable, remove the item at the first row, and then readd at the first row an item that has the text of the selected item. </p> <p>I fear that maybe my code was too long, so I made it shorter, though even doing that today got no results at the Java Programming Forums, as the issue is with the <code>JComboBox</code> and the File class and stuff. </p> <pre><code>package addressbook.gui; 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; public class FileChooserWithDelete extends JFileChooser { private String textFieldString; 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 &lt; files.length; i++) { dcm.addElement(new Object[] {files[i].getName(), "", 0}); } JButton delete = new JButton("Delete"); delete.setToolTipText("Delete file"); 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(); java.io.File[] moreFiles = curDir.listFiles(); System.out.println("Obama is a loser!"); for (int i =0; i &lt; 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); delete.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { File f= getSelectedFile(); 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]); //javax.swing.JTextField jtf = (javax.swing.JTextField) (cont3.getComponents()[1]); String text = (String) comboBox.getItemAt(0); if (f == null) f = new File("./" + text); int option = JOptionPane.showConfirmDialog(null, "Are you sure you wnat to delete?", "Delete file " + f.getName() + "?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (option == JOptionPane.YES_OPTION) { if (!f.exists() || text == null) { JOptionPane.showMessageDialog(null, "File doesn't exist.", "Could not find file.", JOptionPane.ERROR_MESSAGE); cancelSelection(); } else { f.delete(); cancelSelection(); } } }}); cont2.setLayout(new java.awt.FlowLayout()); //((java.awt.Container) (getComponents()[3])).add(delete); cont2.add(delete); //cont2.add(delete); } protected class JIntelligentComboBox extends JComboBox { private List&lt;Object&gt; itemBackup = new ArrayList&lt;Object&gt;(); 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 &lt; 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 &lt; 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&lt;Object&gt; found = new ArrayList&lt;Object&gt;(); for (int i = 0; i &lt; 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.matches("(?i).*" + searchFor + ".*")) { found.add(new Object[]{ ((Object[]) tmp)[0], searchFor, ((Object[]) tmp)[2] }); } } */ 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 &lt; found.size(); i++) { this.getModel().addElement(found.get(i)); } this.setPopupVisible(true); // http://stackoverflow.com/questions/7605995 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&lt;String&gt; notMatching = new ArrayList&lt;String&gt;(); if (!sf.equals("")) { int fs = -1; int lastFs = 0; while ((fs = lowerS.indexOf(lowerSf, (lastFs == 0) ? -1 : lastFs)) &gt; -1) { notMatching.add(s.substring(lastFs, fs)); lastFs = fs + sf.length(); } notMatching.add(s.substring(lastFs)); } String html = ""; if (notMatching.size() &gt; 1) { html = notMatching.get(0); int start = html.length(); int sfl = sf.length(); for (int i = 1; i &lt; notMatching.size(); i++) { String t = notMatching.get(i); html += "&lt;b style=\"color: black;\"&gt;" + s.substring(start, start + sfl) + "&lt;/b&gt;" + t; start += sfl + t.length(); } } this.setText("&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body style=\"color: gray;\"&gt;" + html + "&lt;/body&gt;&lt;/head&gt;"); 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}; } } } public JTextField getComboBoxTextField() { final JTextField jtf = (JTextField) comboBox.getEditor().getEditorComponent(); return jtf; } public static void main(String[] args) { FileChooserWithDelete fcwd = new FileChooserWithDelete(); fcwd.showOpenDialog(null); } } </code></pre> <p>The link to the page at the java programming forum is <a href="http://www.javaprogrammingforums.com/whats-wrong-my-code/34289-using-jcombobox-jfilechooser-having-problems.html" rel="nofollow">here</a>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload