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 7984949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:25:59+00:00 2026-06-04T11:25:59+00:00

When is a GUI component’s position, width and height set? If layout managers are

  • 0

When is a GUI component’s position, width and height set? If layout managers are being used, how are these fields effected? I have a panel that contains two sliders and I want to draw a rectangle next to one of them. Here is my code to initialize my GUI components and paint them with a rectangle next to one of the sliders:

public PhotoSliders() 
{
    initComponents();    //from the netbeans GUI designer
}

public void setColorRect()   //initialize a colored rectangle
{
    colorRect = new ColorRect(Color.RED, (double)(emSlider.getX())/getWidth(), (double)(emSlider.getY())/getHeight());
}

public void paintComponent(Graphics g)
{
    super.paintComponent(g);

    colorRect.paintColorRect(g, getWidth(), getHeight());
}

I have found that the emSlider.getX(), emSlider.getY(), getWidth(), and getHeight() are all 0 initially. When I resize my applet and thus causing it to be repainted, the fields are no longer zero. Why are they all zero initially? Here is the code in my applet class:

public void init()                               //initialize the Applet Class
{
   setLayout(new BorderLayout());
   slidersPanel = new PhotoSliders();
   JPanel north = new JPanel(new BorderLayout());

   north.add(slidersPanel, BorderLayout.WEST);
   add(north, BorderLayout.NORTH);
   add(photoEffect, BorderLayout.CENTER);      

   slidersPanel.setColorRect();

}

   public void paint(Graphics g)                   //paint the photoEffect
   {     
      super.paint(g);
      slidersPanel.setColorRect();
   }
  • 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-06-04T11:26:00+00:00Added an answer on June 4, 2026 at 11:26 am

    A component’s location and size are set when the GUI is rendered, and before that they are the default value, 0. For a desktop applicaiton, this occurs when either pack() is called on the top level window, or when setVisible(true) is called on the top level window, or on the component itself if it is added to the GUI after all has been rendered.

    If it’s an applet, then it’s when the applet is rendered — something that is done behind the scenes. You may be able to listen for this using one of the Swing listeners. I have to look up which one, possibly an AncestorListener…. edit, yes I think that’s right, try using an AncestorListener.

    Consider buying Filthy Rich Clients for a wonderful book that goes into great depths on Swing graphics.

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

Sidebar

Related Questions

I have created for my application, some custom GUI component and used them. Now
I have a GUI tool that manages state sequences. One component is a class
I have a subclass of JLabel that forms a component of my GUI. I
I have done an layout in my GUI but it did not work ,
I have a JTable component in my GUI which displays psuedocode of an algorithm.
Suppose you have a client/server application, say a webserver component and a qt gui.
Let's say you have a gui component and 10 threads all tell it to
I have a simple GUI component written in Java. The class draws an analog
What java GUI layout manager does everyone use? Lately, I have been using MigLayout
I'm building a gui component that has a tree-based data model (e.g. folder structure

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.