Centering JFrame on desktop problem

I have this code which centers JFrame on the desktop and which works fine only outside the constructor of the JFrame class. How can I make sure that the JFrame window is centered on the desktop when it opens?
GraphicsConfiguration gc = this.getGraphicsConfiguration();
Rectangle bounds = gc.getBounds();
int x = (int) ( bounds.getWidth() - this.getWidth() ) /2 ;
int y = (int) ( bounds.getHeight() - this.getHeight() ) /2;
this.setLocation( x, y );
The problem is that at "constructor time" the width and height are 0 (in this.getWidth and this.getHeight methods of JFrame).
any alternative to make it work?
thanks

I order to center the JFrame you can always get the screen size then simply set it's bounds e.g.
java.awt.Toolkit tk = java.awt.Toolkit.getDefaultToolkit();
Dimension scrSize = tk.getScreenSize();
Then depending on it's size determine the size you want the JFrame, and calculate the x, y co-ordinates. Making the size a fraction of the screen size, is easy to center e.g.
youJFrame.setBounds((int)(scrSize.getWidth()-(scrSize.getWidth()*.75)), (int)(scrSize.getHeight()-((int)scrSize.getHeight()*.75)),
(int)(scrSize.getWidth()*0.5), (int)(scrSize.getHeight()*0.5));

Similar Messages

  • Centering windows on desktop

    I have this code which centers JFrame on the desktop and which works fine only outside the constructor of the JFrame class. How can I make sure that the JFrame window is centered on the desktop when it opens?
    GraphicsConfiguration gc = this.getGraphicsConfiguration();
    Rectangle bounds = gc.getBounds();
    int x = (int) ( bounds.getWidth() - this.getWidth() ) /2 ;
    int y = (int) ( bounds.getHeight() - this.getHeight() ) /2;
    this.setLocation( x, y );
    The problem is that at "constructor time" the width and height are 0 (in this.getWidth and this.getHeight methods of JFrame).
    any alternative to make it work?
    thanks

    well try this then. call that method "after" you assign a size. The reason the (0,0) coordinate is centered is because you fram had no size when you set the location if you call it again AFTER you give it a size then it will relocate itself to be centered on your desktop

  • I have a brand new mac book pro and suddenly I have a pixelated huge red diamond that showed up centered on my desktop.  What is it and how do I get rid of it.

    I have a brand new mac book pro and suddenly I have a pixelated huge red diamond that showed up centered on my desktop.  What is it and how do I get rid of it.

    No idea, take it back to the store, preferably with a camera shot or a command shift 3 screenshot
    Good Luck.

  • Fresh install of trial version of Captivate 8 on OS X 10.9.3 - no previous version of Captivate on machine.  I've got lots of crashing to desktop problems right off the bat.  Is there a patch available?

    Fresh install of trial version of Captivate 8 on OS X 10.9.3 - no previous version of Captivate on machine.  I've got lots of crashing to desktop problems right off the bat.  Is there a patch available?  I work at a University and we're evaluating for online courseware - my last experience with Captivate was 5.0.  Anyone else experience this with the trial download?

    Hi there,
    There is no difference in terms of usage and features between a trial version and a licensed version.
    Could you tell me do you get any specific error message when it crashes, or does it crash while using a particular feature, or is it random?
    Can you also try with a different user account (admin) on your MAC, check if it behaves same there?
    Thanks.

  • HT201321 Finder flashing, "Window Restore", desktop problems

    I've never seen this happen before. My finder icon bounces in the dock while a window with this warning is up, "Window Restore problem would you like to restore or not restore?" Since it is flashing on and off you can't click either button. What we found was there was a file on the desktop it did not like, I made a folder inside the main HD and put the items from the destop in it and deleted what was on the desktop. This stopped the Finder bouncing problem but Indesign and Acrobat keep crashing. I can log in as admin and it works well but need to fix the desktop problem. Any ideas?

    Hi,
    Does the issue occur just after the installation or after some changes?
    Please try the follow step to see if it works:
    1.Open Task Manager by right-click on Taskbar and select Start Task Manager or by using keyboard shortcut "Ctrl+Shift+Esc".
    2.Click on More Details button and go to "Processes" tab.
    3.Now look for "Windows Explorer" process. As soon as you click on it, you'll get a new button "Restart" at the bottom-right corner of the window.
    4.You can also right-click on the process and select "Restart" option.
    If it doesn't work,please try the safe mode and clean boot mode to check the result.
    How to perform a clean boot to troubleshoot a problem in Windows 8.1, Windows 8, Windows 7, or Windows Vista
    http://support.microsoft.com/kb/929135
    Regards,
    Kelvin hsu
    TechNet Community Support

  • Desktop problems after Snow Leopard Installation

    After a successful installation of Snow Leopard I am having problems with my Desktop. No matter what Desktop graphic I choose, after 2-3 minutes the Desktop goes back to a plain blue background. Any idea what is causing this? and how to fix it?

    Hmm,
    I was not familiar with EarthDesk but now that I know about it - I conjecture that application runs in the background, right?
    If it does run in the background - then that is the real base of the problem installing Snow Leopard. Others with (other) apps running in the background have experienced repeated run-then-eject-installer problems. One person reported that CarbonCopyCloner's scheduling app caused this specific problem.
    Now, I understand that the desktop problem isn't the same as having the installer begin to run, then stop and eject the installer disk. My point is only that apps running in the background are apparently causing (now various) problems with the SL installer.
    Glad to see that one more problem has been solved.

  • Desktop Problem-Icons Won't Work or Won't Adjust

    Yesterday my desktop icons decided to not work. I could not open from the desktop, move folders, etc. It is as if they were all locked. I COULD open Finder, go to Desktop, and open files and start apps from there. Then I went to View Options, clicked Snap To Grid, and all icons moved. I then turned it off (None). But the icons won't respond. They are stuck in "Snap to Grid." I still can't open a folder on the desktop, but applications will now start.
    This is a bizarre one. Any help would be appreciated.

    I've been having this problem as well, restarting only corrects it for about an hour or so. Was not able to find the library com.apple.finder.plist file in my library and then did a universal search using that string and no results. Not sure why other people have that preference file and I don't. This is a frustrating problem that will influence my decision to purchase new Macs in the future. It's a real time waster. In addition, often when I click on a folder, it comes up empty, even though there may be hundreds of files in the folder. It seems it's part of the same problem, inasmuch as the finder can't process data nodes correctly. The only solution is to open the folder, close it, and open it again and then the files inside it appear, albeit slowly.

  • Cisco VPN Software and Remote Desktop Problem

    Hello-
    Please have patience as I just got my first mac and I'm really just learning how everything works.
    Anyway, I'm trying to connect to my office network from my iMac (OS X 10.5.3) using the Cisco VPN client ver. 4.9.01 (0030) and the Remote Desktop Connection v2.0.0 Beta 3. I'm able to connect via the VPN but I cannot connect to the remote machine using either the machine's hostname or IP address.
    In order to troubleshoot the issue, I tried to ping the remote machine after connecting via VPN and it fails. The name is resolving but ping fails with a "No route to host" and then "Host is down error." However, I know the host is up since when I connect via VPN from my PC I can ping the remote machine without a problem.
    Anyone have any ideas? Does the Cisco VPN client for Mac not work well? Is there some simple configuration setting I'm missing somewhere? BTW, I'm connecting to the Internet via WiFi and am using a Linksys WRT54G router.
    thx,
    ian

    Hello Ian:
    Welcome to Apple discussions (and to the wonderful world of Macs). I think you will find these forums represent the best on-line community anywhere - and everyone is a volunteer!
    I am no help with your problem as I have not used a Cisco VPN. However, in my previous business life I did connect my home Mac to my business VPN. I found that our IT people were most helpful in smoothing connection issues.
    This forum addresses connection issues if you want to post there:
    http://discussions.apple.com/forum.jspa?forumID=1222
    Barry

  • Portege m700 Tablet Extended Desktop Problem

    I've looked all over and can't find a good answer to this, so I figured I'd better check with the source!  I have a Toshiba Portege m700 tablet PC, which has an LCD monitor connected using the digital cable.
    I have the external monitor set as my primary, with the notebook monitor set as the secondary, providing extra desktop real estate to the right of the first monitor.
    The problem is that when I use the touchscreen, the mouse controls are only active on the primary monitor (the external one).  What I'd like to do is have the external monitor be primary, the notebook monitor be secondary, and be able to drag items to the notebook and use ink, or onenote, or other touchscreen apps on there.  I want the touchscreen to control ONLY the secondary (notebook) monitor.  Is this possible?
    It seems like there should be some way to just instruct the computer to map the touchscreen to the secondary monitor instead of always just mapping to the primary.
    However, if there is not, what I did experiment with is having the notebook as primary and the external as secondary, and then using some sort of multimonitor software to manage the taskbar and such to create the illusion of the external monitor being primary.  The problem here is that while the notebook is 1280 x 800 resolution, the external monitor is capable of 1280 x 1024, but when set as secondary will go no higher than 1280 x 800.  Is there a way to force the external monitor to run in 1280 x 1024 if I'm using it as the secondary?
     Is this something for which I need an updated graphics driver?  I tried to get a newer version of the Intel GMA, but it actually removed all extended desktop options for me, so I rolled it back.  The version it's running is 6.14.10.4837.
    Thank you in advance for anyone pointing me to any driver updates, configuration directions, or external software that can effectively manage all these options of extended desktop, screen resolution, and what monitor is assigned to touchscreen control!

    I've just tested the "Extended Desktop for Tablet PC" powertoy, and that doesn't solve this problem.  With the tablet set as the secondary monitor, when I move the window onto that screen, it just recurses the image over itself, which isn't really useful.
    No one has any other ideas about this?

  • IMac 27" extended desktop problems

    Hi!
    I own a 2011 iMac 27" and I am having issues using a Samsung SyncMaster T240M display as extended desktop.
    It works fine when I am just using the computer for a while, but if the displays go to sleep mode, when I return to the computer the iMac display restores right away but then the Samsung display starts to go on and off repeatedly until it finally stays on, but then all my applications are on the iMac display.
    It is pretty much like what would happen if I had unplugged the monitor and plugged it back in.
    Is there a way to fix this or do I just have to live with that? Has anyone experienced the same problem with other displays? Maybe this Samsung display is not syncing well with the iMac?
    Anyway, any help will be very much appreciated.
    See you,
    JP

    If the machine is covered by warranty or even better by AppleCare then contact AppleCare. They will be able to do in-home service if you are within 50 miles of an AASP. You will have to request this service but they will do it.

  • Aperture import and desktop problem

    My Aperture main screen had changed and can not be fully used, as some pictures are not vissible in the browser view. As well the picture if shown in full screen in the main screen is not shown properly - 1/4 on the right side is missing.
    Today I realized, that the missing 1/4 of the working desk is impacting the import - the menu to put create new project and all the data of the picture is missing - so totally impossible to import new pictures.
    Does any one know how to adjust the desk in Aperture and show/hide the importing dialogues?
    help appreciated.

    Hello Milo,
    a similar problem occured recently, see this discussion.
    The problem could be solved by removing the preferences file to the Desktop and restarting Aperture, for Aperture stores the layout of the main window in this file.
    The preferences file is ~/Library/Preferences/com.apple.Aperture.plist
    That file is in your User Library.
    Quit Aperture, if it is running,
    To reveal the hidden User Library in Lion, activate the Finder by clicking on the Desktop, then go to the main Finder menu at the top of the screen, and select the menu Go -> Go to Folder.
    in Lion now hold down the alt-or-option key, and your User Library will appear in the Drop down menu. Select it to open in Finder. (in other MacOS you can access your User Library by opening your Home Folder from the Finder Sidebar).
    Once your User Library is  revealed in the Finder, navigate to the sub folder "Preferences" and scroll down the long list, until you find
         com.apple.Aperture.plist
    move it to the desktop.
    Log off and log on again.
    restart Aperture and see, if the fix worked.
    And please, post back and tell us, if you were successful
    Regards
    Léonie

  • Java desktop problem after patch 118870-01 install

    FYI
    Java desktop stopped functioning properly after the installation of patch 118870-01, part of the recommended patch cluster.
    Symptoms included no longer being able to launch mozilla, and upon reboot, no longer being able to login to java desktop.
    patchrm 118870-01 fixed the problem.

    Found that patch 119593-01 also appears to be causing the problem, along with patch 118870-01.
    Fun :)

  • JFrame.pack() causes problems with jtabbedpane(making new tabs)and resizing

    I have a JFrame with a JTabbedPane inside of it. Inside of each tab I put a JPanel with a JTextArea in it. When I create a new tab, I do something like textArea.requestFocusInWindow();
    However, this will not work unless I do a frame.pack() right after I created the new tab and right before that line of code.
    This in turn causes another problem. If I resize the jframe and then create a new tab, the window will snap back to the size it had before creating the tab. I assume this has to do with the pack() function, however nothing else I try will make the cursor blink in the JTextArea.
    Is there a solution to this...either something to make the cursor blink or something other than the pack function to update the JFrame?
    Thank you

    This posting should help you out:
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=581478

  • Dragging photos to the desktop problem

    Hi, would anyone know why I can't suddenly drag my latest photos to the desktop, while the rest of my photos can be dragged just fine? thanks a lot

    It's not a bug but probably a font problem.  iPhoto requires the Helvetica Neue font for it's operation.  So launch Font Book and make sure you have the  Helvetica Neue font.  If you have it, deactivate it, close Font Book, reopen font book, find the Helvetica Neue font and reactivate it. 
    Then launch iPhoto and try again.
    OT

  • Centering ap div position problem

    Hi people, I'm new to dreamweaver and have what seems to be a basic problem but one that I can't fix, even after looking on the forums and web.
    My problem is when I go into design mode I can drag my "ap divs" around so they are in the centre of my screen (webpage) and nicely lined up, however once I test the website in any web-browser they are no longer centered and seem to reposition themselves at random. I've included some screen-shots so you can see what I mean.
    Website in design view
    Website in webbrowser
    Here is my code so you can look at it and spot my mistakes.
    <title>index</title>
    </head>
    <style type="text/css" >
    body
    background-image:url('background.png');
    background-repeat:no-repeat;
    background-position:50% -2%;
    background-color:#D7D7D7;
    </style>
    <style type="text/css">
    #apDiv1 {
              position:absolute;
              width:578px;
              height:57px;
              z-index:1;
              left: 535px;
              top: 1526px;
              overflow: hidden;
              background-color: #transparent;
              color: #F90;
    </style>
    <style type="text/css">
    #apDiv2 {
              position:absolute;
              width:937px;
              height:288px;
              z-index:1;
              left: 360px;
              top: 14px;
              overflow: hidden;
              background-color: #transparent;
              color: #F90;
    </style>
    <div align="center" id="apDiv1">This page was created by me on 28th Decmber 2099. Updated - 29th December 2099. </div>
    <div align="center" id="apDiv2"><img src="banner.png" width="928" height="284" /></div>
    </html>
    If you can see the solution to my problem or if there is another/better way of centering text/banners using ap divs, then please let me know. Thanks in advance.

    The biggest mistake that those new to Dreamweaver make is using Absolutely Positioned <divs>.
    Why, because in most instances they will 'restrict' your construction. By their nature being Absolutely Positioned they do NOT move away from the co-ordinates that you give them. What you are seeing is the background image moving, its an illusion.
    I would never advise anyone to use AP positioning unless they have a great deal of experience in positioning.
    One way to control AP <divs> is to surround them in a 'wrapper' <div> (see code below)
    <div id="wrapper">
    <div align="center" id="apDiv1">This page was created by me on 28th Decmber 2099. Updated - 29th December 2099. </div>
    <div align="center" id="apDiv2"><img src="banner.png" width="928" height="284" /></div>
    </div><!-- end wrapper -->
    Then use css to make the wrappers position 'relative' and to center it horizontally using margin: 0 auto (see below)
    #wrapper {
    position: relative;
    width: 578px;
    margin: 0 auto;
    Your AP <divs> will now take their co-ordinates from the top/left of the 'wrapper' <div>
    So alter your left positioning to 0 in your apDiv1 and apDiv2 (see below)
    #apDiv1 {
              position:absolute;
              width:578px;
              height:57px;
              z-index:1;
              left: 0;
              top: 1526px;
              overflow: hidden;
              background-color: #transparent;
              color: #F90;
    #apDiv2 {
              position:absolute;
              width:937px;
              height:288px;
              z-index:1;
              left: 0;
              top: 14px;
              overflow: hidden;
              background-color: #transparent;
              color: #F90;
    As the default position is left you don't really need to declare it but it does no harm if you do.

Maybe you are looking for

  • SELECT query - problem with SORT

    Hi, Same select query behaves different way on development and quality server on Development it returns unsorted and Quality returns sorted it can be resolved by SORT but why does it behave differently Do you know what is setting in database thanks

  • MAC (10.6.2) and I Movie 3.0.3 problem

    I have been using Imovie for some years now and today was the first time I have gone into this software since we upgraded to Snow leopard. I have a brand new video on my digital camera I want to import. (Something I have done many times before) When

  • How to use Flash buttons/animation in a Swing/Applet?

    Hello, I want to use flash button as like as JButton that we use in Swing/ Applet. How can I do So? Please help me. Thanks-

  • Error occured during installation of iTunes...

    During installation of iTunes i got the following message: An error occurred during the installation of assembly "Microsoft.VC80.CRT, version="8.0.50727.4053", type="win32", publicKeyToken="1fc8b3b9a1e18e3b",processor Architecture="x86". Please refer

  • I loaded a disc into iTunes and some of the tracks are double listed in iTunes.

    Yeah, I loaded "Imagine" by John Lennon into iTunes on my iMac. Some of the tracks came up in iTunes double listed. I tried dragging one of the listings to trash and it said it'd remove the track (oh no!). I have unticked the duplicate tracks atm, bu