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

1

4 Answers 4

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

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.