Swing and background picture

Hi,

I am trying to put some labels and textfields to a JFrame with a jpeg picture as background. I am working with JLayeredPane and Swing, but the picture will not appear in  the background.

Anybody here who can help me with this?

This is my current source ...

___________________________________________
package daframe;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;


public class ChooseKey extends Frame{
   
    private char rotateLeft;
    private char rotateRight;
    private char pause;
    private char shiftLeft;
    private char shiftRight;
    private char dropDown;
    private int height;
    private int width;
   
    public ChooseKey(int width, int height, String pic)
    {
        TextField rotateLeft = new TextField("",1);
        TextField rotateRight = new TextField("",1);
        TextField pause = new TextField("p");
        TextField shiftLeft = new TextField("d");
        TextField shiftRight = new TextField("a");
        TextField dropDown = new TextField("s");
        Sprache dieSprache = new Sprache(spr);
       
        //set size and title for frame
        setTitle("choose keys");
        setSize(width,height);
           
        //create LayeredPane and add icon to label
        JLayeredPane pane = new JLayeredPane();
        //Image laden und auf Label packen
        ImageIcon back = new ImageIcon(pic);
        JLabel label = new JLabel(back);
        //Der LayeredPane eine Dimension zuweisen
        pane.setPreferredSize(new Dimension(width,height));
               
        //label.setLayout(new BorderLayout());
       
        //add picture to layer 0
        pane.add(label,new Integer(0));
       
        //rotateLeft.setColumns(1);
       
        //pane.setLayout(new FlowLayout());
        pane.setLayout(new GridLayout(6,2));            
               
        //create panel and add components for choosing the keys
        JPanel panel1 = new JPanel();
        panel1.setLayout(new FlowLayout(FlowLayout.LEFT));
        panel1.add(rotateLeft);
        panel1.add(new Label("key_rl"));
        panel1.setOpaque(false);
       
        JPanel panel2 = new JPanel();
        panel2.add(rotateRight);
        panel2.setLayout(new FlowLayout(FlowLayout.LEFT));        
        panel2.add(new Label("key_rr"));
        panel2.setOpaque(false);
       
        JPanel panel3 = new JPanel();
        panel3.setLayout(new FlowLayout(FlowLayout.LEFT));
        panel3.add(pause);
        panel3.add(new Label("key_p"));
        panel3.setOpaque(false);
       
        JPanel panel4 = new JPanel();
        panel4.setLayout(new FlowLayout(FlowLayout.LEFT));
        panel4.add(shiftLeft);
        panel4.add(new Label("key_sl"));
        panel4.setOpaque(false);
       
        JPanel panel5 = new JPanel();
        panel5.setLayout(new FlowLayout(FlowLayout.LEFT));
        panel5.add(shiftRight);
        panel5.add(new Label("key_sr"));
        panel5.setOpaque(false);
       
        JPanel panel6 = new JPanel();
        panel6.setLayout(new FlowLayout(FlowLayout.LEFT));
        panel6.add(dropDown);
        panel6.add(new Label("key_dd"));
        panel6.setOpaque(false);
       
                           
        pane.add(panel1, new Integer(1));
        pane.add(panel2, new Integer(1));
        pane.add(panel3, new Integer(1));
        pane.add(panel4, new Integer(1));
        pane.add(panel5, new Integer(1));
        pane.add(panel6, new Integer(1));
        pane.setOpaque(false);
               
        add(pane);
       
        addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        });
    }
   
   
/**
 * testdriver for class
 * @param args
 */    
    public static void main (String args[])
    {
        ChooseKey t = new ChooseKey(500,400,"images/back.jpg");
        t.show();
    }
_________________________________________


Thanks for your advise.

CU
LVL 7
gnurlAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bhaskar20001Commented:
Hello gnurl,

  which version of jdk you are using.I tested your program using both jdk1.4 and 1.3. But with 1.4 only I got jvm error.
    as with jdk1.3 it is working fine and also image is appearing in background. as you have given relative path for ur image file it depends on from which directory path u  r running ur application. and while testing ur program I commented the "Sprache"  instantiation statement..

--Bhaskar.


   
0
gnurlAuthor Commented:
Hello

--Bhaskar I am using jdk1.4 with Eclipse IDE.
I modified the source (deleted connection to other classes ... as you see  in "Sprache" ) so that ist seems that there is an error in it at the Moment ... I will fix in in some hours (i think) and repost it. Sorry.

The path is ok, but if I use an GridLayout the picture is showed as last component of the Grid ... so it is not an background image but an image in a grid...

Thanks
0
gnurlAuthor Commented:
Hi,

I think I could fix the 1.4 error .... just remove the line with the package declaration und set an closing } at the end of the file.

CU
0
jkrippyCommented:


I found some tips at http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=2&t=000315

I took that and applied it to your code.  I also refactored your code a bit to make it easier for me to see the flow of your GUI code.




import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ChooseKey extends JFrame {

      private char rotateLeft;
      private char rotateRight;
      private char pause;
      private char shiftLeft;
      private char shiftRight;
      private char dropDown;
      private int height;
      private int width;

      public ChooseKey(int width, int height, String pic) {

            // set size and title for frame
            setTitle("choose keys");
            setSize(width, height);

            ImageIcon backgroundImage = new ImageIcon(pic);
            JLabel label = new JLabel(backgroundImage);
            label.setBounds(0, 0, width, height);

            // See link at:
            // http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=2&t=000315
            JDesktopPane dp = new JDesktopPane();
            JPanel transparentPanel = new JPanel();
            transparentPanel.setOpaque(false);
            transparentPanel.setBounds(0, 0, width, height);
            transparentPanel.setPreferredSize(new Dimension(width, height));
            transparentPanel.setLayout(new GridLayout(6, 2));

            // add components for choosing the keys                        
            transparentPanel.add(createPanel("", 1, "key_rl"), new Integer(1));
            transparentPanel.add(createPanel("", 1, "key_rr"), new Integer(1));
            transparentPanel.add(createPanel("p", "key_p"), new Integer(1));
            transparentPanel.add(createPanel("d", "key_sl"), new Integer(1));
            transparentPanel.add(createPanel("a", "key_sr"), new Integer(1));
            transparentPanel.add(createPanel("s", "key_dd"), new Integer(1));
            transparentPanel.setOpaque(false);

            dp.add(label);
            dp.add(transparentPanel);
            setLayeredPane(dp);

            addWindowListener(new WindowAdapter() {
                  public void windowClosing(WindowEvent e) {
                        System.exit(0);
                  }
            });
      }

      protected JPanel createPanel(String textFieldValue, String label) {
            return createPanel(textFieldValue, 0, label);
      }

      protected JPanel createPanel(
            String textFieldValue,
            int column,
            String label) {

            JPanel panel = new JPanel();
            panel.setLayout(new FlowLayout(FlowLayout.LEFT));
            panel.add(new TextField(textFieldValue, column));
            panel.add(new Label(label));
            panel.setOpaque(false);
            return panel;
      }

      /**
      * testdriver for class
      * @param args
      */
      public static void main(String args[]) {

            ChooseKey t = new ChooseKey(500, 400, "images/back.jpg");
            t.show();
      }
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gnurlAuthor Commented:
Hi jkrippy,

this helps a lot, thank you.

CU gnurl
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.