Jump to content

Aligning JComponents

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1
ClemsonCS

ClemsonCS

    Newbie

  • Members
  • PipPip
  • 28 posts
Hi everyone,

I am learning to use the swing api and need some help on aligning my JComponents.
I have a JDialog window which consists of several JPanels using FlowLayout to put a JLabel and JTextField on the same horizontal axis. These JPanels are then added to another JPanel using BoxLayout with vertical alignment. My JLabels have varying lengths and the rows are centered horizontally in the window so the JTexFields don't line up properly. How can I align things so the JTextFields have perfect horizontal alignment and the JLabels are left justified?

#2
ClemsonCS

ClemsonCS

    Newbie

  • Members
  • PipPip
  • 28 posts
If it will take too much work to left justify the JLabels I could do with just right justifying the JPanels that make up the rows.

#3
Turk4n

Turk4n

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 3,847 posts
Would be great if you didn't double post, lol.

Well you could use this while setting up your layout...

setLayout(null);
While having a layout set to null, you can place your JComponents wherever you want to in the panel/frame.
To position them wherever you want you will have to use.
setBounds(int x,int y,int width,int height)
x - the new x-coordinate of this component
y - the new y-coordinate of this component
width - the new width of this component
height - the new height of this component

Hope this will help you !

Also visit,
Component (Java 2 Platform SE v1.4.2)
So you can get a better grip of functions and usage
Posted Image

#4
ClemsonCS

ClemsonCS

    Newbie

  • Members
  • PipPip
  • 28 posts
I found that I could just RIGHT align my JPanel with flowlayout but I found a use for that method else where in my code. thanks.