Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java Swing: Buttons painted on LayeredPane so they float over an Image painted on ContentPane

Posted on 2004-08-15
4
Medium Priority
?
560 Views
Last Modified: 2013-11-23
I need to place some floating buttons over a background.  What I tried to do is paint the background image on the ContentPane of a JFrame and the buttons on the LayeredPane positioned with struts.  This works when I add the buttons directly to the LayeredPane, but when I encapsulate the buttons in a JPanel and then add the panel to the LayeredPane the panel does not show up.  I wrote the following test code which illustrates the problem in its simplest form.

I'll give out an B to someone who gives enough help for me to fix it myself and an A to someone who rewrites the code so it works:

public class ButtonsOverBackground extends JFrame implements Constants
{
   public ButtonsOverBackground()
   {

      /** create background **/
      JLabel thermostatImage;
      thermostatImage = new JLabel(new ImageIcon(BACKGROUND_PATH));

      /** put background in content pane **/
      getContentPane().add(thermostatImage);

      /** create button **/
      JButton b = new JButton(new ImageIcon(IMAGE_PATH + "minus.PNG"));
             
      b.setPressedIcon(new ImageIcon(IMAGE_PATH + "minusIndented.PNG"));
      b.setSelectedIcon(new ImageIcon(IMAGE_PATH + "minusIndented.PNG"));            
      b.setMargin(new Insets(TOP_INSET, LEFT_INSET, BOTTOM_INSET,            
         RIGHT_INSET));
      b.setBorderPainted(false);
      b.setHorizontalAlignment(SwingConstants.LEFT);

      b.setBounds(0,0,70,70);
      b.setOpaque(true);

      /*** put button in layered pane ***/

      /** this works **/
      //getLayeredPane().add(b);

      /** this does not work **/
      JPanel p = new JPanel();
      p.add(b);            
      getLayeredPane().add(p);

            
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
      
   public static void main(String[] args)
   {
      ButtonsOverBackground bob = new ButtonsOverBackground();
      bob.pack();
      bob.show();
   }
}
0
Comment
Question by:Juzzam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 11806136
you don't set the bounds for the panel
probably also want to make the panel transparent.
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 11806137
JPanel p = new JPanel();
 p.setBounds(0, 0, 100, 100);
 p.setOpaque(false);
 p.add(b);          
 getLayeredPane().add(p);
0
 
LVL 22

Expert Comment

by:NovaDenizen
ID: 11810604
Add the buttons before you add the background (or use the add(b, 0) method to add the buttons at the beginning of the list).  When you add the background component first, the background will be considered "on top" of all the other components.  See the javadocs for java.awt.Container.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question