I figured out how to add a delete key, which is what I originally made the thing for, to see if I could do it.
Now, I replaced the JTextField where you put in the file name and put in a JComboBox instead. I tried to get it now so that it will change the items in the JComboBox as you type, making it so that it will show all the files that start with the letters in the JComboBox. I had it so that it was supposed to let you edit the first row and update so that if you choose another row, it will change the contents of the first row in the combo box to the text of that row.
However, I had to remove the first row, maybe that's the issue.
For one thing, it's not letting you type right away, despite me having set the first row's index to already be selected, you have to go and click it. (Which is annoying.)
I have just fixed part of the thing now so that the exceptions go away. However, it's not adding any items to the JComboBox when I type.
It has only the first row able to be edited and the rest not.
I had wanted it to be basically like a JTextField, but with a JComboBox so that it could display the list of files that start with the characters in the JTextField.
It took me a while to figure out what component went where in the JFileChooser component hierarchy, but I did, so that's not the issue here.
It is saying that when I remove the item at 0 in my ActionListener, that that is the cause of the exception. I don't know how to remedy it. JComboBox doesn't have a setItemAt(Object item, int index) method. The only way I could change the contents of the item was to remove then re-add with the new contents.
(All my stuff with the delete button works, though I wish it would auto-refresh as right now, it'll still show the item and let you keep "deleting" it, unless you hit refresh on the JFileChooser, but I can't find a way to refresh it, so I had to make it exit the file chooser rather than waste the user's time. You don't need to look at the stuff that involves the delete button.)
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; public class FileChooserWithDelete extends JFileChooser { private JComboBox<String> comboBox; public FileChooserWithDelete() { super("./"); JButton delete = new JButton("Delete"); delete.setToolTipText("Delete file"); comboBox = new JComboBox<String>(); comboBox.insertItemAt("", 0); comboBox.setSelectedIndex(0); comboBox.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { if (comboBox.getSelectedIndex() != 0) { comboBox.setEditable(false); comboBox.removeItemAt(0); comboBox.insertItemAt((String) comboBox.getSelectedItem(), 0); } else comboBox.setEditable(true); }}); final javax.swing.text.JTextComponent tc = (javax.swing.text.JTextComponent) comboBox.getEditor().getEditorComponent(); tc.getDocument().addDocumentListener( new DocumentListener() { public void changedUpdate(DocumentEvent e) { populateComboBox(); } public void removeUpdate(DocumentEvent e) { } public void insertUpdate(DocumentEvent e) { } }); 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]); 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() || jtf.getText() == 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()); cont2.add(delete); } public static void main(String[] args) { FileChooserWithDelete fcwd = new FileChooserWithDelete(); fcwd.showOpenDialog(null); } private void populateComboBox() { comboBox.removeAllItems(); comboBox.insertItemAt("", 0); File f = new File("./"); java.io.File[] files = f.listFiles(); String str = comboBox.getItemAt(0); int j = 1; for (int i=0; i < files.length; i++) { if (files[i].getName().startsWith(str)) comboBox.insertItemAt( files[i].getName(), j); j++; } } }
Note, this already is a SSCCE as it has a main method and I intend to use it in something already more developed, er, once, and if, I get this to work.