开发者

Java Swing ComboBox list of files from unix machine

开发者 https://www.devze.com 2023-02-23 07:49 出处:网络
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'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
        }
    });
}
0

精彩评论

暂无评论...
验证码 换一张
取 消