Minimizing Jinternal Frame without clicking the button
Asked Answered
S

2

1

Is there any way to minimize/maximize the JinternalFrame without clicking the minimize/maximize button at the top-right corner of the jinternalframe?

I followed this thread programmatically minimize a JInternalFrame?, specifically set

jinterframe.setIcon(false)

But i didn't work.

Thanks.

Shopkeeper answered 23/2, 2012 at 14:14 Comment(0)
B
3

works as I expected, you have to check JInternalFrame#isIconifiable() (eeerght this Veto is really ****)

enter image description here

import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyVetoException;
import javax.swing.*;
import javax.swing.plaf.basic.*;

public class InternalFrameUnMovable extends JFrame {

    private static final long serialVersionUID = 1L;
    public JDesktopPane desktop;

    public InternalFrameUnMovable() {
        desktop = new JDesktopPane();
        getContentPane().add(desktop);
        desktop.add(createInternalFrame(1, Color.RED));
        desktop.add(createInternalFrame(2, Color.GREEN));
        desktop.add(createInternalFrame(3, Color.BLUE));
    }

    private JInternalFrame createInternalFrame(int number, Color background) {
        JInternalFrame internal = new JInternalFrame("Frame" + number, true, true, true, true);
        internal.setBackground(background);
        internal.setVisible(true);
        int location = 50 * number;
        internal.setBounds(location, location, 300, 300);
        return internal;
    }

    public static void main(String args[]) throws PropertyVetoException {
        InternalFrameUnMovable frame = new InternalFrameUnMovable();
        frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frame.setSize(600, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        try {// Activate first internal frame
            JInternalFrame[] frames = frame.desktop.getAllFrames();
            frames[0].setSelected(true);
        } catch (java.beans.PropertyVetoException e) {
        }
        JInternalFrame[] frames = frame.desktop.getAllFrames();// Make first internal frame unmovable
        for (int i = 0; i < frames.length; i++) {
            JInternalFrame f = frames[i];
            if (f.isIconifiable()) {
                f.setIcon(true);
            }
        }
        /*JInternalFrame f = frames[0];
        BasicInternalFrameUI ui = (BasicInternalFrameUI) f.getUI();
        Component north = ui.getNorthPane();
        //MouseMotionListener[] actions = (MouseMotionListener[]) north.getListeners(MouseMotionListener.class);
        MouseMotionListener[] actions = north.getListeners(MouseMotionListener.class);
        for (int i = 0; i < actions.length; i++) {
        north.removeMouseMotionListener(actions[i]);
        }*/
    }
}
Bandy answered 23/2, 2012 at 14:41 Comment(3)
+1 Your example implicitly invokes repaint(). Might that explain the OP's result?Blub
@Blub I can't answering your question, I cannot see forest for trees, I'm only tourist in the Desktop API, (in this case) I used code that works out of EDT too, sure by miracle is there repaint() implemented in Desktop :-),Bandy
I posted a variation of your example to work around an anomaly on Mac OS X.Blub
B
2

The method getAllFrames() "Returns iconified frames as well as expanded frames." On Mac OS X, iconified frames may be ignored. The example below demonstrates the anomaly and employs a List<JInternalFrame> to compensate.

Normal iconified

import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

/**
 * @see https://mcmap.net/q/1172518/-minimizing-jinternal-frame-without-clicking-the-button/230513
 * @see https://mcmap.net/q/1172518/-minimizing-jinternal-frame-without-clicking-the-button
 */
public class InternalFrameCount extends JFrame {

    private static final int SIZE = 300;
    private static final String ICONIFY = "Iconify";
    private static final String RESTORE = "Restore";
    private static final String COUNT = "Count:";
    private JDesktopPane desktop = new JDesktopPane();
    private JLabel count = new JLabel(COUNT);
    private List<JInternalFrame> list = new ArrayList<JInternalFrame>();

    public InternalFrameCount() {
        desktop.add(createInternalFrame(1, Color.RED));
        desktop.add(createInternalFrame(2, Color.GREEN));
        desktop.add(createInternalFrame(3, Color.BLUE));
        this.add(desktop);
        JPanel panel = new JPanel();
        panel.add(new JButton(new ButtonAction(ICONIFY)));
        panel.add(new JButton(new ButtonAction(RESTORE)));
        panel.add(count);
        this.add(panel, BorderLayout.SOUTH);
        count.setText(COUNT + desktop.getAllFrames().length);
    }

    private class ButtonAction extends AbstractAction {

        private boolean iconify;

        public ButtonAction(String name) {
            super(name);
            iconify = ICONIFY.equals(name);
        }

        @Override
        public void actionPerformed(ActionEvent ae) {
            for (JInternalFrame jif : list) {
                try {
                    jif.setIcon(iconify);
                } catch (PropertyVetoException e) {
                    e.printStackTrace(System.err);
                }
            }
            count.setText(COUNT + desktop.getAllFrames().length);
        }
    }

    private JInternalFrame createInternalFrame(int number, Color background) {
        JInternalFrame jif = new JInternalFrame(
            "F" + number, true, true, true, true);
        list.add(jif);
        jif.setBackground(background);
        int topLeft = SIZE / 10 * number;
        jif.pack();
        jif.setBounds(topLeft, topLeft, SIZE / 2, SIZE / 2);
        jif.setVisible(true);
        return jif;
    }

    public static void main(String args[]) throws PropertyVetoException {
        System.out.println(System.getProperty("os.name"));
        System.out.println(System.getProperty("java.runtime.version"));
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                InternalFrameCount frame = new InternalFrameCount();
                frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
                frame.pack();
                frame.setSize(SIZE, SIZE);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}
Blub answered 23/2, 2012 at 22:17 Comment(4)
I'm satisfied +1, but did you understood why Veto is there?Bandy
driving me crazy here or with How to Write a Tree Expansion ListenerBandy
@mKorbel: Sorry, no. I've never seen a listener veto the change, but I guess it might be a useful feature.Blub
@mKorbel: The article's use of the runtime-type tokens.Blub

© 2022 - 2024 — McMap. All rights reserved.