Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java items appear only after the window is resize

Tags:

I have 2 JPanels in a frame. The first panel contains java items like buttons etc. The two buttons I added appears but the JSpinner appear just after I resize the window. I guess this will happen also with other items I will add. How could I solve this problem?

import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.util.ArrayList; import java.util.List;  import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.SpinnerListModel; import javax.swing.SpinnerNumberModel;  public class StartingPoint {  static JFrame window; static DrawingArea draw; static JButton b1, b2; static JPanel userInt; static JSpinner gravitySpinner;  public static void main(String[] args) {     window = new JFrame("Ball");     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     window.setSize(600, 400);     window.setLayout(new BorderLayout());     window.setVisible(true);      draw = new DrawingArea();     window.add(draw, BorderLayout.CENTER);      userInt = new JPanel();     window.add(userInt, BorderLayout.NORTH);      b1 = new JButton("Start");     b2 = new JButton("aaa");     b1.addActionListener(new ActionListener() {         @Override         public void actionPerformed(ActionEvent e) {             draw.setUp();         }     });     userInt.add(b1);     userInt.add(b2);       SpinnerNumberModel gravityModel = new SpinnerNumberModel(.9, .1, 2, .1);     gravitySpinner = new JSpinner(gravityModel);     userInt.add(gravitySpinner); } } 
like image 938
Alex Avatar asked Jan 07 '12 15:01

Alex


People also ask

How do I fix frame size in Java?

You can change the size of the JFrame by simply placing the cursor in the corners and dragging it. Or if you press the resize option next to close(X) in the upper right corner, it will be enlarged to full-screen size. This happens because the resize is set to “true” by default.

How do I stop JFrame from resizing?

Making a Frame Non-Resizable: use setResizable(false) to freeze a frame's size. : JFrame Window « Swing « Java Tutorial. 14.80. 1. 14.80.

What is resizable in Java?

Resizable components are often used when creating charts or diagrams. A common resizable component is a chart in a spreadsheet application. The chart can be moved over a table widget of the application and resized.


2 Answers

You're adding components to the GUI after calling setVisible(true) on the JFrame and that's backwards since you're rendering the GUI before anything has been added, and so it makes sense that things added later won't be displayed until all is repainted.

Instead, first add all your components, and only then render the GUI by calling setVisible(true) on the JFrame.

Edit
Also, you'll want to avoid calling setSize(...) on anything but instead let components size themselves using their preferredSizes and by calling pack() on the JFrame prior to displaying it with setVisible(true).

Edit 2
For example:

import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;  import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JSpinner; import javax.swing.SpinnerNumberModel; import javax.swing.SwingUtilities;  public class StartingPoint {     private DrawingArea draw;    private JButton b1, b2;    private JPanel userInt;    private JSpinner gravitySpinner;     private JPanel mainPanel = new JPanel();     public StartingPoint() {       mainPanel.setLayout(new BorderLayout());       draw = new DrawingArea();       mainPanel.add(draw, BorderLayout.CENTER);        userInt = new JPanel();       mainPanel.add(userInt, BorderLayout.NORTH);        b1 = new JButton("Start");       b2 = new JButton("aaa");       b1.addActionListener(new ActionListener() {          @Override          public void actionPerformed(ActionEvent e) {             draw.setUp();          }       });       userInt.add(b1);       userInt.add(b2);        SpinnerNumberModel gravityModel = new SpinnerNumberModel(.9, .1, 2, .1);       gravitySpinner = new JSpinner(gravityModel);       userInt.add(gravitySpinner);    }     public JPanel getMainPanel() {       return mainPanel;    }     public static void main(String[] args) {       SwingUtilities.invokeLater(new Runnable() {          public void run() {             JFrame window = new JFrame("Ball");             window.add(new StartingPoint().getMainPanel());             window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);              window.pack();             window.setLocationRelativeTo(null);             window.setVisible(true);          }       });    } }  class DrawingArea extends JPanel {     private static final int PREF_W = 600;    private static final int PREF_H = 400;     public void setUp() {       // TODO finish    }     @Override    public Dimension getPreferredSize() {       return new Dimension(PREF_W, PREF_H);    }  } 
like image 82
Hovercraft Full Of Eels Avatar answered Sep 23 '22 13:09

Hovercraft Full Of Eels


Add setVisible(true) at the end and it works.

like image 35
Thilina Anuradh Avatar answered Sep 22 '22 13:09

Thilina Anuradh