2

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.

Community
  • 1
  • 1
user1665700
  • 512
  • 2
  • 13
  • 28

2 Answers2

8

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);
    }
}
cubanacan
  • 644
  • 1
  • 9
  • 26
1

Try this:

frame.setExtendedState(frame.getExtendedState | Frame.ICONIFIED);

or

frame.setExtendedState(frame.getExtendedState | ~Frame.MAXIMIZED_BOTH);
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111