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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:42:56+00:00 2026-05-25T00:42:56+00:00

I need to create a custom LayoutManager to be used by a JPanel. However,

  • 0

I need to create a custom LayoutManager to be used by a JPanel.

However, when I add a Component to the JPanel, the JPanel doesn’t call the addLayoutComponent() method of my custom LayoutManager, even though it should:
http://download.oracle.com/javase/tutorial/uiswing/layout/custom.html

(It does call layoutContainer(), as expected)

Hopefully, someone can tell me what I am doing wrong.
How do I get the JPanel to call addLayoutComponent()?

import java.awt.*;
import javax.swing.*;

public class Test
{
    public static void main(String[] args)
    {
        createAndShowGUI();
    }



    private static void createAndShowGUI()
    {
        JButton button = new JButton("Test");
        button.setBounds(64, 64, 128, 64);

        JPanel panel = new JPanel(new CustomLayoutManager());

        //FIXME: Missing call to CustomLayoutManager.addLayoutComponent()
        panel.add(button); 

        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(640, 480);
        frame.add(panel);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.toFront();
    }



    public static class CustomLayoutManager implements LayoutManager
    {
        public void addLayoutComponent(String name, Component comp)
        {
            System.out.println("addLayoutComponent");
        }

        public void layoutContainer(Container parent)
        {
            System.out.println("layoutContainer");
        }

        public Dimension minimumLayoutSize(Container parent)
        {
            System.out.println("minimumLayoutSize");
            return new Dimension();
        }

        public Dimension preferredLayoutSize(Container parent)
        {
            System.out.println("preferredLayoutSize");
            return new Dimension();
        }

        public void removeLayoutComponent(Component comp)
        {
            System.out.println("removeLayoutComponent");
        }
    }
}
  • 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-25T00:42:57+00:00Added an answer on May 25, 2026 at 12:42 am

    How do I get the JPanel to call addLayoutComponent()?

    It would only invoke this method if your layout manager uses constraints

    panel.add(button);  
    

    Try:

    panel.add("some constraint value", button);  
    

    This method is intended to be used to pass constraints to the layout manager. I think BorderLayout is the only layout manager that might have used this. However it generally should not be used anymore. Instead LayoutManager2 uses:

    public void addLayoutComponent(Component component, Object constraint)
    

    which allows you to pass any Object as a constraint.

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

Sidebar

Related Questions

I need create custom dialog and put JPanel into it. Is it possible?
Why do we need to create custom exceptions in .NET?
I need to create a custom volume slider for a WMP object. The current
I need to create a custom membership user and provider for an ASP.NET mvc
i kinda know how to create a Custom Web Part. But now i need
I need to create custom pathways using the Google Maps API V3 for a
I need to create a custom look for the Google Maps info windows (straight-edge
I need to create one textfield custom components which accepts only credit card number
I need to create a lot of custom Flex 4 skins, in most of
I need to create a dependency property on a custom control. The problem is

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.