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

  • Home
  • SEARCH
  • 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 7189357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:17:06+00:00 2026-05-28T19:17:06+00:00

Can somebody please help me understand why my custom JComponent ‘Bar’, only displays when

  • 0

Can somebody please help me understand why my custom JComponent ‘Bar’, only displays when added directly to the JFrame, and not when added to a JPanel (which is then added to the JFrame)?

Thanks!

package main;

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class Board {

    public void start(){
        JFrame frame = new JFrame();
        JButton button1 = new JButton("Button 1");
        Bar statusbar = new Bar();
        JLabel status = new JLabel("Status: ");

        JPanel topPanel = new JPanel();
        topPanel.add(status);
        topPanel.add(statusbar);

        JPanel mainPanel = new JPanel();
        mainPanel.add(button1);
        mainPanel.add(statusbar);

        frame.getContentPane().add(BorderLayout.NORTH, topPanel);
        frame.getContentPane().add(BorderLayout.SOUTH, mainPanel);
        frame.getContentPane().add(BorderLayout.CENTER, statusbar);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(200,100);
        frame.setVisible(true);
    }
}

Here is my Bar Class…

package main;

import java.awt.Graphics;

import javax.swing.JComponent;

public class Bar extends JComponent{
    public void paint(Graphics g){
        g.fillRect(0, 0, 100, 10);
    }
}
  • 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-28T19:17:07+00:00Added an answer on May 28, 2026 at 7:17 pm

    You’re adding statusbar to several different places in the component tree, Swing doesn’t deal with that well (or at all).

    Create a separate Bar instances each time you use it, if you want their display to be synchronized, they should share the same model.

    Edit

    Ah, on a second glance, the problem here is that you never set a size (or preferred size) for the Bar components, so they get squished to 0 by the layout manager.

    Try:

    public static class Bar extends JComponent {
    
        private Bar() {
            setPreferredSize(new Dimension(25, 5));
        }
    
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.fillRect(0, 0, 100, 10);
        }
    }
    

    You should also add a frame.pack() before display.

    (the multiple references to the same component thing is still true, too)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can somebody please help me with this mail script. I'm simply trying to send
Can somebody help me understand how this pointed corner was actually generated in css
Can somebody please help me fix my code.? I can't see where I'm going
I am trying to understand the difference/disadvantages of strcpy and strncpy. Can somebody please
I'm trying to optimize this mysql query using EXPLAIN. Can somebody please help me
Could somebody please help me understand the concept of 'impersonation'? The way I understand
Can somebody please help me to port the following code to GCC ? I
Can somebody please help me out on how to increase the size of the
can somebody please help me how to make a war of a simple mule
Can somebody please help me to write a DeepCopy routine for this matrix class

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.