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.

will m
  • 1
  • 1

2 Answers2

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

Community
  • 1
  • 1
MByD
  • 135,866
  • 28
  • 264
  • 277
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
        }
    });
}
Fseee
  • 2,476
  • 9
  • 40
  • 63