T3086690
T3086690

Reputation: 1

How to fix annoying white splash in this code?

The problem is when I run the code below, I see a white splash on my screen for about maybe 100ms which is extremely annoying:

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

public class MyPanel extends JPanel {

    public MyPanel() {
        this.setBackground(Color.black);
        JTextArea jtext = new JTextArea(10, 20);
        jtext.setBackground(Color.black);
        jtext.setForeground(Color.white);
        jtext.setText("some stuff here");
        add(jtext);
        add(new JButton("some button"));
        // TODO: gui elements
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("MyPanel");
                frame.getContentPane().add(new MyPanel());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
                frame.setVisible(true);
            }
        });
    }
}

My entire work setup is in dark theme. Equivilant code written in C++ with Qt does not show any white splashs on the same system. I'm using Ubuntu 12.04 and Intel HD Graphics 3000, java version "1.7.0_45". Please let me know how this can be fixed. I googled this problem but all I found was: "how to create splash screen in java?" which what I'm trying to avoid.

update: solution: I'm not sure why this works: I extended MyPanel to JFrame and put most of the functions there. then in the run() invoked it and set visible to true. here is the code:

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

public class MyPanel extends JFrame {

    public MyPanel() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBackground(Color.black);
        JTextArea jtext = new JTextArea(10, 20);
        jtext.setBackground(Color.black);
        jtext.setForeground(Color.white);
        jtext.setText("some stuff here");
        getContentPane().add(jtext);

        // TODO: gui elements

        setExtendedState(JFrame.MAXIMIZED_BOTH);
        setSize(350, 250);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                MyPanel mypanel = new MyPanel();
                mypanel.setVisible(true);
            }
        });
    }
}

Upvotes: 0

Views: 133

Answers (2)

trashgod
trashgod

Reputation: 205805

Although it looks normal to me on Ubuntu 12.04 / Intel Iris Pro 5200 / Java 6, I can see the effect by running in a slowed emulator. Setting the frame's background to black before setVisible() seems to help.

frame.setBackground(Color.black);

Upvotes: 2

Andrew Thompson
Andrew Thompson

Reputation: 168825

I did not see the 'white flash' here, but try this altered code that calls pack() on the frame.

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

public class MyPanel extends JPanel {

    public MyPanel() {
        this.setBackground(Color.black);
        JTextArea jtext = new JTextArea(10, 20);
        jtext.setBackground(Color.black);
        jtext.setForeground(Color.white);
        jtext.setText("some stuff here");
        add(jtext);
        add(new JButton("some button"));
        // TODO: gui elements
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame("MyPanel");
                frame.getContentPane().add(new MyPanel());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

                frame.pack();

                frame.setVisible(true);
            }
        });
    }
}

Upvotes: 2

Related Questions