![]() |
|
|
|||||||
![]() |
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 VisionSet |
|
|
|
#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); carlos@gkpwdun.com |
|
|
|
#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(). Igor Planinc |
|
|
|
#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 Chris Smith |
|
|
|
#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. Roedy Green |
|
|
|
#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. 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. Hal Rosser |
|
|
|
#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 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); } > } > Alex Molochnikov |
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| new window minimizes to taskbar | -D- | A+ Certification | 0 | 03-03-2007 01:07 AM |
| New releases: World Trade Center, Jackass Number Two & Wicker Man: Updated complete downloadable R1 DVD DB & info lists | Doug MacLean | DVD Video | 0 | 10-24-2006 06:04 AM |
| Hook Up DVD Player to a Flat Screen PC Monitor? | weft2 | DVD Video | 13 | 02-06-2006 06:14 PM |
| How can I replace a DVD's wide screen movie with a full screen movie from another DVD? | blackhole@aol.com | DVD Video | 15 | 10-19-2005 03:53 PM |
| Panasonic Widscreen 86cm TV Info Needed Screen Size TX-86PW300A. | The Other Guy. | DVD Video | 0 | 01-10-2005 01:32 PM |