Search code examples
javaswingminimizejwindow

How can I minimize/iconify a JWindow in Java?


My app has a JWindow that needs to be minimized when the custom minimizer button clicked. Please reply if anyone knows how to minimize a JWindow. I have searched a lot but couldn't find any suitable method to minimize. I know how to minimize a JFrame. So please don't bother answering regarding JFrame. Thanks.


Solution

  • Due to the fact that a JWindow is not decorated with any control icons, no setState method is provided. One workaround is to allow your custom minimizer button to set the window visible as required:

    public class JWindowTest extends JFrame {
    
        JWindow window = new JWindow();
        JButton maxMinButton = new JButton("Minimize Window");
    
        public JWindowTest() {
            setSize(300, 200);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            maxMinButton.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent e) {
                    if (window.isVisible()) {
                        maxMinButton.setText("Restore Window");
                    } else {
                        maxMinButton.setText("Minimize Window");
                    }
                    window.setVisible(!window.isVisible());
                }
            });
    
            add(maxMinButton);
    
            window.setBounds(30, 30, 300, 220);
            window.setLocationRelativeTo(this);
            window.add(new JLabel("Test JWindow", JLabel.CENTER));
            window.setVisible(true);
        }
    
        public static void main(String[] args) {
            new JWindowTest().setVisible(true);
        }
    }