Go Premium for a chance to win a PS4. Enter to Win

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
?
562 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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

971 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