• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

How do I center my Dialog window on the screen?

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
jkteater
Asked:
jkteater
  • 4
1 Solution
 
CEHJCommented:
Try
centerPanel.setLocationRelativeTo(null);        
              
      //Display the window.
      centerPanel.setVisible(true);

Open in new window

0
 
jkteaterAuthor Commented:
undefined for type JPanel
0
 
CEHJCommented:
Sorry - i thought that was the name of a JDialog. You need to call that on the dialog instance
0
 
CEHJCommented:
btw, the reason i thought that was that you called setVisible on it (JPanel is visible by default)
0
 
CEHJCommented:
:)
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now