Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to minimize a JFrame by clicking on a JButton [duplicate]

Tags:

java

swing

jframe

Possible Duplicate:
JButton minimizing a window(JFrame)

I removed the JFrame's close/minimize buttons, and I want to add my own buttons, for close, it can work by using .dispose();, but what should I use to minimize the JFrame if I click on a JButton?

I think .setVisible(false); will hide it completely, and I won't have anything to click on, in the TaskBar to get the JFrame back.

like image 619
user1665700 Avatar asked Nov 23 '25 03:11

user1665700


1 Answers

Just use .setState(Frame.ICONIFIED)

Working Example:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class FrameTest {
    public static void main(String args[]) throws Exception {
        final JFrame frame = new JFrame();
        frame.setUndecorated(true);
        JButton button = new JButton("Minimize");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.setState(Frame.ICONIFIED);
            }
        });
        frame.add(button);
        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
}
like image 139
cubanacan Avatar answered Nov 24 '25 19:11

cubanacan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!