-1

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);
blackDog
  • 1
  • 1
  • 5
  • *"unable to find a way of making a system looking swing JButton."* See [`UIManager.getSystemLookAndFeelClassName()`](http://docs.oracle.com/javase/7/docs/api/javax/swing/UIManager.html#getSystemLookAndFeelClassName%28%29). – Andrew Thompson Nov 29 '12 at 23:26
  • *"may be done in a reasonably sized block of code."* Put a number (of lines) to 'reasonable'. *"Every sight iv looked at "claims" this can be done"* Link to the top 3 pages you looked at. – Andrew Thompson Nov 29 '12 at 23:29
  • single didits would be nice... i find web sights like to post solutions in the tripple digits. Then with a lot of poorly alocated time I find that i can reduce it to 5-10 lines of code myself... just thought I would save my self a few hoursand ask around – blackDog Nov 29 '12 at 23:35
  • *"just thought I would save my self a few hoursand ask around"* You should do a couple of hours research before putting your question to SO. -1 for 'does not show any research effort'. – Andrew Thompson Nov 29 '12 at 23:49
  • "You should be a teacher, you would fit right in with todays education system being the way it is" in terms of 'on question forum grading pepole with no intent to teach' – blackDog Nov 30 '12 at 00:34
  • *"..with no intent to teach"* Huh. Where is (any) evidence of your 'intention to learn'? Such evidence might be seen in linking to 3 of the top pages you had seen, as I suggested in my 2nd comment. – Andrew Thompson Nov 30 '12 at 00:39

2 Answers2

4

This example seems to work with the following lines added to the constructor:

this.addMouseListener(new MouseHandler(this));
this.add(new JLabel("Label"));
this.add(new JButton(UIManager.getIcon("html.pendingImage")));

enter image description here

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • `LayerDemo extends JDialog` requires a different close operation. – trashgod Nov 29 '12 at 23:43
  • ya this looks a little better... could you help me out with something a little more spesific. I think i was a littile to vag in my post. – blackDog Nov 29 '12 at 23:43
  • final ImageIcon icon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(Elevation.class.getResource("UACicon.png"))); final JLabel label = new JLabel(icon); final JDialog dialog = new JDialog(null,"Windows System Update - Koala",Dialog.ModalityType.APPLICATION_MODAL); dialog.getLayeredPane().setLayer(label,0); dialog.pack(); dialog.setVisible(true); – blackDog Nov 29 '12 at 23:46
  • 1
    Please edit your question to include an [sscce](http://sscce.org/) that exhibits any problem(s) you encounter. Either of the two existing answers might be suitable. Code in a comment is largely unreadable; ImageIO.read()` might be a better choice. – trashgod Nov 29 '12 at 23:47
  • sorry im not sure how to post code to this answere box and im not popular enough to answere my own question in a real responce box so I had to make a new question with a better description. – blackDog Nov 30 '12 at 00:01
  • 1
    I migrated the code for you, but you need to work on an sscce. You should be able to edit your own question. – trashgod Nov 30 '12 at 00:03
  • I think you're getting confused over the use of the `JRootPane`'s layered pane. There is at least on other component already on the layered pane, that's the `contentPane`. You might want to have read of [this](http://docs.oracle.com/javase/tutorial/uiswing/components/rootpane.html) – MadProgrammer Nov 30 '12 at 00:08
  • do you know why my dialog appears blank? – blackDog Nov 30 '12 at 00:22
  • 1
    @blackDog Take a look at [How to Use Layered Panes](http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html), you're basically competing for a layer position and losing – MadProgrammer Nov 30 '12 at 00:48
4

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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366