Placing Buttons

I am trying to place a button in a specific location inside of an internal frame. I was thinking that I could use the setLocation method but that doesnt seem to work. The button's picture icon is not showing up at all. I think it is because I need to seta certain layout for the content pane first. This is the basic code I have so far:

ImageIcon bpic =new ImageIcon("button.bmp");

JButton but1 =new JButton("My Button", bpic);

but1.setContentAreaFilled(false);

but1.setBorderPainted(false);

getContentPane().add(but1);

but1.setLocation(0,0);

bpic is suppposed be an icon that you can press as the button but it will not work. I also would like to maybe have some text below or above the button icon but not be able to be clicked as the button. Thanks for any help.

[999 byte] By [lesnaubra] at [2007-11-27 11:56:53]
# 1

it is better to use layouts

http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html

but if you insist set your internal frame layout to null

e.g. internalFrame.setLayout(null);

then use setLocation or setBounds

note: My advice is to use layout

Yannixa at 2007-7-29 19:10:06 > top of Java-index,Desktop,Core GUI APIs...
# 2

I still cannot get the image to show up as the button. I also need to find a way to change the button image when the button is pressed down.

lesnaubra at 2007-7-29 19:10:06 > top of Java-index,Desktop,Core GUI APIs...
# 3

Not sure if .bmp files are supported. Try a .gif or .jpg which are the sample files types used in the tutorials.

camickra at 2007-7-29 19:10:06 > top of Java-index,Desktop,Core GUI APIs...
# 4

I actually just tried that for the same reasons and still nothing. I Don't get any errors or warnings or anything like that. I've edited my code a little so I'll post it really quick:

Icon bee = new ImageIcon("button.gif");

JButton but1 = new JButton("HEY", bee);

but1.setContentAreaFilled(false);

but1.setBorderPainted(false);

this.getContentPane().setLayout(null);

but1.setLocation(100,100);

but1.setSize(200, 150);

this.getContentPane().add(but1);

I use the setaBorederPainted(false) and setContentAreaFilled(false) so that the borders of the buttons do not show up and only the image should but the image still won't show. I know I'm using the correct file name also. Any ideas?

lesnaubra at 2007-7-29 19:10:06 > top of Java-index,Desktop,Core GUI APIs...
# 5

Also the way Sun's tutorial says to make the Icon is to use the code,

ImageIcon leftButtonIcon = createImageIcon("images/right.gif");

It keeps giving me an error with the createImageIcon() function. I don't know if this is just something that has changed since the tutorial was written or what. I don't know if this is part of the reason I cannot get the Icon image to show up.

lesnaubra at 2007-7-29 19:10:06 > top of Java-index,Desktop,Core GUI APIs...
# 6

> Also the way Sun's tutorial says to make the Icon is to use the code,

Download the whole sample program and run the code. The createImageIcon(...) method is coded in the program. Its only a single line of code to create an icon:

ImageIcon icon = new ImageIcon("right.gif");

or you can create a button directly:

JButton button = new JButton( new ImageIcon("right.gif") );

There is no trick to doing this.

If you need further help then you need to create a "Short, Self Contained, Compilable and Executable, Example Program (SSCCE)",

see http://homepage1.nifty.com/algafield/sscce.html,

that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

Don't forget to use the "Code Formatting Tags",

see http://forum.java.sun.com/help.jspa?sec=formatting,

so the posted code retains its original formatting.

camickra at 2007-7-29 19:10:06 > top of Java-index,Desktop,Core GUI APIs...