File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes How to place stuff without a layout? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to place stuff without a layout?" Watch "How to place stuff without a layout?" New topic
Author

How to place stuff without a layout?

Hans vogn
Ranch Hand

Joined: Feb 04, 2009
Posts: 46
Hi

I want to make a frame that holds a panel, that hold some buttons, and I want to control where those buttons should be myself. They only way I found that possible was by using setLayout(null), and my code looks something like this:



Nothing but the empty frame is shown, and i really can't find out what i'm doing wrong.

Can someoneplease explain what is wrong?

Thanks in advance.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Because you're using a null layout manager, the preferred size of the JPanel is 0x0. You will need to set that manually.

Or use a custom layout manager that does not layout the components but only calculates the preferred and minimum sizes:
This way, your JPanel's preferred size will be the most bottom-right point of any component.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10171
    
    8

In addition to what Rob suggested, try setting the bounds of the button after you have added it to the panel.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Shrinath M Aithal
Ranch Hand

Joined: May 20, 2009
Posts: 82
easiest way -> use netbeans
no, i am not joking.. if you are going to design a serious GUI, and if you aren't comfortable with coding the UI, use netbeans and I assure you, you'll find it lot easier..

just drag and drop..


Regards
Shri..

SCJP 5.0
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2155
    
    7
The easiest way is to learn how to use LayoutManagers. Then you are not dependent on any particulare IDE.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10171
    
    8

Shrinath M Aithal wrote:easiest way -> use netbeans


One word...don't.
You will end up learning the IDE and not Java.
Shrinath M Aithal
Ranch Hand

Joined: May 20, 2009
Posts: 82
Maneesh Godbole wrote:
You will end up learning the IDE and not Java.


you missed my point : if you are not comfortable with UI programming, but you want it someway.. then go with IDE.. Just a suggestion.. thats it .. and yes, that is the easiest way..
if you want to learn how it is done without an IDE, then learning Layout Managers is good.. you cant ask everyone to learn all of java even if they dont like
By the way, IDEs are created for the purpose of making things easier..
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10171
    
    8

One question for you.
Imagine you use netbeans. Then you switch jobs and the new company uses eclipse. What would you do?
Shrinath M Aithal
Ranch Hand

Joined: May 20, 2009
Posts: 82
Maneesh Godbole wrote:One question for you.
Imagine you use netbeans. Then you switch jobs and the new company uses eclipse. What would you do?


Lol thats no problem with me.. i've known both, because I love trying all those stuffs..

in answer to your question in general.. both are almost same regarding drag and drop or rather layout..
it doesn't take much time learning Eclipse IDE if you already know netbeans and vice versa is true

you may say it wont take much time learning Layout managers too.. but for some of them who aren't really interested in Layout Managers, or they are willing to save time then learning IDE's are simpler and faster then layout managers..

dont you feel so

why are you guys against IDEs? I am not a huge fan myself, but I dont hate them and I use them whenever I feel that a particular work could be done easily in IDE rather than coding.. And when desining GUI's, IDE seems to be faster than coding things myself.. I'll be looking at whatever I am doing rather than only codes.. ( obviously you'll have to keep compiling and executing the files through a console if you aren't using IDE)..
So that makes my GUI programming faster and easier..
Its just my opinion.. I dont say others should follow it.. Sorry if I hurt anyone's feelings..
Hans vogn
Ranch Hand

Joined: Feb 04, 2009
Posts: 46
Thank you very much.

I'll stick to coding it myself (instead if IEDs) as I'm still only learning Java.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10171
    
    8

Shrinath M Aithal wrote:
why are you guys against IDEs?


I think you are missing the point here. We are not "against" IDE's, but DnD kind of IDEs. Hand coding is king.
At a beginners stage, one should use simple editors (heck even command line will do). That is the best way to grasp concepts like packaging and classpaths. Then move on to IDEs where you still hand code.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

Most of us love IDEs. Eclipse certainly makes my life a lot easier.

It's the automatically generated code that's a problem for us. Other then generating serialVersionUIDs, getters, setters, overridden en implementing methods, I don't let Eclipse generate a single thing. And even those I document manually.
Shrinath M Aithal
Ranch Hand

Joined: May 20, 2009
Posts: 82
Maneesh Godbole wrote:

Hand coding is king.


I agree with that.. I dont like doing IDEs myself.. but when it comes to gui, i like IDEs because they save your time a lot.. you dont have to type something like "JButton jb=new JButton("whatever"); panel.add(jb); " and you dont need to mention its size or any such criteria explicitly by typing.. you'll get it as soon as you pull the button to stage..

Maneesh Godbole wrote:
At a beginners stage, one should use simple editors (heck even command line will do). That is the best way to grasp concepts like packaging and classpaths. Then move on to IDEs where you still hand code.


According to me, IDEs are time savers.. you can do lot of designing in small time.. after that, obviously there is hand coding where you give actions to all those buttons or whatever you want to do..

what I say is, when there is facility to do things in a easy way, do it the easy way..
I dont say you shouldn't learn layout managers.. and I am not against any other post advising him to learn layout managers.. All I say is, if you are interested in learning it, do it.. but I am suggesting, there is also an easy way, if you wanna have it easy way..

Maneesh Godbole wrote: One question for you.
Imagine you use netbeans. Then you switch jobs and the new company uses eclipse. What would you do?


it'd be easy to learn it because I know netbeans already.. and i am used to work with IDEs..
what if I asked you the same question.. ;)
you are only doing hand coding, and what if the company asks you to do the same thing with IDEs because it is faster ?

you've to start learning how to use IDEs then.. just like that.. finally all I've to say is :

1. You should know the concept.. agreed..
2. If there is an easy way of doing things, adapt to it..
3. There is no harm in doing things in IDE.. they eliminate half the typing delays and thats why many companies prefer them.
4. I dont say you shouldn't hand code.. Even in IDEs you'll have to code most of it yourself.. but in today's world, time saved is money gained..and that too by breaking less sweat .. so adapt to it.



Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10171
    
    8

IDE usage is very subjective so I would not like to continue this discussion here. Also let us not forget, we are almost hijacking Han's thread!
It might be a good idea, to open a new thread if you feel like it. Let us cease this discussion here.
Shrinath M Aithal
Ranch Hand

Joined: May 20, 2009
Posts: 82
Maneesh Godbole wrote:IDE usage is very subjective so I would not like to continue this discussion here. Also let us not forget, we are almost hijacking Han's thread!
It might be a good idea, to open a new thread if you feel like it. Let us cease this discussion here.


he he
ya..

it is interesting though.. I think ranch should open a poll by providing all the above discussions above and see how subjective it is
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to place stuff without a layout?
 
Similar Threads
Classes
Problem with Adding JScrollPane to JPanel!
Adding JPanel to JFrame
Null Pointer Exception in a GUI application
Why can't I display a JPanel?