0

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'm not quite sure how to start.

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.