• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Placing a component at center both vertically and horizontally

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sandeep Ghosh
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check the Interface LayoutManager2. This should help.
 
Sandeep Ghosh
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shashi ,
Sorry couldn't replied earlier.Thanks a lot for the help.
sonu
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic