Icewine
Icewine

Reputation: 1851

Start of Simple Java program causes a white screen pause

I have a simple text program and am loading the jframe with 2 JPanels(body and footer) a text area for the body and lable for the footer. I am not sure if my ordering is wrong or i forgot something but i get a lag when starting the program that causes it to blink white. This is kind of annoying and i would like to avoid this if possible. Any thoughts?


This is what it should look like but at the start it blinks white and hands for half a second. I tried setting the frame background to blue too but that didnt change anything.

hangs hereshould look like this...

    JFrame frame = new JFrame("SIMPLE TEXT");
    JPanel panel = new JPanel();
    JTextArea textarea = new JTextArea();
    JPanel footer = new JPanel();       
    JLabel linecol = new JLabel("line: 0 col: 0");

    frame.setSize(640,480);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.setResizable(false);

    panel.setSize(frame.getBounds().width, frame.getBounds().height);
    panel.setBackground(bgColor);
    panel.setForeground(fgColor);
    panel.setLayout(new BorderLayout());

    footer.setSize(640,20);
    footer.setBackground(bgColor);
    footer.setLayout(new BorderLayout());
    footer.setBorder(new EmptyBorder(0,0,0,10));

    textarea.setSize(400, 400);
    textarea.setOpaque(false);
    textarea.setForeground(fgColor);
    textarea.setCaretColor(fgColor);


    linecol.setForeground(fgColor); 


    frame.add(panel);
    panel.add(footer, BorderLayout.PAGE_END);
    panel.add(textarea);
    footer.add(linecol, BorderLayout.LINE_END);

Upvotes: 2

Views: 2512

Answers (2)

nintendo
nintendo

Reputation: 140

Even when calling setVisible(true) at the end of your component setup, you may still experience an initial white blink. Because your content maybe have some EDT thread operations with different duration and timings

A more safe approach is to use SwingUtilities.invokeLater() to ensure that setVisible(true) is executed after all GUI operations have completed:

// Initialize and set up your frame and components
JFrame frame = new JFrame("Your Frame Title");
// Configuration and component adding code here

// Delay setting the frame visible to avoid initial white blink
javax.swing.SwingUtilities.invokeLater(() -> {
    frame.setVisible(true);
});

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347314

Take this frame.setVisible(true); and move it to the list command you call after the screen has been built

JFrame frame = new JFrame("SIMPLE TEXT");
JPanel panel = new JPanel();
JTextArea textarea = new JTextArea();
JPanel footer = new JPanel();       
JLabel linecol = new JLabel("line: 0 col: 0");

//frame.setSize(640,480);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//frame.setLocationRelativeTo(null);
//frame.setVisible(true);
//frame.setResizable(false);

//...

frame.add(panel);
panel.add(footer, BorderLayout.PAGE_END);
panel.add(textarea);
footer.add(linecol, BorderLayout.LINE_END);

frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

You will also find that using setSize to try and size your components is pointless (and highly discouraged) as the frame and panels are under the control of a layout manager...

Also beware, setResizable can change the size of the frame's borders, this needs to be called before you try and size and/or position the window.

Updated

As suggested by Andrew Thompson, you should be using JTextArea(int, int) to provide sizing hints to the layout manager. You may also find the JTextArea more useful if you were to place it within a JScrollPane. See How to Use Scroll Panes for more details

Upvotes: 2

Related Questions