13 Replies - 3141 Views - Last Post: 03 July 2010 - 10:10 PM Rate Topic: -----

#1 sh1n3  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 164
  • Joined: 22-April 10

Java Swing Components Placement

Posted 03 July 2010 - 06:59 AM

I just began learning swing and I need to know how to put Swing components to the next line.
I'm currently using Flow Layout but everything appears on a single line.
Is there an inbuilt method or anything for this?
Is This A Good Question/Topic? 0
  • +

Replies To: Java Swing Components Placement

#2 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Java Swing Components Placement

Posted 03 July 2010 - 07:15 AM

There are actually many other layouts at your disposal. Look into Grid Layout, Box Layouts, and Border Layouts. Let us know if we can give you any more help :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#3 sh1n3  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 164
  • Joined: 22-April 10

Re: Java Swing Components Placement

Posted 03 July 2010 - 09:45 AM

Actually pbl had told me of many Layouts that he has been using like:

  • Paragraph Layout
  • Constraint Layout
  • Clock Layout
  • Basic Grid Layout
  • Grid Layout Plus


custom-made by JHLabs [which I'm sure can be found through Google]
But I'd need to import the jar file every time I use them.

I could easily do it Box Layout. And is there a way I can use 2 Layouts
together in the same JPanel?

I was more interested in Flow Layout and Null Layout.
I do know that in null layout something called Insets is used to set
the co-ordinates of the value.

But if there is some sort of method to align like Component.ALIGN_RIGHT
it would help me the trouble of going through Sun's tutorials.
Was This Post Helpful? 0
  • +
  • -

#4 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Java Swing Components Placement

Posted 03 July 2010 - 10:05 AM

Border layout uses the cardinal directions and CENTER to align components. There are indeed many other types of layouts, but the three that I mentioned are the most common for beginners to use. You can't use more than one layout in the same JPanel, but you can use an individual layout for each JPanel you have then organize your JFrame with a separate Layout as well
Was This Post Helpful? 1
  • +
  • -

#5 sh1n3  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 164
  • Joined: 22-April 10

Re: Java Swing Components Placement

Posted 03 July 2010 - 10:12 AM

Got it! :-)
Thanks for the info!
Was This Post Helpful? 0
  • +
  • -

#6 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Java Swing Components Placement

Posted 03 July 2010 - 10:42 AM

And just for your information, never use a null layout. Its the only layout whereby you can not guarentee how your application will look on other screen resolutions. If you want to be very precise about layout, and you dont mind going in at the deep end, look into using GridBag Layout. I know people hate this layout, but I think its the best if used properly.
Was This Post Helpful? 0
  • +
  • -

#7 sh1n3  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 164
  • Joined: 22-April 10

Re: Java Swing Components Placement

Posted 03 July 2010 - 10:51 AM

I was using that before because it was real easy like plotting a graph but people discouraged me from using theGridBagLayout. Also had problems with NullLayout when I re-sized the frame so I looked a bit into box layout and flow layout. So now I'm using both of them in 2 panels.
Was This Post Helpful? 0
  • +
  • -

#8 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Java Swing Components Placement

Posted 03 July 2010 - 11:54 AM

Thats one of the major issues of a null layout, because if you resize the application, the x and y positions you set are now in a different place. Whatever layout you start using is more down to what you prefere. If you do look into GridBag, you may want to start with the standard GridLayout. Its similar, just not as complex.
Was This Post Helpful? 2
  • +
  • -

#9 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Java Swing Components Placement

Posted 03 July 2010 - 12:25 PM

I agree with nick. I love GridBagLayout, but it is pretty heavy stuff for a beginner (which is why I didn't list it). It gives you complete control over every aspect of your GUI.
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10573
  • View blog
  • Posts: 39,148
  • Joined: 27-December 08

Re: Java Swing Components Placement

Posted 03 July 2010 - 02:15 PM

@nick2price: As much as I want to give you a + for each of your posts discouraging null layout, I'm going to give you a single + b/c you encouraged GridBagLayout. I have yet to encounter a situation where a combination of BoxLayout, FlowLayout, BorderLayout, and GridLayout haven't been easier to use for the same job in comparison to GridBagLayout. As Luckless said, it gives you absolute control over your GUI. As a developer, I like absolute control. As a designer, I got bit by this with absolute positioning with CSS in web design class. For this reason, it is better to stick to a more flexible and better resizable layout.
Was This Post Helpful? 0
  • +
  • -

#11 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 562
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Java Swing Components Placement

Posted 03 July 2010 - 02:24 PM

To be honest, I dont even know why I am promoting it myself. I used it a few times and liked the complexity of it when it comes to position a component. However, If I am making a gui now, I hardly ever use it.
Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10573
  • View blog
  • Posts: 39,148
  • Joined: 27-December 08

Re: Java Swing Components Placement

Posted 03 July 2010 - 02:28 PM

This is why we leave GridBagLayout to the GUI Generators. Let them deal with the sloppy and cumbersome code, while we do the same thing a lot more elegantly with a lot less code. :)
Was This Post Helpful? 2
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Swing Components Placement

Posted 03 July 2010 - 08:58 PM

View Postsh1n3, on 03 July 2010 - 10:45 AM, said:

But I'd need to import the jar file every time I use them.


Just cut & paste the code into your directory removing the package statement

View Postsh1n3, on 03 July 2010 - 10:45 AM, said:

I could easily do it Box Layout. And is there a way I can use 2 Layouts
together in the same JPanel?

Yes you can mix all them the way you want


Instead of using null layout I ALWAYS manage to achieve what I want with a combination of Box
A vertical Box into which I add horizontal box with struts or glue

Box mainBox = Box.createVerticalBox();

Box line1 = Box.createHorizontalBox();
.... add JComponents to line1 like
line1.add(new JLabel("sdsdsadas");
.... add struts if necessary
line1.add(Box.createHorizontalStrut(10);
line1.add(new JLabel("sdsdsadas");
mainBox.add(line1);

Box line2 = Box.createHorizontalBox();
.... add JComponents to line1 like
line2.add(new JLabel("sdsdsadas");
.... add glue if necessary
line2.add(Box.createHorizontalGlue();
line2.add(new JLabel("sdsdsadas");
mainBox.add(line2);

// your particular cas a right justified JComponent
Box line3 = Box.createHorizontalBox();
line3.add(Box.createHorizontalGlue();
line3.add(new JLabel("sdsdsadas");
mainBox.add(line3);

... then add the mainBox CENTER of a BorderLayout
JPanel p = new JPanel(new BorderLayout());
p.add(mainBox.CENTER);



Hope this helps
Was This Post Helpful? 1
  • +
  • -

#14 sh1n3  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 164
  • Joined: 22-April 10

Re: Java Swing Components Placement

Posted 03 July 2010 - 10:10 PM

Yes it does, Thank you guys for helping me out.
I think I'll go with what pbl said about using
Horizontal and vertical boxes.
Really appreciate it! :-)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1