繁体   English   中英

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

[英]Using a JComboBox on a JFileChooser to list all files that begin with text of first row of combo box

我设法在JFileChooser上得到一个删除按钮。 我弄清楚了将按钮放在哪里。 在此过程中,我弄乱了getComponents()方法,找到了要在其中搜索文件的JTextField并将其替换为JComboBox 计划是让它列出第一行中所有以文本开头的文件,这是唯一可以在JComboBox上进行编辑的文件,否则它是不可编辑的,并且可以在另一行中选择一个项目并将其设置为位于第一行的项目文本到该项目的文本。 (并且还更新了JCombobBox ,尽管我不认为我尚未实现该部分,尽管可以通过一个简单的方法调用来实现,但是无论如何,我都尝试将其发布在Java编程论坛上。

它在其中显示项目,但不让它随输入内容更新。 而是显示目录中的所有项目。 另外,当我去选择一个项目时,它会删除第一行并将第一行的文本设置为该项目。 但是,它现在使第一行变得不可编辑,我觉得有些奇怪。

之所以要删除第一行,是因为开发JComboBox类的才华横溢的人不会费心去制作setItmeAt(Object item, int index)方法,而只是创建getItemAt(int index) 因此,我必须获取项目的文本,将其放入变量,在第一行中删除该项目,然后在第一行中读取具有所选项目的文本的项目。

我担心我的代码可能太长了,所以我把它缩短了,尽管即使今天这样做在Java编程论坛上也没有结果,因为JComboBox和File类以及其他东西都是问题。

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


}  
}

Java编程论坛上的页面链接位于此处

您也许可以采用此处研究的方法。 对于通知,您可以利用现有JFileChooser事件,如图所示这里 另外,您也可以定义自己PropertyChangeEvent ,如图所示这里

我有工作。 但是,有一个问题。 以下几行使它成为我所有弹出窗口(即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;

   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM