在JFileChooser上使用JComboBox列出以第一行组合框文本开头的所有文件

时间:2013-12-03 04:55:39

标签: java swing file jcombobox jfilechooser

我设法在JFileChooser上获得了删除按钮。我想出了按钮的位置。在这个过程中,我想通过弄乱getComponents()方法,找到搜索文件的JTextField,并用JComboBox替换它。计划是让它列出所有以第一行中的文本开头的文件,唯一可以在JComboBox上编辑的文件,否则它是不可编辑的,它可以选择另一行中的项目,它会将第一行的项目文本设置为该项目的文本。 (并且还更新JCombobBox,虽然我认为我还没有实现那个部分,尽管一个简单的方法调用应该这样做,但是,无论如何,我在Java Programming Forums上尝试了posting。 / p>

它显示了那里的项目,但它不会让它随着输入更新。相反,它显示目录中的所有项目。此外,当我选择一个项目时,它会删除第一行并将第一行的文本设置为该项目。但是,它现在使第一行不可编辑,我认为或做一些古怪的事情。

它正在移除第一行,因为开发JComboBox类的优秀人员无法制作setItmeAt(Object item, int index)方法,只能使用getItemAt(int index)JComboBox。所以我必须获取项目的文本,将其放在变量中,删除第一行的项目,然后在第一行读取包含所选项目文本的项目。

我担心也许我的代码太长了,所以我把它缩短了,尽管今天做到这一点在Java Programming Forums没有结果,因为问题在于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 < 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 < 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<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.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 < 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<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}; } } } 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); } } 和File类和东西。

{{1}}

java编程论坛页面的链接是here

2 个答案:

答案 0 :(得分:1)

您可以调整检查的方法here。对于通知,您可以利用现有的JFileChooser事件,如here所示。或者,您可以定义自己的PropertyChangeEvent,如图所示here

答案 1 :(得分:0)

我已经开始工作了。但是,有一个问题。以下几行导致它使我的所有弹出窗口(即JPopupMenu,JMenu等)变得粗糙,以至于你无法真正阅读它们上的标题。

以下几行似乎是罪魁祸首。

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();
         }

那是做什么的?当我拿出它时,它摆脱了错误。

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;
import javax.swing.JPanel;
import javax.swing.JCheckBox;
import javax.swing.BorderFactory;


public class FileChooserWithDelete extends JFileChooser
{

   private String textFieldString;
   private JIntelligentComboBox comboBox;
   private DefaultComboBoxModel dcm;
   private JCheckBox read, write, execute;



   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});

      }


      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();
                     comboBox.removeAllItems();
                     dcm.removeAllElements();




                     java.io.File[] moreFiles = curDir.listFiles();




                     System.out.println("Obama is a loser!");







                     for (int i =0; i < moreFiles.length; i++)
                     {



                        dcm.addElement(new Object[] {moreFiles[i].getName(), "", 0});
                        //comboBox.insertItemAt(moreFiles[i].getName(), i);


                     }


                     for (int i =0; i < comboBox.getItemCount(); i++)
                     {
                        System.out.println(java.util.Arrays.toString((Object[]) comboBox.getItemAt(i)));


                     }

                    // comboBox.init();
                  */

                     comboBox.updateFiles();


                  }
               }
            });




      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]);


      read = new JCheckBox("Read");
      write = new JCheckBox("Write");
      execute = new JCheckBox("Execute");


      JPanel checkBoxPanel = new JPanel();
      checkBoxPanel.setBorder(BorderFactory.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));



      cont3.remove(1);

      cont3.add(comboBox, 1);


      checkBoxPanel.setLayout(new java.awt.FlowLayout());
      checkBoxPanel.add(read);
      checkBoxPanel.add(write);
      checkBoxPanel.add(execute);

      read.setEnabled(false);
      write.setEnabled(false);
      execute.setEnabled(false);


      addPropertyChangeListener(
            new PropertyChangeListener() {

               public void propertyChange(PropertyChangeEvent e2)
               {

                  if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY
                              .equals(e2.getPropertyName())) {
                     JFileChooser chooser = (JFileChooser)e2.getSource();
                     File oldFile = (File)e2.getOldValue();
                     File newFile = (File)e2.getNewValue();

                              // The selected file should always be the same as newFile
                     File curFile = chooser.getSelectedFile();

                     try
                     {

                        if (curFile.canRead())
                        {

                           read.setSelected(true);

                        }

                        else
                        {
                           read.setSelected(false);

                        }

                        if (curFile.canWrite())
                        {
                           write.setSelected(true);
                        }

                        else
                        {
                           write.setSelected(false);
                        }

                        if (curFile.canExecute())
                        {
                           execute.setSelected(true);

                        }

                        else
                        {
                           execute.setSelected(false);

                        }
                     }

                     catch(NullPointerException npe)
                     {

                        return;
                     }


                  } 



               }

            });




      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(checkBoxPanel);





   }




   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 updateFiles()
      {

         itemBackup.clear();


         java.io.File f = getCurrentDirectory();

         java.io.File[] files = f.listFiles();

         for (int i =0; i < files.length; i++)
         {

            itemBackup.add(new Object[] {files[i].getName(), "", 0});

         }





      }
      private void searchAndListEntries(Object searchFor) {
         List<Object> found = new ArrayList<Object>();

         for (int i =0; i < itemBackup.size(); i++)
         {

            System.out.println(java.util.Arrays.toString((Object[])itemBackup.get(i)));

         }



         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.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 < 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();
         }
         */

         System.out.println("break");

         for (int i =0; i < found.size(); i++)
         {

            System.out.println(java.util.Arrays.toString((Object[]) found.get(i)));


         }
      }

      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};
         }
      }
   }

   public JTextField getComboBoxTextField()
   {

      final JTextField jtf = (JTextField) comboBox.getEditor().getEditorComponent();

      return jtf;

   }
}