Solved

How do I center my Dialog window on the screen?

Posted on 2011-09-29
5
164 Views
Last Modified: 2012-05-12
When my Panel opens it is always on the upper left side of the screen.  How can I have it open in the center of the screen?

public void createDialog() {
	  
	  appReg = Registry.getRegistry(this);
	  setTitle(appReg.getString("edi.TITLE"));
	   	   
	  Component selectionsPanel = selectedTable();
   	  Component buttonPanel = OKCancelButtons();
   	  Component currentPanel = currentItems();
   	  centerPanel = new JPanel();
      centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));
      centerPanel.setPreferredSize(new Dimension(750, 350));
            
      centerPanel.add(Box.createRigidArea(new Dimension(5, 0)));
      centerPanel.add(currentPanel);
      centerPanel.add(selectionsPanel);
      centerPanel.add(Box.createHorizontalGlue());
      centerPanel.add(Box.createRigidArea(new Dimension(0, 2)));
      centerPanel.add(buttonPanel);
      
      getContentPane().add(centerPanel);
      
      addComponentListener(new ComponentAdapter() {
        public void componentResized(ComponentEvent event) {
           Component c = event.getComponent();
           c.setSize(Math.max((int)centerPanel.getPreferredSize().getWidth()/2, c.getWidth()),
                     Math.max((int)centerPanel.getPreferredSize().getHeight()/2, c.getHeight()));
        }
      });
  	  	
      this.pack();
      setLocation(getParent().getLocation(null));        
              
      //Display the window.
      centerPanel.setVisible(true);
        
   }// end createDialog()

Open in new window

0
Comment
Question by:jkteater
  • 4
5 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 36815246
Try
centerPanel.setLocationRelativeTo(null);        
              
      //Display the window.
      centerPanel.setVisible(true);

Open in new window

0
 

Author Comment

by:jkteater
ID: 36815304
undefined for type JPanel
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36815558
Sorry - i thought that was the name of a JDialog. You need to call that on the dialog instance
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36815568
btw, the reason i thought that was that you called setVisible on it (JPanel is visible by default)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36892139
:)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 85
Java SE 8u111  Lot of stuff broke 11 54
couple of eclipse 5 19
servlet filter example 37 41
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now