Results 1 to 2 of 2
  1. #1
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Iconified JInternalFrame in JToolbar

    Hi, I created JFrame, JInternalFrame, JDesktopPane and JToolbar.
    What I am trying to do here is to show iconnified(minimized) JInternalFrame to toolbar and
    when user restore the JInternalFrame I want it back to JDesktopPane. I created a small program that describes what I am trying to do.
    I searched at google but maybe I am entering wrong keyword.
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Toolkit;
    import java.beans.PropertyVetoException;
    import javax.swing.JDesktopPane;
    import javax.swing.JFrame;
    import javax.swing.JInternalFrame;
    import javax.swing.JToolBar;
    import javax.swing.SwingUtilities;
    
    public class FrmMain extends JFrame
    {
        private JDesktopPane myDesktopPane;
        private JInternalFrame myInternalFrame;
        private JToolBar myToolBar;
        public FrmMain()
        {
            int screenW = Toolkit.getDefaultToolkit().getScreenSize().width - 200;
            int screenH = Toolkit.getDefaultToolkit().getScreenSize().height - 100;
    
            setLayout(new BorderLayout());
            setPreferredSize(new Dimension(screenW, screenH));
    
            myDesktopPane = new JDesktopPane();
            add(myDesktopPane, BorderLayout.CENTER);
    
            myToolBar = new JToolBar(JToolBar.HORIZONTAL);
            add(myToolBar, BorderLayout.AFTER_LAST_LINE);
            myToolBar.setPreferredSize(new Dimension(100, 35));
            myToolBar.setVisible(true);
    
            myInternalFrame = new JInternalFrame();
            myDesktopPane.add(myInternalFrame);
            myInternalFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            myInternalFrame.setTitle(".:: INTERNAL FRAME ::.");
            myInternalFrame.setIconifiable(true);
            myInternalFrame.setClosable(true);
            myInternalFrame.setResizable(true);
    
            try
            {
                myInternalFrame.setMaximum(true);
            }
            catch(PropertyVetoException pvEx)
            {
                pvEx.printStackTrace();
            }
            myInternalFrame.setVisible(true);
    
            setVisible(true);
    
            pack();
        }
    
        public static void main(String args[])
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    FrmMain myMain = new FrmMain();
                    myMain.setDefaultCloseOperation(EXIT_ON_CLOSE);
                }
            });
        }
    }
    Thanks in advance.

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    I was able to solve the problem by adding JButton to JToolbar. I was thinking if there is already a Component that do this and it just that I am not aware of it.

    Here is the code:
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.beans.PropertyVetoException;
    import javax.swing.JButton;
    import javax.swing.JDesktopPane;
    import javax.swing.JFrame;
    import javax.swing.JInternalFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JToolBar;
    import javax.swing.SwingUtilities;
    
    public class FrmMain extends JFrame
    {
        private JDesktopPane myDesktopPane;
        private JInternalFrame myInternalFrame;
        private JToolBar myToolBar;
        private JToolbarButton myToolbarButton;
        
        public FrmMain()
        {
            setLayout(new BorderLayout());
            setTitle(".:: MAIN FRAME ::.");
    
            //JDesktopPane
            myDesktopPane = new JDesktopPane();
            add(myDesktopPane, BorderLayout.CENTER);
    
            //JToolBar
            myToolBar = new JToolBar(JToolBar.HORIZONTAL);
            add(myToolBar, BorderLayout.AFTER_LAST_LINE);
            myToolBar.setPreferredSize(new Dimension(100, 35));
            myToolBar.setVisible(true);
    
            //JInternalFrame
            myInternalFrame = new JInternalFrame();
            myDesktopPane.add(myInternalFrame);
            myInternalFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
            myInternalFrame.setTitle(".:: INTERNAL FRAME ::.");
            myInternalFrame.setIconifiable(true);
            myInternalFrame.setClosable(true);
            myInternalFrame.setResizable(true);
            try
            {
                myInternalFrame.setMaximum(true);
            }
            catch(PropertyVetoException pvEx)
            {
                pvEx.printStackTrace();
                JOptionPane.showMessageDialog(null
                        , "Could not maximize/minimize " + myInternalFrame. getTitle()
                        , "ERROR:", JOptionPane.ERROR_MESSAGE);
            }
            myInternalFrame.setVisible(true);
    
            //JToolbarButton
            myToolbarButton = new JToolbarButton(myInternalFrame);
            myToolbarButton.setPreferredSize(new Dimension(80, 25));
            myToolBar.add(myToolbarButton);
    
            setVisible(true);
    
            pack();
        }
    
        public static void main(String args[])
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    FrmMain myMain = new FrmMain();
                    myMain.setDefaultCloseOperation(EXIT_ON_CLOSE);
                    myMain.setExtendedState(JFrame.MAXIMIZED_BOTH);
                }
            });
        }
    
        protected class JToolbarButton extends JButton implements ActionListener
        {
            JInternalFrame anInternalFrame;
    
            public JToolbarButton(JInternalFrame anInternalFrame)
            {
                //Copy the JInternalFrame's Title
                setText(anInternalFrame.getTitle());
                this.anInternalFrame = anInternalFrame;
                addActionListener(this);
            }
    
            /*
             * returns true if JInternalFrame is minimized,
             * false if not minimized
             */
            public boolean getState()
            {
                return anInternalFrame.isIcon();
            }
    
            public void actionPerformed(ActionEvent ae)
            {
                try
                {
                    /*
                     * If anInternalFrame.isIcon() == true,
                     * minimize and hide anInternalFrame
                     * If anInternalFrame.isIcon() == false,
                     * maximize and show anInternalFrame
                     */
                    anInternalFrame.setIcon(!anInternalFrame.isIcon());
                    anInternalFrame.setVisible(!anInternalFrame.isIcon());
                }
                catch(PropertyVetoException pvEx)
                {
                    pvEx.printStackTrace();
                    JOptionPane.showMessageDialog(null 
                            , "Could not maximize/minimize " + getText()
                            , "ERROR:", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }

Similar Threads

  1. JToolBar make it non-draggable
    By nathic in forum New To Java
    Replies: 1
    Last Post: 03-22-2011, 08:52 AM
  2. FrameView iconified at startup
    By fabr_00 in forum AWT / Swing
    Replies: 2
    Last Post: 01-09-2011, 04:18 AM
  3. ActionListener for JToolBar components
    By LianaN in forum New To Java
    Replies: 6
    Last Post: 09-18-2010, 01:37 PM
  4. JToolBar movable but not floatable
    By Fosters in forum AWT / Swing
    Replies: 0
    Last Post: 08-22-2008, 03:04 PM
  5. Open a shell minimized (iconified)
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 02:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •