0

Ok I gather that every JComponent can set its location...bet it JPanel, JButton, JLabel..whatever. It can set its location use .setLocation(x,y).

I have come to suspect that actually when I do

JButton btn = new JButton("Click me!") ;
btn.setLocation(10,200); 

I am actually changing the location of a button in a panel, and not in the frame. And if I do

JPanel jPanel = new JPanel(); 
jPanel.setLocation(10,100); 

I am changing the location of jPanel not in the JFrame but in the default JPanel provided by default. So JComponents can change their locations, why not just dump everything directly into a bloody jFrame object? :S

I haven't tried but I believe I can arrange stuff just by using the setLocation(x,y) method..and I guess that'd be a big pain the butt. This leads to my question..if we can set components location by using the method, what is the LayoutManager for?

Can you please provide example to show the difference?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Lews Therin
  • 10,907
  • 4
  • 48
  • 72
  • To all: please see this thread: [the-jpanel-contentpane-confusion](http://stackoverflow.com/questions/7758975/the-jpanel-contentpane-confusion) which this is a continuation of (at mKorbel's request). – Hovercraft Full Of Eels Oct 13 '11 at 19:19
  • 7
    Don't use setLocation or setBounds. There is only one answer to this question and it is simple: read the tutorials on the Swing layout managers. Start [HERE](http://download.oracle.com/javase/tutorial/uiswing/layout/visual.html). You'll find plenty of examples here as well. – Hovercraft Full Of Eels Oct 13 '11 at 19:23

1 Answers1

1

Can you please provide example to show the difference?

Here is an example of using layouts, as well as a challenge.

The challenge is to make a resizable, PLAF changable version of that UI using setLocation()/setBounds().

If you (or anyone) can manage it (in code that is small enough to post to the thread), I'll contribute 500 bounty points to the answer.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 2
    +1 nice demo and not somehow the problem to create Resiziable GUI with AbsoluteLayout, there you hold JComponents in percentual HEIGHT/WEIGHT against its parent, by add ComponentListener and Timer and one void that translate percentual amount to the Insets (easiest with even numbers), if you want then I delete this comment (remove one from possible way) , becasue I don't want any bounty that coming from AbsoluteLayout – mKorbel Oct 14 '11 at 07:00
  • @mKorbel *"I don't want any bounty that coming from AbsoluteLayout"* My challenge was like the one by James Randi et al. to prove that magic (or something similar) exists. They put up the $1m, confident it would not be claimed. ;) – Andrew Thompson Oct 14 '11 at 07:43
  • I.E. The [One Million Dollar Paranormal Challenge](http://www.randi.org/site/index.php/1m-challenge.html). – Andrew Thompson Oct 14 '11 at 07:50
  • hehehe someone that wanted how to save the world, but I hope that not my world, – mKorbel Oct 14 '11 at 08:11