I'm running my first simple Java Swing application from my UNIX environment. Currently it has an image and some buttons that do random things - one of which executes a command to my UNIX shell.
I have a list of ".ksh" files in one of my directories on the UNIX machine that I'd like to read into a Swing GUI ComboBox.
The dropdown items will populate from the list of files in the directory on the UNIX machine, and when i click a file from the list, it will execute the script in the UNIX shell. The I开发者_JS百科'm not quite sure how to start.
This way you could get the list of the files (as string array) with the extension ".ksh":
File dir = new File(pathToDir);
String[] files;
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return !name.endWith(".ksh");
}
};
files = dir.list(filter);
Then iterate the array and add the names to it.
To execute a command on shell, see one of these many answers
Try something like this:
private JComboBox myComboBox = new JComboBox();
private void showFiles(){
String myPath = "writeYourPathHere..."
File folder = new File(myPath);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
myComboBox.addItem(listOfFiles[i].getName());
}
}
Once you select a file from the combobox
private void selectedFile(){
myComboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//do something
}
});
}
精彩评论