Tuesday, May 8, 2012

How to add jInternal frame and jPanel into jDesktopPane

0 comments
jInternal frame is a nothing but another window which can helps to improve GUI in your project. jPanel is another side which is widely use in projects for making better GUI. Can we use jInternal frame and jPanel together?.
Answer is Yes. See how to add jInternal frame and jPanel into jDesktopPane.
  • Make GUI using Netbeans as per your choice.Here is a sample GUI.
  • When you run this GUI,This is look like as..
  • Code for adding jPanel into jDesktopPane.
  private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                       
     jDesktopPane1.removeAll();  
     jDesktopPane1.repaint();  
     NewJPanel panel = new NewJPanel();  
     Dimension screenSize = jDesktopPane1.getSize();  
     panel.setSize(screenSize.width, screenSize.height);  
     jDesktopPane1.add(panel);  
     int width = panel.getWidth();  
     int height = panel.getHeight();  
     panel.setBounds(((screenSize.width / 2) - (width / 2)), ((screenSize.height / 2) - (height / 2)), width, height);  
     panel.setVisible(true);  
   }   
  • The repaint() and removeAll() method is for refreshing jDestopPane. That means if we go for second form to display it overrides on first form and in a result both are visible so for cleanness purpose that methods are used.
  • setBounds() and setSize() methods are used for set frame size as jDesktopPane size set.That methods dynamically adjust size as jDesktopPane size change.
  • Note that main thing we do here,We create the object for jPanel and it add into jDesktopPane. The same thing do for jInternal frame.
  • Output for jPanel Form.
  • Also same for jInternal Frame.
   private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                       
     jDesktopPane1.removeAll();  
     jDesktopPane1.repaint();  
     NewJInternalFrame frame = new NewJInternalFrame();  
     Dimension screenSize = jDesktopPane1.getSize();  
     frame.setSize(screenSize.width, screenSize.height);  
     jDesktopPane1.add(frame);  
     int width = frame.getWidth();  
     int height = frame.getHeight();  
     frame.setBounds(((screenSize.width / 2) - (width / 2)), ((screenSize.height / 2) - (height / 2)), width, height);  
     frame.setVisible(true);  
   }  
  • See the output for jInternal Frame.
  • Implementing both jPanel and jInternal frame together we can build effective GUI.
  • Hope you find best. Thanks for reading.

Leave a Reply