Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I center my Dialog window on the screen?

Posted on 2011-09-29
5
Medium Priority
?
179 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 200 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

877 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