Modal Frame - also centers on parent

Submitted by code_admin on Fri, 07/20/2018 - 12:40

How to use method in my common library:

Frame Constructor

  1.     public trunkObjectFrame(Rectangle p_rect) {
  2.         try {
  3.             jbInit();
  4.             if (null == p_rect) {
  5.                 Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
  6.                 this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
  7.             } else {
  8.                 Point p = new Point((int)p_rect.getCenterX() - (this.getWidth()/2),(int)p_rect.getCenterY()- (this.getHeight()/2));
  9.                 this.setLocation(p);            
  10.             };
  11.         } catch (Exception e) {
  12.             e.printStackTrace();
  13.         }
  14.     }

Frame jbInit

  1.     private void jbInit() {
  2.         this.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
  3.        
  4.         this.getContentPane().setLayout(borderLayout1);
  5.         this.setSize(new Dimension(600, 400));
  6.         this.setTitle( "Trunk Object Browser" );
  7.         this.setResizable(true);
  8.     }

Action to open

  1.     private transient Action m_ViewTrunkObjectsAction = new AbstractAction() {
  2.         {
  3.             putValue(NAME, "OpenModalFrame");
  4.         }
  5.  
  6.         public void actionPerformed(ActionEvent e) {
  7.             //Find parent that is a frame for getbounds
  8.             Component comp = m_SVNMainPanel;
  9.             while ( !( comp instanceof Frame ) )
  10.             {
  11.             comp = comp.getParent();
  12.             }
  13.             Frame ownerFrame = (Frame)comp;            
  14.             trunkObjectFrame dlg;
  15.             try {
  16.                 dlg = new trunkObjectFrame(ownerFrame.getBounds());
  17.             } catch (Exception f) {
  18.                 JOptionPane.showMessageDialog(null, "There was an error reading data from settings file " + f.toString());
  19.                 f.printStackTrace();
  20.                 return;                                
  21.             }
  22.             //dlg.setVisible(true);
  23.             ModalFrameUtil.showAsModal(dlg, ownerFrame);
  24.             ownerFrame.setVisible(true); //make sure owner stays visible when frame is closed
  25.         }
  26.     };

Tags

RJM Article Type
Work Notes