• Post Reply Bookmark Topic Watch Topic
  • New Topic

Window adjusting question

 
Raj Medha
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me tell you that I'm very new to Swing !!
I've made a screen it has 4 Radiobutton. I created the window with the following :
Toolkit theKit = window.getToolkit();
Dimension wndsize = theKit.getScreenSize();
window.setBounds(wndsize.width/4,wndsize.height/4,
wndsize.width/2,wndsize.height/2);
window.addWindowListener(new WindowHandler());
window.setVisible(true);
then I added my Radiobutton to it by using :
radioAdd.setBounds(150,50,150,20);
radioEdit.setBounds(150,80,150,20);
radioDelete.setBounds(150,110,150,20);
radioExit.setBounds(150,140,150,20);
when I execute the program, I get the window in the middle of the screen and all my Radiobutton in it. - Perfect !! But, when I maximise the window, the radiobutton stays at the position as defined above (ofcourse) I need to adjust the Radiobutton into the center of the window when maximise the window and when minimise it, I need it again to the center.
How do you do it ? Please let me know.
thanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's OK to use setBounds() for a JFrame or JDialog, but don't use it for an ordinary component, except in very special circumstances. You've already found out one reason why.
You have to use a layout manager. A layout manager is an object that includes logic to arrange components nicely inside a container. The Java tutorial has a section on them as part of its chapter on using Swing. I'd advise you to work through as much of this tutorial as you can manage -- you will learn a lot.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the Swing tutorial on using layout managers.

Layout managers specify how components are displayed in relation to one another without having to use any hard coded locations. In your case you could add all of your radiobuttons to one JPanel using either a centered FlowLayout or a GridLayout and then add the JPanel to the center of the frame using the default BorderLayout of the frame's content pane. A centered FlowLayout would cluster all the radiobuttons in the center, giving each the amount of space it needed. A GridLayout would evenly distribute the radiobuttons across the width of the whole screen.

The code would look something like this for the FlowLayout -
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!