Dragging JFrame to the top of the screen to maximize it automatically and dragging downward to restore it to NORMAL size

I would like to ask for help regarding code below.

Objectives:

I should be able to enlarge the JFrame to full screen whenever I’m dragging it to the top of the screen. Similarly if the JFrame is in full screen mode, whenever I drag it down it should return to the normal mode depends on the minimum size parameters.

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

public class DraggableFrame extends JFrame {
    private int posX = 0, posY = 0;
    private boolean isDraggingDown = false;

    public DraggableFrame() {
        // Initialize the JFrame
        setUndecorated(true);
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // Add mouse listener for dragging
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                posX = e.getX();
                posY = e.getY();
                isDraggingDown = false;
            }
        });

        addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                int x = e.getXOnScreen() - posX;
                int y = e.getYOnScreen() - posY;

                // Handle dragging while in full screen
                if (getExtendedState() == JFrame.MAXIMIZED_BOTH) {
                    if (y > 0 && !isDraggingDown) {
                        isDraggingDown = true;
                        setExtendedState(JFrame.NORMAL);
                        setSize(800, 600); // Restore to initial size
                        setLocation(x, y);
                    } else if (y <= 0 && !isDraggingDown) {
                        // If the frame is dragged to the top while maximized, do nothing
                        return;
                    }
                } else {
                    setLocation(x, y);

                    // Check if dragged to the top
                    if (getY() <= 0) {
                        // Enlarge window to fullscreen without covering taskbar
                        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
                        GraphicsDevice device = env.getDefaultScreenDevice();

                        Rectangle screenBounds = device.getDefaultConfiguration().getBounds();
                        Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(device.getDefaultConfiguration());

                        int taskbarHeight = screenInsets.bottom;

                        setBounds(screenBounds.x, screenBounds.y, screenBounds.width, screenBounds.height - taskbarHeight);
                    }
                }
            }
        });

        // Restore to normal size if dragged down while in fullscreen
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                if (getY() == 0 && getExtendedState() == JFrame.MAXIMIZED_BOTH) {
                    setExtendedState(JFrame.NORMAL);
                    setSize(800, 600); // Restore to initial size
                    setLocationRelativeTo(null);
                }
            }
        });
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            DraggableFrame frame = new DraggableFrame();
            frame.setVisible(true);
        });
    }
}

I tried to apply some code that I found all over the internet but it seems none of them worked as I expected.

What I want is the following:

I should be able to enlarge the JFrame to full screen whenever I’m dragging it to the top of the screen. Similarly if the JFrame is in full screen mode, whenever I drag it down it should return to the normal mode depends on the minimum size parameters.

1

The problem is this condition:

            // Handle dragging while in full screen
            if (getExtendedState() == JFrame.MAXIMIZED_BOTH) {

You never set the frames extended state to JFrame.MAXIMIZED_BOTH, that condition is therefore never true and you never reach to code that would switch the frame back to normal.

Since setting the frame to JFrame.MAXIMIZED_BOTH obscures the task bar (which it seems you want to avoid) you need another indication when the frame is maximized.

You could do this by adding a bolean field maximized to your DraggableFrame and adjust it when necessary:

public class DraggableFrame extends JFrame {
    private boolean maximized = false;

    // ...
        addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                int x = e.getXOnScreen() - posX;
                int y = e.getYOnScreen() - posY;

                // Handle dragging while in full screen
                if (maximized) {
                    if (y > 0 && !isDraggingDown) {
                        isDraggingDown = true;
                        setExtendedState(JFrame.NORMAL);
                        setSize(800, 600); // Restore to initial size
                        setLocation(x, y);
                        maximized = false;
                    } else if (y <= 0 && !isDraggingDown) {
                        // If the frame is dragged to the top while maximized, do nothing
                        return;
                    }
                } else {
                    setLocation(x, y);

                    // Check if dragged to the top
                    if (getY() <= 0) {
                        // Enlarge window to fullscreen without covering taskbar
                        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
                        GraphicsDevice device = env.getDefaultScreenDevice();

                        Rectangle screenBounds = device.getDefaultConfiguration().getBounds();
                        Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(device.getDefaultConfiguration());

                        int taskbarHeight = screenInsets.bottom;

                        setBounds(screenBounds.x, screenBounds.y, screenBounds.width, screenBounds.height - taskbarHeight);
                        maximized = true;
                    }
                }
            }
        });

        // Restore to normal size if dragged down while in fullscreen
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                if (getY() == 0 && maximized) {
                    setExtendedState(JFrame.NORMAL);
                    setSize(800, 600); // Restore to initial size
                    setLocationRelativeTo(null);
                    maximized = false;
                }
            }
        });
    }
    // ...
}

Even better would be to either not use undecorated frames (since these work together with the Window Manager of your OS) or (if you need undecorated frames because you want to make a full-screen game) make the window full screen from the beginning.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật