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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:31:24+00:00 2026-06-15T05:31:24+00:00

I am unable to make any components apear on a JLayeredPane when adding it

  • 0

I am unable to make any components apear on a JLayeredPane when adding it to a JDialog.

I have also been unable to find a web resource that shows this may be done in a reasonably sized block of code. Every sight iv looked at “claims” this can be done, and then shows a disgustingly long solution.

What i want is to take a JLayered pane add a Button and place a JLabel with an icon in it onto this pane aswell. In english i want a button with an icon stuck in the front of its text.

That is the awt Button as I have been unable to find a way of making a system looking swing JButton.

Edit: could you help me out with something a little more specific. I think I was a littile to vague in my post.

Button button = new Button("ok");
JDialog dialog = new JDialog(null,"Windows",Dialog.ModalityType.APPLICATION_MODAL);
dialog.getLayeredPane().add(button);
dialog.pack();
dialog.setVisible(true);
  • 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-15T05:31:25+00:00Added an answer on June 15, 2026 at 5:31 am

    I don’t seem to have any issues…

    public class TestLayeredDialog {
    
        public static void main(String[] args) {
            new TestLayeredDialog();
        }
    
        public TestLayeredDialog() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JDialog dialog = new JDialog();
                    dialog.setModal(true);
                    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                    dialog.setLayout(new BorderLayout());
                    dialog.add(new MyContent());
                    dialog.pack();
                    dialog.setLocationRelativeTo(null);
                    dialog.setVisible(true);
    
                    System.exit(0);
                }
            });
        }
    
        public class MyContent extends JLayeredPane {
    
            public MyContent() {
                JLabel label = new JLabel("Hello new world");
                label.setSize(label.getPreferredSize());
                label.setLocation(0, 0);
                add(label);
    
                Dimension size = getPreferredSize();
    
                JButton button = new JButton("Click me");
                button.setSize(button.getPreferredSize());
                button.setLocation(size.width - button.getWidth(), size.height - button.getHeight());
                button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        SwingUtilities.getWindowAncestor(MyContent.this).dispose();
                    }
                });
                add(button);
            }
    
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(200, 200);
            }
    
        }
    
    }
    

    Remember, JLayeredPane DOES NOT have a layout manager. You become responsible for managing the size and position of the child components, that’s the point.

    Updated with new example

    enter image description here

    public class TestLayeredDialog {
    
        public static void main(String[] args) {
            new TestLayeredDialog();
        }
    
        public TestLayeredDialog() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JDialog dialog = new JDialog();
                    dialog.setModal(true);
                    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                    dialog.setLayout(new BorderLayout());
    
                    JLabel label = new JLabel("Hello new world");
                    label.setSize(label.getPreferredSize());
                    label.setLocation(0, 0);
                    dialog.getLayeredPane().add(label, new Integer(1));
    
                    dialog.setSize(100, 100);
                    dialog.setLocationRelativeTo(null);
                    dialog.setVisible(true);
    
                    System.exit(0);
                }
            });
        }
    }
    

    The layered pane of the JRootPane is responsible for (amongst other things) laying out the content pane and menu bar. It is also used (in some cases) to display things like popups.

    enter image description here

    Have a read through How to Use Root Panes

    You can choose to put components in the root pane’s layered pane. If
    you do, then you should be aware that certain depths are defined to be
    used for specific functions, and you should use the depths as
    intended. Otherwise, your components might not play well with the
    others. Here’s a diagram that shows the functional layers and their
    relationship:

    Using this, means you are competing with components already on the screen.

    Unless you have VERY good reason to be messing with this component, I would suggest you avoid it as 1- It’s possible to be changed in the future (the layer position of the components) and 2- It may interfere with other components used by the Swing API

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

Sidebar

Related Questions

I have been unable to figure out this problem - it just doesn't make
I have been trying to make jquery datepicker plugin work on a content page
After loading updatepanel through Timer, i am unable to make any async request from
I'm trying to make a floating joystick for my android game but am unable
I'm struggling to make Glassfish 3.1.1 to log to syslog, but I'm unable to.
I'm unable to execute the following prepared statement , PREPARE stmt FROM 'SELECT sb.id,sb.category,sb.make,sb.name,sb.pic,rs.slot
I am unable to change my max execution time. I have changed it in
I've been unable to install the autotest-fsevent gem after updating to Xcode 4.3 (removed
Ok, I've searched quite a bit, but seem unable to find an answer or
I'd like to be able to make an Ajax call using JSF/Seam/RichFaces and have

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.