【发布时间】:2015-03-25 02:00:13
【问题描述】:
我需要显示两个带有目录名称的连接组合框。 There is a start path which contains multiple directories that are displayed in the first jcombobox, and when a directory is selected, the sub directories need to be displayed in the second jcombobox.第二个 jcombobox 应该能够选择这些子目录之一。每个子子目录都包含多个 .txt 文件。我设法在包括文件在内的两个 jcombobox 上显示目录和子目录。
package calc.my.pay;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JComboBox;
import java.awt.Color;
import javax.swing.JTable;
import javax.swing.JScrollPane;
public class CalcMyPay extends JFrame implements ActionListener {
private JPanel contentPane;
private JScrollPane scrollPane;
private JComboBox folderSelector, subFolderSelector;
private File[] directory, subDirectory;
private String subPath, finalSubPath, selectedSubDirectory, finalSubDirectory;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CalcMyPay frame = new CalcMyPay();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
* @throws IOException
*/
public CalcMyPay()
{
setBackground(Color.LIGHT_GRAY);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(350, 10, 1000, 700);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
scrollPane = new JScrollPane();
scrollPane.setBounds(12, 120, 958, 300);
String startPath = "C:/Users/Zeus/Desktop/Content/";
// The starting path of the file
directory = new File(startPath).listFiles();
// Folders dropdown box
folderSelector = new JComboBox();
folderSelector.setBounds(60, 13, 200, 22);
folderSelector.insertItemAt("Choose directory", 0);
folderSelector.setSelectedIndex(0);
for(int i=0; i < directory.length; i++) {
System.out.println(directory[i]);
folderSelector.addItem(directory[i].getName());
}
contentPane.add(folderSelector);
// Sub folder dropdown box
subFolderSelector = new JComboBox();
subFolderSelector.setBounds(300, 13, 200, 22);
subFolderSelector.insertItemAt("Choose subdirectory", 0);
subFolderSelector.setSelectedIndex(0);
contentPane.add(subFolderSelector);
folderSelector.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Get the folder dropdown selected item
selectedSubDirectory = folderSelector.getSelectedItem().toString();
//System.out.println("Folder selected: " + Arrays.asList(directory).toString().contains(selectedSubDirectory));
//System.out.println("Subfolders based on folder selected : " + Arrays.asList(directory) + " " + selectedSubDirectory);
//subFolderSelector.addItem(folderSelector.getSelectedItem());
// Check if the array from the main directory contains the selected directory
if(Arrays.asList(directory).toString().contains(selectedSubDirectory)) {
// Make a new file that list all the directories in the given path
subPath = startPath + selectedSubDirectory;
subDirectory = new File(subPath).listFiles();
// Sort the array directory in a descending order
Arrays.sort(subDirectory, Collections.reverseOrder());
// Delete the previous list items, if any
subFolderSelector.removeAllItems();
// Iterate through all the directories in the selected folder
for(int i=0; i < subDirectory.length; i++) {
// Pass only directories (files will be omitted)
// ### Should check if directory contains only 4 numbers here
if(subDirectory[i].isDirectory()) {
//subFolderSelector.addItem(folderSelector.getSelectedItem());
subFolderSelector.addItem(subDirectory[i].getName());
}
}
}
}
});
subFolderSelector.addActionListener(new ActionListener() {
int count = 0;
@Override
public void actionPerformed(ActionEvent e) {
count++;
if(count == 3) {
selectedSubSubDirectory = subFolderSelector.getSelectedItem().toString();
// Make an new file that list all the file in the selected sub folder
if(Arrays.asList(subDirectory).toString().contains(selectedSubDirectory)) {
//textFiles = new File(finalSubPath).listFiles();
subSubPath = startPath + selectedSubDirectory + "/" + selectedSubSubDirectory;
textFiles = new File(subSubPath).listFiles();
for(File file: textFiles) {
if(file.isFile() && file.toString().toLowerCase().endsWith("_110.txt")) {
System.out.println(file);
}
}
}
count = 0;
}
}
});
}
}
但是,正如您在 subDirectorySelector 的第二个 actionListener 中看到的那样,我有一个计数器。那里的代码执行了 3 次(因为第二个 jcombobox 改变了值)。如果在第一个 jcombobox 中两次选择相同的路径,则会出现错误。必须有更好的(并且可能)更短的方法来做到这一点。你会改变什么?
谢谢
【问题讨论】:
标签: java directory jcombobox subdirectory