Java GUI搜索目录文件

时间:2016-06-09 16:28:08

标签: java swing user-interface search actionlistener

这些Threee类是我到目前为止所得到的。

ffprobe version 3.0.2-static http://johnvansickle.com/ffmpeg/  Copyright (c) 2007-2016 the FFmpeg developers
  built with gcc 5.3.1 (Debian 5.3.1-16) 20160424
  configuration: --enable-gpl --enable-version3 --enable-static --disable-debug --enable-libmp3lame --enable-libx264 --enable-libx265 --enable-libwebp --enable-libspeex --enable-libvorbis --enable-libvpx --enable-libfreetype --enable-fontconfig --enable-libxvid --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libtheora --enable-libvo-amrwbenc --enable-gray --enable-libopenjpeg --enable-libopus --enable-libass --enable-gnutls --enable-libvidstab --enable-libsoxr --enable-frei0r --enable-libfribidi --disable-indev=sndio --disable-outdev=sndio --enable-librtmp --enable-libmfx --enable-libzimg --cc=gcc
  libavutil      55. 17.103 / 55. 17.103
  libavcodec     57. 24.102 / 57. 24.102
  libavformat    57. 25.100 / 57. 25.100
  libavdevice    57.  0.101 / 57.  0.101
  libavfilter     6. 31.100 /  6. 31.100
  libswscale      4.  0.100 /  4.  0.100
  libswresample   2.  0.101 /  2.  0.101
  libpostproc    54.  0.100 / 54.  0.100
Input #0, matroska,webm, from '/home/demoss/public_html/source/bALRt9I697PVQh4zglou.webm':
  Metadata:
    encoder         : Chrome
  Duration: N/A, start: 0.000000, bitrate: N/A
    Stream #0:0(eng): Video: vp8, yuv420p, 640x480, SAR 1:1 DAR 4:3, 30 fps, 30 tbr, 1k tbn, 1k tbc (default)
    Stream #0:1(eng): Audio: opus, 48000 Hz, mono, fltp (default)
duration=N/A
package ex;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.tree.*;
import java.io.*;
import javax.swing.event.*;

import java.util.*;

import java.text.*;

class ATable extends AbstractTableModel {
private String title[] = { "Name", "Size", "Type", "Modified Date" };
private String val[][] = new String[1][4];

public void setValueArr(int i) {
   val = new String[i][4];
}

public int getRowCount() {
   return val.length;
}

public int getColumnCount() {
  return val[0].length;
}

public String getColumnName(int column) {
  return title[column];
}

public boolean isCellEditable(int rowIndex, int columnIndex) {
   if (columnIndex == 0)
      return true;
   else
     return false;
}

public Object getValueAt(int row, int column) {
  return val[row][column];
}

public void setValueAt(String aValue, int rowIndex, int columnIndex) {
  val[rowIndex][columnIndex] = aValue;
}
}

}

            package ex;

            import java.awt.*;
            import java.awt.event.*;
            import javax.swing.*;
            import javax.swing.table.*;
            import javax.swing.tree.*;
            import java.io.*;
            import javax.swing.event.*;

            import java.util.*;

            import java.text.*;

            class FileViewer implements TreeWillExpandListener, TreeSelectionListener {
               private JFrame frame = new JFrame("Explorer");
               private Container con = null;

               private JSplitPane pMain = new JSplitPane();
               private JScrollPane pLeft = null;
               private JPanel pRight = new JPanel(new BorderLayout());

               private DefaultMutableTreeNode root = new DefaultMutableTreeNode("My Computer");
               private JTree tree;

               private JPanel pNorth = new JPanel();
               private JPanel northText = new JPanel();
               private JLabel northLabel0 = new JLabel("Path"); 
               private JTextField pathText = new JTextField();
               private JLabel northLabel1 = new JLabel("Search"); 
               private JTextField searchText = new JTextField(); 


               private Dimension dim, dim1;
               private int xpos, ypos;

               FileViewer() {
                  init();
                  start();
                  frame.setSize(800, 600);
                  dim = Toolkit.getDefaultToolkit().getScreenSize();
                  dim1 = frame.getSize();
                  xpos = (int) (dim.getWidth() / 2 - dim1.getWidth() / 2);
                  ypos = (int) (dim.getHeight() / 2 - dim1.getHeight() / 2);
                  frame.setLocation(xpos, ypos);
                  frame.setVisible(true);
               }


            void init() {
                  pMain.setResizeWeight(1);
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                  con = frame.getContentPane();
                  con.setLayout(new BorderLayout());

                  pathText.setPreferredSize(new Dimension(300, 20)); 
                  searchText.setPreferredSize(new Dimension(300, 20));
                  northText.add(northLabel0); 
                  northText.add(pathText);
                  northText.add(northLabel1); 
                  northText.add(searchText); 
                  pNorth.add(northText);
                  con.add(pNorth, "North");
                  File file = new File("C:/");
                  File list[] = file.listFiles();
                  DefaultMutableTreeNode temp;

                  for (int i = 0; i < list.length; ++i) {
                     temp = new DefaultMutableTreeNode(list[i].getPath());
                     temp.add(new DefaultMutableTreeNode("None"));
                     root.add(temp);
                  }
                  tree = new JTree(root);
                  pLeft = new JScrollPane(tree);

                  pMain.setDividerLocation(150);
                  pMain.setLeftComponent(pLeft);
                  pMain.setRightComponent(pRight);
                  con.add(pMain);
               }

               void start() {
                  tree.addTreeWillExpandListener(this);
                  tree.addTreeSelectionListener(this);
               }

               public static void main(String args[]) {
                  JFrame.setDefaultLookAndFeelDecorated(true);
                  new FileViewer();
               }

               String getPath(TreeExpansionEvent e) {
                  StringBuffer path = new StringBuffer();
                  TreePath temp = e.getPath();
                  Object list[] = temp.getPath();
                  for (int i = 0; i < list.length; ++i) {
                     if (i > 0) {
                        path.append(((DefaultMutableTreeNode) list[i]).getUserObject() + "\\");
                     }
                  }
                  return path.toString();
               }

               String getPath(TreeSelectionEvent e) {
                  StringBuffer path = new StringBuffer();
                  TreePath temp = e.getPath();
                  Object list[] = temp.getPath();
                  for (int i = 0; i < list.length; ++i) {
                     if (i > 0) {
                        path.append(((DefaultMutableTreeNode) list[i]).getUserObject() + "\\");
                     }
                  }
                  return path.toString();
               }

               public void getSearch(){

               }





               public void treeWillCollapse(TreeExpansionEvent event) {
               }

               public void treeWillExpand(TreeExpansionEvent e) {
                  if (((String) ((DefaultMutableTreeNode) e.getPath().getLastPathComponent()).getUserObject()).equals("My Computer")) {
                  } else {
                     try {
                        DefaultMutableTreeNode parent = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
                        File tempFile = new File(getPath(e));
                        File list[] = tempFile.listFiles();
                        DefaultMutableTreeNode tempChild;
                        for (File temp : list) {
                           if (temp.isDirectory() && !temp.isHidden()) {
                              tempChild = new DefaultMutableTreeNode(temp.getName());
                              if (true) {
                                 File inFile = new File(getPath(e) + temp.getName() + "\\");
                                 File inFileList[] = inFile.listFiles();
                                 for (File inTemp : inFileList) {
                                    if (inTemp.isDirectory() && !inTemp.isHidden()) {
                                       tempChild.add(new DefaultMutableTreeNode("None"));
                                       break;
                                    }
                                 }
                              }
                              parent.add(tempChild);
                           }
                        }
                        parent.remove(0);
                     } catch (Exception ex) {
                        JOptionPane.showMessageDialog(frame, "Can't Find a File");
                     }
                  }
               }

               public void valueChanged(TreeSelectionEvent e) {
                  if (((String) ((DefaultMutableTreeNode) e.getPath().getLastPathComponent()).getUserObject()).equals("My Computer")) {
                     pathText.setText("My Computer");
                  } else {
                     try {
                        pathText.setText(getPath(e)); 
                        pRight = new FView(getPath(e), null).getTablePanel();
                        pMain.setRightComponent(pRight);
                     } catch (Exception ex) {
                        JOptionPane.showMessageDialog(frame, "Can't Find a File");
                     }
                  }
               }

我想要的是搜索文件&#39;功能在路径文本域的右侧。

搜索文本字段的位置现在很好,但我不知道如何在其上添加功能。

它应该显示一个文件,其中包含用户在搜索文本字段中键入的单词。 (仅限当前目录中的文件。不需要在子目录中显示文件)

当搜索文本字段为空时,它应该像往常一样显示当前目录中的所有文件。

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:2)

con.add(pNorth, "North");

不要使用魔术字符串。 API将包含您可以使用的变量:

con.add(pNorth, BorderLayout.NORTH);
  

它应该显示一个文件,其中包含用户在搜索文本字段

上键入的单词

您需要将DocumentListener添加到Document的{​​{1}}。无论何时添加或删除文本,都会触发事件,然后您可以进行搜索。

阅读How to Write a DocumentListener上Swing教程中的部分,了解更多信息和示例。