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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:34:41+00:00 2026-05-27T17:34:41+00:00

how can I correctly returns XxxSize from JComponent(s) added to the JLabel 1st. figure

  • 0

how can I correctly returns XxxSize from JComponent(s) added to the JLabel

1st. figure >> lets LayoutManager works like as for JPanel, JLabel returns Size(0, 0)

enter image description here

2nd. figure >> added some PreferredSize to the JLabel

enter image description here

3rd. figure >> calculated PreferredSize from JComponent(s) added to the JLabel

enter image description here

4th. figure >> lets LayoutManager works changed JLabel to JPanel, now LayoutManager correctly calculated Dimension without using any XxxSize

enter image description here

notice sice there is used Nimbus L&F, same output is there for all accesible L&F

import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;
import java.util.Queue;
import javax.swing.*;

public class NimbusBorderPainterDemo extends JFrame {

    private static final long serialVersionUID = 1L;
    private JFrame frame = new JFrame();
    private JPanel fatherPanel = new JPanel(), titlePanel = new JPanel();
    private JLabel buttonPanel = new JLabel();


    //figure  ---> 4th. switch JLabel with JPanel
    //private JPanel buttonPanel = new JPanel();
    private Queue<Icon> iconQueue = new LinkedList<Icon>();

    public NimbusBorderPainterDemo() {
        iconQueue.add(UIManager.getIcon("OptionPane.errorIcon"));
        iconQueue.add(UIManager.getIcon("OptionPane.informationIcon"));
        iconQueue.add(UIManager.getIcon("OptionPane.warningIcon"));
        JButton button0 = createButton();
        JButton button1 = createButton();
        JButton button2 = createButton();
        button2.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.exit(1);
            }
        });
        int gap = 5;
        buttonPanel.setLayout(new GridLayout(0, 3, gap, 0));
        buttonPanel.add(button0);
        buttonPanel.add(button1);
        buttonPanel.add(button2);

        // figure 1st. --->  without PreferredSize

        // figure 2nd. --->
        //buttonPanel.setPreferredSize(new Dimension(160, 30));

        // figure 3rd. --->
        /*Dimension dim = button0.getPreferredSize();
        int w = dim.width;
        int h = dim.height;
        w = (w + 5) * 3;
        h += 4;
        dim = new Dimension(w, h);
        buttonPanel.setPreferredSize(dim);*/

        titlePanel.setLayout(new BorderLayout());
        titlePanel.add(new JLabel(nextIcon()), BorderLayout.WEST);
        titlePanel.add(new JLabel("My Frame"), BorderLayout.CENTER);
        titlePanel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
        titlePanel.add(buttonPanel, BorderLayout.EAST);
        fatherPanel.setLayout(new BorderLayout());
        fatherPanel.add(titlePanel, BorderLayout.CENTER);
        frame.setUndecorated(true);
        frame.add(fatherPanel);
        frame.setLocation(50, 50);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.setVisible(true);
    }

    private JButton createButton() {
        JButton button = new JButton();
        button.setBorderPainted(false);
        button.setBorder(null);
        button.setFocusable(false);
        button.setMargin(new Insets(0, 0, 0, 0));
        button.setContentAreaFilled(false);
        button.setIcon(nextIcon());
        //button.setRolloverIcon(nextIcon());
        //button.setPressedIcon(nextIcon());
        //button.setDisabledIcon(nextIcon());
        nextIcon();
        return button;
    }

    private Icon nextIcon() {
        Icon icon = iconQueue.peek();
        iconQueue.add(iconQueue.remove());
        return icon;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
                } catch (Exception fail) {
                }
                UIManager.getLookAndFeelDefaults().put("nimbusFocus", Color.RED);
                NimbusBorderPainterDemo nimbusBorderPainterDemo = new NimbusBorderPainterDemo();
            }
        });
    }
}
  • 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-27T17:34:42+00:00Added an answer on May 27, 2026 at 5:34 pm

    The default preferred size calculation is to use the layout manager to determine the preferred size of a component. This means the layout manager iterates through all the child components to determine the preferred size of each. For a JPanel, which is meant to be used as a Container this calculation is used.

    However, for other Swing components, the getPreferredSize() method is always overridden to provide a reasonable size for the given component.

    In the case of a JLabel, the preferred size calculation takes into account the text and the icon used. Since you didn’t provide either the preferred size is zero. Of course if you manually override this calculation by using the setPreferredSize() method then the component will have a preferred size.

    So even though Swing allows you to add components to any component and use a layout manager to layout the child components, these child components are not used in the preferred size calculation.

    This is not just a Nimbus issue.

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

Sidebar

Related Questions

I've an issue with my JSON. It works returns correctly in PHP 5.3 (so
Ok so I can reference my table correctly in a html page like this:
Using PowerShell, I can do: $shell = new-object -com Shell.Application $shell.GetSetting(0x2) Which correctly returns
Can anyone tell me how I can correctly pass my application context to my
The JSON that I am parsing can be found here 'redacted'. I can correctly
Here's the troublesome function. I can not correctly locate the nested UL element: function
Not sure if I can explain this correctly, but I am trying to execute
I'm a bit worried if this function sends emails that can be recognized correctly
How can i write this correctly ? I want to check how many days
I can start the service correctly if the service's Log on account has a

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.