Solved

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

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

770 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