Wednesday 7 October 2009

Restoring minimized JInternalFrame.

This is going to be straight forward as I assume you have got some basic or perhaps an advance knowledge of Java Swing JInternalFrame. Also if you are a beginner opting to learn more about Java Swing and it's components, then I suggest you visit here .

Straight to what we are here for - Assuming you have a button, on which when you click will open an InternalFrame. At times you might want to restrict the frame from opening again if it is currently opened, and then perhaps restore it to JDesktop if perhaps minimized. Now lets look into how to block the frame from been opened twice.

We will start by creating a helper bean which will hold the status of the internalFrame. Note this status will either be displayed or not displaying.

Helper bean:
  public final class global
  {
    public global()
    {
    }

    private boolean displayed;

    public boolean isDisplayed()
    {
      return displayed;
    }

    public void setDisplayed(boolean displayed)
    {
      this.displayed = displayed;
    }
  }

We then move on to creating the JInternalFrame. Notice that the code below set the status of Displayed to false in the helper bean, anytime the iFrame is closed. In doing so, the button ActionEvent will determine the status as not displayed and hence open the frame.

I implement it this way..
  public class iFrame extends javax.swing.JInternalFrame
  {
    /** Creates iFrame */
    public iFrame()
    {
      initComponents();
      addInternalFrameListener(new InternalFrameAdapter()
      {
        public void internalFrameClosed(InternalFrameEvent e)
        {
          /** Set the the status of iFrame in helper **/
          new global().setDisplayed(false);
        }
      });
    }

    @SuppressWarnings("unchecked")
    private void initComponents()
    {
      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 394, Short.MAX_VALUE)
      );

      layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGap(0, 274, Short.MAX_VALUE)
      );
      pack();
    }
  }

Lastly the jDesktop Pane. This holds the button and ActionEvent to determine whether the iFrame is displaying or otherwise. Here if the iFrame is displayed, we restrict double opening and then make sure the iFrame is restored to jDesktop if minimized.

Note: The implementation here is pretty generic - but you could tweak it a little bit down to suite your environment and requirement. Also the 'If Checks' can be reduced down depending on your requirement.
 public class Main extends JFrame
  {
    JDesktopPane desktop;
    public static void main(String[] args)
    {
      SwingUtilities.invokeLater(new Runnable()
      {
        public void run()
        {
          new Main().displayUI();
        }
      });
    }

    public void displayUI()
    {
      desktop = new JDesktopPane();
      JButton button = new JButton("Open");
      button.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent ae)
        {
          try
          {
            iFrame sites = new iFrame();
            if (!sites.isDisplayable())
            {
              if (new global().isDisplayed())
              {
                if (!sites.isMaximum())
                {
                  for (int x = 0; x < desktop.
                  getAllFrames().length; x++)
                  {
                    JInternalFrame frames =(JInternalFrame) desktop.getAllFrames()[x];

                    if (frames.getClass().equals(sites.getClass()))
                    {
                      frames.setIcon(false);
                      frames.toFront();
                      frames.setSelected(true);
                    }
                  }
                }
              }
              else
              {
                sites.setVisible(true);
                desktop.add(sites);
                sites.setSelected(true);
                new global().setDisplayed(true);
              }
            }
          }
          catch (Exception e)
          {
            e.printStackTrace();
          }
        }
      });

      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(400, 400);
      frame.setLocationRelativeTo(null);
      frame.add(desktop, BorderLayout.CENTER);
      frame.add(button, BorderLayout.SOUTH);
      frame.setVisible(true);
    }
  }


Hope this helps!