Absolutely positioning controls
I am trying to place two JButtons in an applet without using a layout manager, I start by creating the JButtons in init() and then call setLayout(null) and setBounds() on each button like this:
setLayout(null);
button1 =new JButton("Manage uploads");
button2 =new JButton("Manage uploads");
add(button1);
add(button2);
But I do not see any buttons on the applet. What am I doing wrong here?
Thanks in advance
[608 byte] By [
danc81a] at [2007-11-27 7:34:44]

> What am I doing wrong here?You should be using a layout manager: http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html
Or if you want to use absolute cooridnates... do specify the absolute coordinates...!Use "setBounds" to do so.
But I need to controls sized and positioned precisely rather than layed out by a LayoutManager, how can I do it without?
I am using setBounds, I missed that bit when I pasted the code, the real code is:
setLayout(null);
button1 = new JButton("Manage uploads");
button2 = new JButton("Manage uploads");
add(button1);
add(button2);
button1.setBounds(WIDTH - 100, HEIGHT - 75, 45, 24);
button2.setBounds(WIDTH - 100, HEIGHT - 75, 45, 24);
are you sure WIDTH - 100 and HEIGHT - 100 ends up with a positive value?Besides, did you use setBounds on the Panel which contains these elements?That's why you were suggested to use LayoutManagers. You always end up with nasty errors when using absolute coordinates...
Also, you should set the bounds of the buttons before you add them to the JPanel.
WIDTH -100 and HEIGHT -100 are definately positive values. The controls are added in the init() method of an Applet-derived object, are you saying it needs to contain a panel or be derived from a panel maybe?(I am new to Java and having real problems with layouts so far)
That cracked it! I called setBounds before adding the controls and the now display correctly, thanks!
I'm having issues with the drawing now when the control is resized. I am custom painting the background of the control by using the paint event to draw the contents. At the end of the paint event I call setBounds() to move the buttons to the correct place but they disappear. What is the best way to move the buttons when the applet resizes?
Ummm... use a layout manager.
I know, I know... you've heard it right... but trust me, try the GridBagLayout, and
Component.setLocation(int x, int y) - Moves this component to a new location.
I've been down the same path as you, went quit mad, asked for help, and ended up with a half-clean mostly-usable solution using a GridBag. No I don't have that code, and its proprietory, so I couldn't post it anyway.
OK, I have used the GridBagLayout and setLocation and it is working almost correctly. If I resize the control really quickly I can see the controls being drawn and then drawn over so when I stop resizing the controls are gone.
I call setLocation() at the end of my paint event, I am assuming this is not the correct place to do it but I thought if I set the control location after I have finsihed painting it would draw them on top of my custom drawing but that doesn't appear to be the case.
What would be the correct way to draw these controls on top of my custom drawing?