Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7028541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:24:05+00:00 2026-05-28T00:24:05+00:00

I have 2 JPanels in a frame. The first panel contains java items like

  • 0

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);
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:24:06+00:00Added an answer on May 28, 2026 at 12:24 am

    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);
       }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a frame with 4 JPanels and 1 JScrollPane, the 4 panels are
I have a JPanel that encapsulates two JPanels, one on top of the other.
I have a JPanel subclass on which I add buttons, labels, tables, etc. To
I have to create a CD inventory program for my first Java class. The
I have the following class which implements 3 JPanels. 1 Panel has a label,
I have three JPanels inside a main Frame. Clockwise from the left, in the
I've been programming in Java for a little while now but have never really
I have sever JPanels which have to be ordered vertically. For that I want
I have a JPanel to which I'd like to add JPEG and PNG images
I have a JPanel, which I would like to detect the following events (1)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.