1

I know this question was asked before, but there was no answer to it.

In Java, when I am adding components to the frame, all the elements after adding a JTextField are not rendered when the application is initialized. They are rendered after you refresh the screen e.g. minimize and maximize the screen. In the following only the textfield is rendered. It looks like some Java rendering issue.

My code is as follows:

private void initialize() {
    frame = new JFrame();
    frame.setVisible(true);
    frame.setBounds(100, 100, 569, 321);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    txtGenerationRate = new JTextField();
    txtGenerationRate.setBounds(322, 29, 86, 20);
    frame.getContentPane().add(txtGenerationRate);
    txtGenerationRate.setColumns(10);

    lblAmountOfSolarPanelsText = new JLabel("Amount of solar panels:");
    lblAmountOfSolarPanelsText.setBounds(10, 57, 159, 14);
    frame.getContentPane().add(lblAmountOfSolarPanelsText);

    frame.setVisible(true); // added it for the second time, just to make sure
}

Can anyone help, please?

Piotr

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Piotr Nowak
  • 85
  • 2
  • 9
  • I can confirm the lagging. The first render costs a while or does not render the child component. You can find some useful information from http://stackoverflow.com/questions/6978017/first-call-to-jframe-constructor-takes-a-long-time-during-swing-application-star – Thai Tran Jan 31 '13 at 16:27

4 Answers4

2

At first, you should definitely read about how to use layouts: http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html

The second thing, you should set your frame visible after all components are added, if you do not, you have to call repaint().

Stephan
  • 4,395
  • 3
  • 26
  • 49
1

Try putting this at the bottom of the initialize method

frame.setVisible(true);
frame.setBounds(100, 100, 569, 321);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Piotr Nowak
  • 85
  • 2
  • 9
0

you could also try frame.revalidate(); and frame.repaint();

redc0w
  • 101
  • 4
0

This seems working for me, I see both label and text field when the window first appears. The label text does not fit into label so is truncated, but apart that everything looks as expected. Maybe the wrong fragment has been removed from the code while trying to simplify it?

enter image description here

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93