Hi,

I am trying to add buttons to a Panel which is located on JFrame after the JFrame has been set to visible.

My aim is to be able to click "File --> Load" then have it loop through a folder of images,
creating buttons which have the images on them, and then adding them to the JPanel.

Below is the actionevent section which is supposed to load the images and store the buttons in a list. NOTE: TileButton is a class which inherrits JButton - and i have added a few of my own methods.

if(ae.getActionCommand() == "Load Images") {
	            File folder = new File("./images");
	            File[] files = folder.listFiles();
	            
	            System.out.println("Here");	
	        	TileButton button;
	        	
	            for(int i = 0; i < files.length; i++) {

	            	System.out.println(files[i].getPath());
	            	button = new TileButton();
	            	button.addActionListener(this);
	            	button.setIsSet(true);
	            	button.setIcon(files[i].getAbsolutePath());
	            	buttonList.add(i, button);
	                //System.out.println(button.getIcon().getIconHeight() + ":" + button.isSet());
	    			objPanel.add(buttonList.get(i));
	            }	 
		/* Test Button Part */

		}

The printouts on the code above all work properly, but the buttons never show up on the panel.

I have tried repainting the panel and Jframe.

I have had buttons display on the panel before I set the Jframe to visible, just not after.

I have only posted a small but of code - if anyone needs more I shall post it.


Thanks.

You may need to let the layout manager know that you need it to update the layout after you have added the buttons - try calling invalidate() on your panel after adding the buttons.
The other thing is that icons on jLables and jButtons are notorious because if there is any problem with the icon file name/path etc you get no errors or exceptions, you just get a blank label/button.
Use the exists() method of the File class with the path/name of the file to confirm that your program can find eachfile.

commented: Very helpful person +1

You may need to let the layout manager know that you need it to update the layout after you have added the buttons - try calling invalidate() on your panel after adding the buttons.
The other thing is that icons on jLables and jButtons are notorious because if there is any problem with the icon file name/path etc you get no errors or exceptions, you just get a blank label/button.
Use the exists() method of the File class with the path/name of the file to confirm that your program can find eachfile.

Thanks for the help.

I tried the above and it still isn't working. The image files are there because I print out the paths. I'm pretty sure its a problem with adding the buttons to the panel AFTER the frame is visible.

Invalidate didn't change anything.

Any other ideas? Thanks.

I can confirm that its possible to add buttons to a visible panel.
You could try invalidate() on the JFrame after calling it for the panel.
Java's understanding of paths may not exactly match yours, so I would still take 30 secs to use the file exists test to be absolutely certain...
(new File(path)).exists()

I can confirm that its possible to add buttons to a visible panel.
You could try invalidate() on the JFrame after calling it for the panel.
Java's understanding of paths may not exactly match yours, so I would still take 30 secs to use the file exists test to be absolutely certain...
(new File(path)).exists()

Hi, Sorry my answer seemed like I hadn't tried the adding exists(), I actually had and it didn't change anything as the files do exist.

I tried using invalidate() on my frame and it still doesn't work :(

I've tried everything i can think of.


EDIT: I just set the size of the button and it worked..........


When adding the button before showing the JFrame I never set the size, it just fitted into the gridlayout with its own dimensions. Is there any way to make it do this again from the actionPerformed method?

Thanks.

Sorry, I don't know why manually setting the size would have that effect :(
The invalidate() calls should force the layout manager to re-do the layout, and the buttons should get a size from the images, so I think you just hit the limits of my knowledge in this particular direction. :(:(
Maybe one of the smarter people here can shed some light on this... please?

I'm still having problems.

I have found that it works with a JButton but not my TileButton. I don't understand why this is happening as I inherrit the JButton class.

I tried it with a null layout and setting the bounds manually and this seems to work, but it doesn't display anything when I use a GridLayout.

It seems really strange, I will keep on playing around and see if I get anywhere.

Thanks for the help.

I'm still having problems.

I have found that it works with a JButton but not my TileButton. I don't understand why this is happening as I inherrit the JButton class.

I tried it with a null layout and setting the bounds manually and this seems to work, but it doesn't display anything when I use a GridLayout.

It seems really strange, I will keep on playing around and see if I get anywhere.

Thanks for the help.

hey i was searching around and found this:http://stackoverflow.com/questions/3468844/java-swing-add-remove-jbuttons-on-runtime hoping it can be of some help to you

commented: Helped solve my problem +1

@JamesCherrill

invalidate(), please not, never can you please describe reason for sending flag Invalidate for JComponent, I thin that you'll never call this method by hand, because by default is implemented correctly in APIs, and if yes then you have to confirm that by notify()/notifyAll()

for already visible Container is there only

revalidate();
repaint(); // required for componnd JComponents

OMG - just re-read my posts and you're right, I consistently said invalidate when I really meant revalidate. It's a case of complete brain failure I'm afraid, or a "senior moment" as we old folk call them. Thanks for correcting it. Now, what was my name...?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.