![]() |
|
|
|||||||
![]() |
Java - How to place a window in the center of the screen ? |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
I would like to open a window and to placed it at a certain position on a screen
(e.g. in the center). How do I do this? My (simplified) current code looks like: public class testGBL extends JFrame implements ActionListener { JLabel lab1; .... JButton butBack; public testGBL() { ... Container contentPane = getContentPane(); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); contentPane.setLayout(gridbag); c.fill = GridBagConstraints.HORIZONTAL; butBack = new JButton("Back"); c.gridx = 0; c.gridy = 0; c.weightx = 1.0; c.gridwidth = 2; gridbag.setConstraints(butBack, c); butBack.setEnabled(false); butBack.addActionListener(this); contentPane.add(butBack); .... } public static void main(String args[]) { testGBL window = new testGBL(); window.setTitle("test"); window.pack(); window.setVisible(true); } } Tom Parson |
|
|
|
|
#2 |
|
Posts: n/a
|
"Tom Parson" <> wrote in message news:4379d7b5$0$21942$... > I would like to open a window and to placed it at a certain position on a screen > (e.g. in the center). window.setLocation(int x, int y); or if you want it in the centre use this cludge: window.setLocationRelativeTo(null); should take a component as the argument and centre it on that -- Mike W |
|
|
|
#3 |
|
Posts: n/a
|
>I would like to open a window and to placed it at a certain >position on a screen (e.g. in the center). > >How do I do this? My (simplified) current code looks like: > >public class testGBL extends JFrame implements ActionListener { > JLabel lab1; > .... > JButton butBack; > > public testGBL() { > > ... > Container contentPane = getContentPane(); > GridBagLayout gridbag = new GridBagLayout(); > GridBagConstraints c = new GridBagConstraints(); > contentPane.setLayout(gridbag); > > c.fill = GridBagConstraints.HORIZONTAL; > > butBack = new JButton("Back"); > c.gridx = 0; > c.gridy = 0; > c.weightx = 1.0; > c.gridwidth = 2; > gridbag.setConstraints(butBack, c); > butBack.setEnabled(false); > butBack.addActionListener(this); > contentPane.add(butBack); > .... > } > > public static void main(String args[]) { > testGBL window = new testGBL(); > window.setTitle("test"); > window.pack(); > window.setVisible(true); } > } Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); int X = (screen.width / 2) - (widthWindow / 2); // Center horizontally. int Y = (screen.height / 2) - (heightWindow / 2); // Center vertically. window.setBounds(X,Y , widthWindow,heightWindow); |
|
|
|
#4 |
|
Posts: n/a
|
Tom Parson wrote:
> I would like to open a window and to placed it at a certain position on a screen > (e.g. in the center). > > How do I do this? My (simplified) current code looks like: > > > > public class testGBL extends JFrame implements ActionListener { > JLabel lab1; > .... > JButton butBack; > > public testGBL() { > > ... > Container contentPane = getContentPane(); > GridBagLayout gridbag = new GridBagLayout(); > GridBagConstraints c = new GridBagConstraints(); > contentPane.setLayout(gridbag); > > c.fill = GridBagConstraints.HORIZONTAL; > > butBack = new JButton("Back"); > c.gridx = 0; > c.gridy = 0; > c.weightx = 1.0; > c.gridwidth = 2; > gridbag.setConstraints(butBack, c); > butBack.setEnabled(false); > butBack.addActionListener(this); > contentPane.add(butBack); > .... > } > > public static void main(String args[]) { > testGBL window = new testGBL(); > window.setTitle("test"); > window.pack(); > window.setVisible(true); } > } > ..setLocation() or .setBounds(). |
|
|
|
#5 |
|
Posts: n/a
|
Tom Parson <> wrote:
> I would like to open a window and to placed it at a certain position on a screen > (e.g. in the center). There's an easy way to center a Swing JFrame on the screen: f.setLocationRelativeTo(null); Do this after the pack() or setSize(...) call. -- www.designacourse.com The Easiest Way To Train Anyone... Anywhere. Chris Smith - Lead Software Developer/Technical Trainer MindIQ Corporation |
|
|
|
#6 |
|
Posts: n/a
|
On 15 Nov 2005 12:42:30 GMT, (Tom Parson) wrote,
quoted or indirectly quoted someone who said : >I would like to open a window and to placed it at a certain position on a screen >(e.g. in the center). > >How do I do this? My (simplified) current code looks like: see http://mindprod.com/jgloss/coordinates.html -- Canadian Mind Products, Roedy Green. http://mindprod.com Java custom programming, consulting and coaching. |
|
|
|
#7 |
|
Posts: n/a
|
Tom Parson wrote:
> I would like to open a window and to placed it at a certain position on a screen > (e.g. in the center). > > How do I do this? My (simplified) current code looks like: > > > > public class testGBL extends JFrame implements ActionListener { ....snip Tom, You can call the the setBounds method to fine tune the components location and size on the screen. A java.awt.Toolkit object will also be helpful if you want information son the screen size in order to place your component in a relative location. Carl. |
|
|
|
#8 |
|
Posts: n/a
|
"Tom Parson" <> wrote in message news:4379d7b5$0$21942$... > I would like to open a window and to placed it at a certain position on a screen > (e.g. in the center). > > How do I do this? My (simplified) current code looks like: > > > > public class testGBL extends JFrame implements ActionListener { > JLabel lab1; > .... > JButton butBack; > > public testGBL() { > > ... > Container contentPane = getContentPane(); > GridBagLayout gridbag = new GridBagLayout(); > GridBagConstraints c = new GridBagConstraints(); > contentPane.setLayout(gridbag); > > c.fill = GridBagConstraints.HORIZONTAL; > Take a look at the Toolkit (getDefaultToolkit) and its getScreenSize() method which returns a DImension object, which has width and height properties of the screen. Then you use the setBounds method of the JFrame to set the x,y,width,and height from your meticulous calculations. |
|
|
|
#9 |
|
Posts: n/a
|
after packing the window, you must set the position of the window like
this. left_of_window = (screen_resolution_width - width_of_the_window)/2 top_of_the_window = (screen_resolution_height - height_of_the_window) /2 this would center the window in the screen. /Cheers, -MadGuy |
|
|
|
#10 |
|
Posts: n/a
|
To place the JFrame at the desired origin, use setLocation() method. To
center JFrame on the screen, use setLocationRelativeTo(null). Reading the JFrame docs could also help. "Tom Parson" <> wrote in message news:4379d7b5$0$21942$... > I would like to open a window and to placed it at a certain position on a screen > (e.g. in the center). > > How do I do this? My (simplified) current code looks like: > > > > public class testGBL extends JFrame implements ActionListener { > JLabel lab1; > .... > JButton butBack; > > public testGBL() { > > ... > Container contentPane = getContentPane(); > GridBagLayout gridbag = new GridBagLayout(); > GridBagConstraints c = new GridBagConstraints(); > contentPane.setLayout(gridbag); > > c.fill = GridBagConstraints.HORIZONTAL; > > butBack = new JButton("Back"); > c.gridx = 0; > c.gridy = 0; > c.weightx = 1.0; > c.gridwidth = 2; > gridbag.setConstraints(butBack, c); > butBack.setEnabled(false); > butBack.addActionListener(this); > contentPane.add(butBack); > .... > } > > public static void main(String args[]) { > testGBL window = new testGBL(); > window.setTitle("test"); > window.pack(); > window.setVisible(true); } > } > |
|