Results 1 to 6 of 6
  1. #1
    Andolph23 is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default JLabel wont appear in front of background image

    I made a panel with a picture as the background but I can't make a label or button show up in front of the background image. Thanks in advance for the help

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    If you need more help post your SSCCE.

  3. #3
    Andolph23 is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.applet.*;
    import java.net.URL;
    import java.awt.Component;
    
    public class China extends JFrame
    {
    
        Image img;
    
        public static void main(String[] args)
        {
            new China();
        }
    
        public China()
        {
            // Window and Picture Setup
            this.setSize(415,335);
            this.setTitle("China Restaraunt");
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel panel = new PicturePanel();
    
    
             Toolkit kit = Toolkit.getDefaultToolkit();
            img = kit.getImage("C:\\Users\\Andy\\Pictures\\Mountain.jpg");
            img = img.getScaledInstance(400, -1, Image.SCALE_SMOOTH);
            this.repaint();
            JLabel label1 = new JLabel("Test label");
    
            panel.add(label1);
            this.add(panel);
            this.setVisible(true);
    
        }
    
        private class PicturePanel extends JPanel
        {
            public void paint(Graphics g)
            {
                g.drawImage(img,0,0,this);
            }
        }
    }

    If you run this code you can see that I have a picture set as the background but the label i made won't show up.

    Moderator Edit: Code tags added to aid readability
    Last edited by Fubarable; 05-26-2010 at 11:13 PM. Reason: Moderator Edit: Code tags added to aid readability

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Note changes in code. Also note that code has not been compiled nor tested.

    Java Code:
    public class China extends JFrame
    {
    
        Image img;
    
        public static void main(String[] args)
        {
            new China();
        }
    
        public China()
        {
            // Window and Picture Setup
            this.setPreferredSize(new Dimension(415,335)); // **** note change
            this.setTitle("China Restaraunt");
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JPanel panel = new PicturePanel();
    
    
            Toolkit kit = Toolkit.getDefaultToolkit();
            img = kit.getImage("C:\\Users\\Andy\\Pictures\\Mountain.jpg");
            img = img.getScaledInstance(400, -1, Image.SCALE_SMOOTH);
            // this.repaint(); // **** no need for this
            JLabel label1 = new JLabel("Test label");
    
            panel.add(label1);
            add(panel);
            pack();  // **** added
            setVisible(true);
    
        }
    
        private class PicturePanel extends JPanel
        {
            // **** this method should be paintComponent, not paint
            public void paintComponent(Graphics g)
            {
                //  **** don't forget to call the super method first
                super.paintComponent(g)
                g.drawImage(img,0,0,this);
            }
        }
    }

  5. #5
    Andolph23 is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    Thanks so much. It worked exactly how i wanted it to, but if you could explain some things so that I understand the changes better that would be great. For one, why did you use the setPreferredSize method instead of just setSize? Also, how is the paintComponent method ever called if it's not used in the China class? I thought that when i used this.paint(), it was calling the paint method but with it gone I don't see how this works.

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Swing tutorial

    why did you use the setPreferredSize method instead of just setSize?
    Read the section from the Swing tutorial on "Using Layout Managers" to understand how layout managers work.

    , how is the paintComponent method ever called
    Read the section from the Swing tutorial on "Custom Painting" for an explanation and examples (especially the part on "A Closer Look at the Paint Mechanism).

    Download a keep a reference to the tutorial.

Similar Threads

  1. uploaded image wont display if i change filename
    By schenker in forum Java Servlet
    Replies: 12
    Last Post: 06-11-2010, 11:13 AM
  2. Image in JLabel
    By ishere11 in forum AWT / Swing
    Replies: 2
    Last Post: 04-29-2010, 08:02 AM
  3. Background image
    By leiferouis in forum New To Java
    Replies: 9
    Last Post: 03-08-2009, 05:49 PM
  4. Image as background
    By Java.child in forum AWT / Swing
    Replies: 2
    Last Post: 10-02-2008, 11:37 PM
  5. Background image in java
    By toby in forum AWT / Swing
    Replies: 1
    Last Post: 07-29-2007, 07:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •