shabgard tanha
shabgard tanha

Reputation: 1

How can I know an undecorated JFrame has been fully painted on the screen?

I have a simple code like this:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test {
    public static void main(String[] args) {
        Rectangle maximizeRectangle=GraphicsEnvironment.getLocalGraphicsEnvironment().
        getMaximumWindowBounds();
        JFrame frame1 = new JFrame();
        JFrame frame2 = new JFrame();

        //frame1
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.setUndecorated(true);
        frame1.setBounds(maximizeRectangle);
        JPanel cp1 = new JPanel();
        cp1.setBackground(new Color(200, 200, 200));
        JButton but1 = new JButton("I'm frame 1");
        cp1.add(but1);
        ActionListener ac1 = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame2.setVisible(true);
                //here is my question:
                //how can I know when frame2 has been fully painted? and then I can 
                //set visibility of frame1 to false to avoid flickering
                frame1.setVisible(false);
            }
        };
        but1.addActionListener(ac1);
        frame1.setContentPane(cp1);

        //frame2
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame2.setUndecorated(true);
        frame2.setBounds(maximizeRectangle);
        JPanel cp2 = new JPanel();
        cp2.setBackground(new Color(200, 200, 200));
        JButton but2 = new JButton("I'm frame 2");
        cp2.add(but2);
        ActionListener ac2 = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame1.setVisible(true);
                //here is my question:
                //how can I know when frame1 has been fully painted? and then I can 
                //set visibility of frame2 to false to avoid flickering
                frame2.setVisible(false);
            }
        };
        but2.addActionListener(ac2);
        frame2.setContentPane(cp2);

        frame1.setVisible(true);
    }
}

when I press buttons, I can see flickers(desktop background). how can I know a JFrame has been fully painted on the screen before I set visibility of another JFrame to false?

when I press buttons, I can see flickers(desktop background). how can I know a JFrame has been fully painted on the screen before I set visibility of another JFrame to false?

I can't use timer solution!

Many thanks

Upvotes: 0

Views: 62

Answers (2)

shabgard tanha
shabgard tanha

Reputation: 1

@Medvynskyy: many thanks for your reply; but I think I couldn't say my real intention. in fact, I want to maximize a JFrame according to this code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

public class Test {
    public static void main(String[] args) {
        Rectangle maximizeRectangle=GraphicsEnvironment.getLocalGraphicsEnvironment().
        getMaximumWindowBounds();
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setUndecorated(true);
        frame.getContentPane().setLayout(new FlowLayout());
        frame.setSize(500, 500);
        frame.setLocationRelativeTo(null);
        frame.getRootPane().setBorder(new LineBorder(Color.BLACK, 10));

        JButton but1 = new JButton("Maximize");
        ActionListener ac1 = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.setBounds(maximizeRectangle);
            }
        };
        but1.addActionListener(ac1);

        JButton but2 = new JButton("Minimize");
        ActionListener ac2 = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.setSize(500, 500);
                frame.setLocationRelativeTo(null);
            }
        };
        but2.addActionListener(ac2);

        frame.add(but1);
        frame.add(but2);
        frame.setVisible(true);
    }
}

but when I press the Maximize button, I can see a flicker at the top-left corner. of course I know why that happens: because at first JFrame at the size (500, 500) is transformed to the location (0, 0) and then is expanded to full screen. how can I see only full screen JFrame when I press Maximize? my previous question was an idea for this problem.

Upvotes: 0

Sergiy Medvynskyy
Sergiy Medvynskyy

Reputation: 11327

I've rewrite youre example without second frame and corresponding flickering. As I already mentioned in my comment, you should use CardLayout instead of second frame.

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Test {
    private static final String FRAME1 = "frame1";

    private static final String FRAME2 = "frame2";

    public static void main(String[] args) {
        SwingUtilities.invokeLater(Test::startUp);
    }

    private static void startUp() {
        Rectangle maximizeRectangle = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
        JFrame frame1 = new JFrame();
        CardLayout cl = new CardLayout();
        frame1.setLayout(cl);

        // frame1
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.setUndecorated(true);
        frame1.setBounds(maximizeRectangle);
        JPanel cp1 = new JPanel();
        cp1.setBackground(new Color(200, 200, 200));
        JButton but1 = new JButton("I'm frame 1");
        cp1.add(but1);
        ActionListener ac1 = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cl.show(frame1.getContentPane(), FRAME2);
            }
        };
        but1.addActionListener(ac1);
        frame1.add(cp1, FRAME1);

        // frame2
        JPanel cp2 = new JPanel();
        cp2.setBackground(new Color(200, 200, 200));
        JButton but2 = new JButton("I'm frame 2");
        cp2.add(but2);
        ActionListener ac2 = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cl.show(frame1.getContentPane(), FRAME1);
            }
        };
        but2.addActionListener(ac2);
        frame1.add(cp2, FRAME2);

        frame1.setVisible(true);
    }
}

Upvotes: 2

Related Questions