Solved

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

Posted on 2004-08-15
4
553 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
  • 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 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

828 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