aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Placing a component at center both vertically and horizontally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Placing a component at center both vertically and horizontally " Watch "Placing a component at center both vertically and horizontally " New topic
Author

Placing a component at center both vertically and horizontally

Sandeep Ghosh
Ranch Hand

Joined: Jan 23, 2002
Posts: 145
Hi Friends,
This code displays a textfield and button at the top of the frame .How could I put these two component just at the center of frame both horizontally and vertically.
Thanks in adv.
Sonu

[ June 13, 2002: Message edited by: Dirk Schreckmann ]
alex earnshaw
Ranch Hand

Joined: Nov 05, 2001
Posts: 60
In your TestPanel constructor create a new panel, add your button/text field to that panel. Set the layout of the TestPanel to BorderLayout, then add the panel containing the button/text field to the CENTER of the TestPanel.

[ June 13, 2002: Message edited by: alex earnshaw ]
Sandeep Ghosh
Ranch Hand

Joined: Jan 23, 2002
Posts: 145
Hi Alex,
Thanks for prompt reply,but problem still remain.2 Component is still shown at the top of the JFrame.
I am still waiting for the ans.
Thanks in adv
Sandeep
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
You have to get the screen size using the default toolkit first, then call setBounds passing as follows...
yourframe.setBounds(yourwindow.width/4, yourwindow.height/4, yourwindow.width/2, yourwindow.height/)
This is a crude way. I believe swing has some methods that may do it for you.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Sandeep Ghosh
Ranch Hand

Joined: Jan 23, 2002
Posts: 145
Hi,
Thanks a lot for the soloution.I am also looking for some solution in swing and layout manager.Does anybody have any suggestion regardng this problem.

Sandeep
Luis Araujo
Greenhorn

Joined: Aug 27, 2001
Posts: 6
Check the Interface LayoutManager2. This should help.
Sandeep Ghosh
Ranch Hand

Joined: Jan 23, 2002
Posts: 145
Hi Friend,
Problem with earlier suggestion that is using setBound(int,int,int,int) is that it is ignoring this method and using BorderLayout.Do I have to change the Layout.About using LayoutManager2 could you be more specific.
Thanks in adv
Sonu
Sandeep Ghosh
Ranch Hand

Joined: Jan 23, 2002
Posts: 145
Hi Friend,
I am trying this one,but frmSize.width and frmSize.height is showing 0.0 and 0.0.Why?
Please anybody have any solution.
Thanks in adv.
Sonu
public mainapplic() {
TestPanel tp=new TestPanel();
Container c=getContentPane();
c.setLayout(xYLayout2);
Dimension dlgSize = tp.getPreferredSize();
Dimension frmSize = getPreferredSize();
Point loc = tp.getLocation();
System.out.println(frmSize.getWidth()+":"+frmSize.getHeight());
c.add(tp);
tp.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
setSize(400,400);
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent evt)
{
System.exit(-1);
}});
}
[ June 15, 2002: Message edited by: Sonu Ghosh ]
Shashi Kanta
Ranch Hand

Joined: May 08, 2002
Posts: 89
hi sonu,
u r trying to get the width and height in the constructor!!
get the width and height in some other method.
since the frame has not been yet created when u r calling the constructor, it'l give zero values.
by the way do u want to center ur text only or some components too? if text only, i can give u the code that'll work both in swings and awt.
rgds,
Shashi
Shashi Kanta
Ranch Hand

Joined: May 08, 2002
Posts: 89
sorry, i can do with other components too.
if u r still having problem in putting the text and the buttons in the center of the table, i can help u.
rgds
shashi
Sandeep Ghosh
Ranch Hand

Joined: Jan 23, 2002
Posts: 145
Hi Friend,
My Components(JTextField and JButton) are in JPanel.I want to put the JPanel in the center of the JFrame.Please help me,I am still waiting for the soloution.
Thanks in adv
Sonu
Shashi Kanta
Ranch Hand

Joined: May 08, 2002
Posts: 89
hi sonu,
first of all, i have to tell u that the BorderLayout doesn't not respect the sizes of its components! so when u add a component to a container having BorderLayout, the component's size will be set according to the space available in the container. if u add 2 components, the available space will be divided equally among them and if u add one component only, the whole of the conatiner's size will be given to the component! thats what is exactly happening in your case. the JPanel gets all the space of the JFrame. and since in a JPanel, the components are laid out in a FlowLayout fashion, the components are displayed from top. so u see the JTextField and the JButton on top.
if u have only one component to add to a conatiner and want the component to be at the center, use either a GridBagLayout manager or don't use any layout, i.e. set the layout of the container to null and then place the component accordingly.
i have used null layout.
here's the code u want.

///
hope thats what u want.
rgds
Shashi
Sandeep Ghosh
Ranch Hand

Joined: Jan 23, 2002
Posts: 145
Hi Shashi ,
Sorry couldn't replied earlier.Thanks a lot for the help.
sonu
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Placing a component at center both vertically and horizontally