Reputation: 67
I am trying to create a certain amount of buttons that is equal to the amount of files in a folder. I'm pretty sure this is done through a for loop, although I do not know how to set the unique locations per button, because I couldn't set the location the save or it would just have many buttons in the same spot. Because there could be many files in the folder, using an if statement for each number wouldn't work and would be a tedious process. Would the for loop be creating a new button each iteration? If so, how could I set each location differently? Are there any other ways to do this? I know how to create a button, but I don't know how to set the unique location of each button. (Preferably the y
part)
for (int i = 0; i <= numberOfFiles; i++) {
// Create new button?
}
I am looking to create the same amount of buttons as a certain amount of files in a folder.
Upvotes: 0
Views: 651
Reputation: 338201
Yes, you got it right. Inside your for
loop instantiate your new button and add the new button to your user-interface. See this related Question.
for ( int i = 0; i <= numberOfFiles; i++ )
{
JButton button = new JButton( "whatever" );
myUi.add( button ) ;
}
A shorter way to write that loop, if you do not need a count:
for ( File file : files )
{
JButton button = new JButton( "whatever" );
myUi.add( button ) ;
}
Upvotes: 1