Solved

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

Posted on 2004-08-15
4
547 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now