1

I am in the very early days of learning Java and currently looking at GUIs.

I would like to be able to make a series of buttons corresponding to each file found in the folder. For example, the name of the file could appear inside the button. At this stage the buttons DON'T need to do anything !

I have managed to make separate programs which (a) Produces an array of file names found in the folder. (b) Produces a set of buttons using a for loop. However, my attempts at combining the two have been laughable. My knowledge of OO Java just doesn't cut the mustard.

Please could somebody advise me on how to proceed? Please keep it simple!

ButtonGrid.java

import javax.swing.JFrame; 
import javax.swing.JButton; 
import java.awt.GridLayout;
 
public class ButtonGrid {
 
        JFrame frame=new JFrame(); 
        JButton[][] grid; 
 
        public ButtonGrid(int width, int length){ 
                frame.setLayout(new GridLayout(width,length)); 
                grid=new JButton[width][length]; 
                for(int y=0; y<length; y++){
                        for(int x=0; x<width; x++){
                                grid[x][y]=new JButton("("+x+","+y+")"); 
                                frame.add(grid[x][y]); 
                        }
                }
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack(); 
                frame.setVisible(true); 
        }
        public static void main(String[] args) {
                new ButtonGrid(3,3);
        }
}

ListFiles.Java

import java.io.File;
 
public class ListFiles 
{
 
 public static void main(String[] args) 
{
 
  String path = "."; 
   String files;
  File folder = new File(path);
  File[] listOfFiles = folder.listFiles(); 
 
  for (int i = 0; i < listOfFiles.length; i++) 
  {
   files = listOfFiles[i].getName();
          System.out.println(files);
   }
}
}
Community
  • 1
  • 1

2 Answers2

1

Code :

import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.io.File;
import javax.swing.JButton;

public class ListFiles
{
    public static void main(String[] args) {
        JFrame frame=new JFrame();
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(2,5));
        String path = ".";
        String file;
        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();

        for (int i = 0; i < listOfFiles.length; i++) {
            file = listOfFiles[i].getName();
            panel.add( new JButton( file ) );
        }
        frame.add( panel ) ;
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
0

Place your code from ListFiles.java's main method into ButtonGrid.java's constructor

public ButtonGrid(){ 

            File folder = new File(path);
            File[] listOfFiles = folder.listFiles();
            // about even width and height but this should be changed
            int width = (int)Math.ceil( Math.sqrt( listOfFiles.length ) );

            frame.setLayout(new GridLayout(width,width)); 
            grid=new JButton[width][width];

            int count = 0;
            for(int y=0; y < width; y++){
                    for(int x=0; x<width; x++){
                        if( count < listOfFiles.length ){
                            grid[x][y]=new JButton(listOfFiles[count].getName()); 
                            frame.add(grid[x][y]); 
                            count++;
                        }
                    }
            }
            ...

It should be pretty simple, i havent tested this but something very similar should work if this doesnt. Also the other answer was added while i was typing but my code is closer to your original post so ive posted anyway. : )

Adrian
  • 495
  • 2
  • 10