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.
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);
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