9

I want to place some buttons in a JPanel at random positions (x,y), and these layout classes are annoying.

Is this even possible in Swing?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
vlad-ardelean
  • 7,480
  • 15
  • 80
  • 124
  • 4
    It's better to learn about different [Layout Managers](http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html). – trashgod Jan 06 '12 at 13:11
  • 1
    No, you definitely _do not want_ to do any swing layout manually. And no, there's nothing annoying about layoutManagers - except you think learning is annoying in itself ;-) – kleopatra Jan 06 '12 at 13:24
  • 1
    why is everybody second-guessing the op? maybe he just needs complete control over the buttons positions (like for example in some kind of game where they move around and the user has to hit them). – fortran Jan 06 '12 at 13:27
  • 1
    @fortran *"complete control over the buttons"* If you have the logic needed to maintain that 'control' in a logical sense - put it in a custom layout manager. – Andrew Thompson Jan 06 '12 at 13:41
  • 1
    @AndrewThompson why? If I just want to shake them randomly (for example), what is the advantage of putting it in a custom layout manager? – fortran Jan 06 '12 at 13:46
  • Post an actual (working*) example on a separate question & I might be willing to discuss it further. * And by 'working' I mean works for a variety of components & PLAFs in a resizable GUI. – Andrew Thompson Jan 06 '12 at 14:00

2 Answers2

12

You can set the coordinates if you use a null layout:

panel.setLayout(null);
Button b = new Button(....);
panel.add(b);
b.setSize(width, height);
b.setLocation(x,y);

But it is strongly recommended to use layouts. Layout classes are not "annoying", they are your friend if you understand them properly.

I propose reading a tutorial about GridBagLayout, it is easy to understand (kinda html tables) and very powerful.

Stephan
  • 4,395
  • 3
  • 26
  • 49
  • +1 for recommending LayoutManagers, -0.5 for doing manual layout incompletely (hach, nicely prooving the usefulness of managers ... :-) – kleopatra Jan 06 '12 at 13:26
5

use null as "Layout Manager":

http://docs.oracle.com/javase/tutorial/uiswing/layout/none.html

fortran
  • 74,053
  • 25
  • 135
  • 175
  • 3
    that is what I wrote before editing, but then I realised that someone might try to look for a class called like that ^_^ – fortran Jan 06 '12 at 13:08
  • @kleopatra what kind of problem do you have? – fortran Jan 06 '12 at 13:31
  • 1
    no problem at all, it's simple wrong to propagate usage of null layout :-) – kleopatra Jan 06 '12 at 13:34
  • 1
    See [setLayout(null) is never necessary. Ever!](https://forums.oracle.com/forums/thread.jspa?threadID=1351374) for more discussion on the matter. – Andrew Thompson Jan 06 '12 at 13:39
  • @AndrewThompson creating a custom LayoutManager that does the same as the null layout when you really need to place components "manually" seems too add unnecessary layers and complexity – fortran Jan 06 '12 at 13:44
  • Try it (and make it work for `null` layout across the range of circumstances outlined in the linked thread) to find out different. – Andrew Thompson Jan 06 '12 at 13:47