Page 1 of 2 12 LastLast
Results 1 to 20 of 22
Like Tree1Likes

Thread: JLayeredPane issues?!

  1. #1
    seniorstatus is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default JLayeredPane issues?!

    in my GUI class i have a Frame that holds a Jpanel which holds another JPanel and a JLayeredPane. the jpanel which calls "MenuPanel" works fine, but the JLayeredPane which calls "DP" wont show up. any suggestions on how to fix this error?

    The code for my GUI Class
    Java Code:
    package mypackage;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Color;
    import java.util.*;
    import javax.swing.*;
    
    public class GUI
    {
        public MPanel Menu = new MPanel();//MPanel interface call
        public DPanel DP = new DPanel();
    	JPanel MenuPanel;
    	JLayeredPane DataPanel;
    	JPanel mainPane;
    	
    	ImageIcon backgroundimage=new ImageIcon("http://www.java-forums.org/images/background5.jpg");
    	 
    	public GUI() 
        {
    		GridBagLayout gridbag = new GridBagLayout();
    		GridBagConstraints constraints = new GridBagConstraints();
            MenuPanel = new JPanel();
            MenuPanel.setOpaque(false);
            MenuPanel.add(Menu);
            Menu.setMinimumSize(new Dimension(500, 600));
            Menu.setPreferredSize(new Dimension(500, 600));
            Menu.setMaximumSize(new Dimension(500, 600));
            DataPanel = new JLayeredPane();
            DataPanel.setOpaque(false);
            DataPanel.add(DP, JLayeredPane.DEFAULT_LAYER);
            DataPanel.setVisible(true);
            DataPanel.setPreferredSize(new Dimension(1080, 600));
            
         //create the main panel and add the other panels as components
            mainPane = new JPanel();
            mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.LINE_AXIS));
            mainPane.setBorder(BorderFactory.createEmptyBorder(0,15,15,15));
            mainPane.add(Box.createRigidArea(new Dimension(0, 0)));
            mainPane.add(MenuPanel);
            mainPane.add(Box.createRigidArea(new Dimension(1, 0)));
            mainPane.add(DataPanel);
            mainPane.add(Box.createGlue());
            mainPane.setPreferredSize(new Dimension(1620, 610));
        }
     
    
        private static void createAndShowGUI() {
            JFrame frame = new JFrame("MIPS SIMULATOR");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //Create and set up the content pane.
            GUI panel = new GUI();
            panel.mainPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(panel.mainPane);
            frame.setSize(1580, 610);
            frame.setLocationRelativeTo(null);
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
     
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    the code for DPanel which is being called:
    Java Code:
    package mypackage;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Color;
    import java.util.*;
    import javax.swing.*;
    
    public class DPanel extends JPanel
    {
    	public JLabel Test;
    	
    	 DPanel()
    	{
    		Test = new JLabel();
    		Test.setText("This is a *** test!! please work!!!");
    		
    	}
    }
    Last edited by Norm; 04-25-2012 at 01:39 AM. Reason: word removed

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: JLayeredPane issues?!

    How do you know that DP does not show up? What do you expect to see?
    Fubarable likes this.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: JLayeredPane issues?!

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    seniorstatus is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default Re: JLayeredPane issues?!

    Hey. Thanks for the response. I know it doesnt work because I should see the label ”test” from the Dpanel class. instead, I just see an empty panel. I even tried adjusting the z index, didnt work. Any ideas?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: JLayeredPane issues?!

    What is there to be seen in the panel? Where do you add anything to it?
    Put this in DPanel class's constructor:
    add(new JLabel("Here is something"));
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    seniorstatus is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default Re: JLayeredPane issues?!

    thanks, i just tried it, no avail :-/ i. snt that redundant though since in the DPanel class i created a label and set the text?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: JLayeredPane issues?!

    Can you Post the code that you just tried.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    seniorstatus is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default Re: JLayeredPane issues?!

    i commented out the call to DPanel and just added it directly into the the GUI class to see if the issue is a call to the DPanel class or a display issue. seems to be a display issue.
    Java Code:
    package mypackage;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.Color;
    import java.util.*;
    import javax.swing.*;
    
    public class GUI
    {
        public MPanel Menu = new MPanel();//MPanel interface call
        public DPanel DP = new DPanel();
    	JPanel MenuPanel;
    	JLayeredPane DataPanel;
    	JPanel mainPane;
    	
    	ImageIcon backgroundimage=new ImageIcon("images/background5.jpg");
    	 
    	public GUI() 
        {
    		GridBagLayout gridbag = new GridBagLayout();
    		GridBagConstraints constraints = new GridBagConstraints();
            MenuPanel = new JPanel();
            MenuPanel.setOpaque(false);
            MenuPanel.add(Menu);
            Menu.setMinimumSize(new Dimension(500, 600));
            Menu.setPreferredSize(new Dimension(500, 600));
            Menu.setMaximumSize(new Dimension(500, 600));
            DataPanel = new JLayeredPane()
            {
            	{
            		add(new JLabel("Here is something"));
            	}
            };
            DataPanel.setOpaque(false);
            //DataPanel.add(DP, JLayeredPane.DEFAULT_LAYER);
            DataPanel.setVisible(true);
            DataPanel.setPreferredSize(new Dimension(1080, 600));
            
         //create the main panel and add the other panels as components
            mainPane = new JPanel();
            mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.LINE_AXIS));
            mainPane.setBorder(BorderFactory.createEmptyBorder(0,15,15,15));
            mainPane.add(Box.createRigidArea(new Dimension(0, 0)));
            mainPane.add(MenuPanel);
            mainPane.add(Box.createRigidArea(new Dimension(1, 0)));
            mainPane.add(DataPanel);
            mainPane.add(Box.createGlue());
            mainPane.setPreferredSize(new Dimension(1620, 610));
        }
     
    
        private static void createAndShowGUI() {
            JFrame frame = new JFrame("MIPS SIMULATOR");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            //Create and set up the content pane.
            GUI panel = new GUI();
            panel.mainPane.setOpaque(true); //content panes must be opaque
            frame.setContentPane(panel.mainPane);
            frame.setSize(1580, 610);
            frame.setLocationRelativeTo(null);
            //Display the window.
            frame.pack();
            frame.setVisible(true);
        }
     
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: JLayeredPane issues?!

    Where in this mess of GUI construction do you expect to see the DPanel object to show?
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    seniorstatus is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default Re: JLayeredPane issues?!

    if you uncomment line 35, the DPANEL should be shown in the JLayeredPane named "DataPanel". however i commented it out just to see if the layeredpane is working without calling another class by using line 31. however, that Jlabel isnt even output to the screen

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: JLayeredPane issues?!

    Where is the JLayeredPane supposed to show in the GUI? I find the way it is written to be confusing and hard to follow.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    seniorstatus is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default Re: JLayeredPane issues?!

    sorry. here is an Image: JLayeredPane issues?!-screenshot.jpg


    im going to add comments to my code to help you follow. but did the image answer your question?

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: JLayeredPane issues?!

    Have you ever gotten the JLayeredPane to display?
    If not, remove all the other GUI and work on that by itself. When you can see it, then work in the other components.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    seniorstatus is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default Re: JLayeredPane issues?!

    I havent gotten it to display at all. it seems JPanels will display without any issues, but JLayeredPanes wont show. im trying something now. im going to post it up in a few and let you know what happens

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: JLayeredPane issues?!

    Go to the tutorial, get some working code and work with it to see how the class works.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    seniorstatus is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default Re: JLayeredPane issues?!

    AHA!

    i went through the tutorial a thousand times (exaggeration), but i found the error. My DPanel class was missing all sorts of stuff. i needed to set the layout since JLayeredPanes dont automatically have a layout manager., then i needed to create the image as an icon, and set the icon as a JLabel. then i had to add a frame with a JComponent and add the DPanel to it and then finally it worked, somewhat. lol the code for the DPanel is:

    Java Code:
    package mypackage;
    import java.awt.*; 
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import java.awt.Color;
    import java.io.File;
    import java.util.*;
    import javax.swing.*;
    import javax.accessibility.*;
    
    public class DPanel extends JPanel
    {
    	
    	private JLayeredPane layeredPane;
    	private JLabel backgroundLabel;
    	
    	DPanel()
    	{
    		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    		//create and load the background icon
    		final ImageIcon icon = createImageIcon("/src/mypackage/background5.jpg");
    		
    		//create and setup the layered pane
    		layeredPane = new JLayeredPane();
    		layeredPane.setPreferredSize(new Dimension(1080, 600));
    		
    		//create and add the background label to the layered pane
    		backgroundLabel = new JLabel(icon);
    		if (icon != null)
    		{
    			backgroundLabel.setBounds(0, 0, icon.getIconWidth(), icon.getIconHeight());
    		}
    		else
    		{
    			System.err.println("background icon not found; using black square instead.");
    			backgroundLabel.setBounds(0, 0, 1080, 600);
    			backgroundLabel.setOpaque(true);
    			backgroundLabel.setBackground(Color.BLACK);
    		}
    		layeredPane.add(backgroundLabel, new Integer(2), 0);
    		layeredPane.setLayer(backgroundLabel, 2);
    		
    		//add layered pane to this JPanel
    		add(Box.createRigidArea(new Dimension(0,10)));
    		add(layeredPane);
    	}
    		
    		/** Returns an ImageIcon, or null if the path was invalid. */
    	protected static ImageIcon createImageIcon(String path)
    	{
    		java.net.URL imgURL = DPanel.class.getResource(path);
    		if (imgURL != null)
    		{
    			return new ImageIcon(imgURL);
    		}
    		else
    		{
    			System.err.println("Couldn't find file: " + path);
    			return null;
    		}
    	}
    	
    	private static void createAndShowGUI()
    	{
    		//create and set up the window.
    		JFrame frame = new JFrame("Data Path");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		//create and setup the content pane
    		JComponent newContentPane = new DPanel();
    		newContentPane.setOpaque(true);
    		frame.setContentPane(newContentPane);
    		
    		//display the window
    		frame.pack();
    		frame.setVisible(true);
    	}
    	
    	public static void main(String[] args)
    	{
    		//schedule a job for the event-dispatching thread:
    		//creating and showing this applications GUI.
    		javax.swing.SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				createAndShowGUI();
    			}
    		});
    	}
    }
    so it works, such that i see output on the screen. However, my image cant be found. i get the flags as coded "System.err.println("background icon not found; using black square instead.");" and System.err.println("Couldn't find file: " + path);. the black square is shown where the image is supposed to be. ive moved the location of "background5.jpg" countless times and my app still cant find it. any suggestions?

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: JLayeredPane issues?!

    To find the path to a file that a program is using, create a File object using the path and print that object's absolute path?
    If you don't understand my response, don't ignore it, ask a question.

  18. #18
    seniorstatus is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default Re: JLayeredPane issues?!

    Quote Originally Posted by Norm View Post
    To find the path to a file that a program is using, create a File object using the path and print that object's absolute path?
    i created the file object :
    Java Code:
    File path1 = new File("src/background5.jpg");

    how and where would i print the object's absolute path?

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: JLayeredPane issues?!

    Use: System.out.println(<path1's absolutepath>);

    Your path is not the same as in the code.
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    seniorstatus is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default Re: JLayeredPane issues?!

    wont that cause a problem though? say i move the project to another computer, then i have to go and change the path again? is there a way to use a relative path?

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 3
    Last Post: 01-02-2012, 05:35 AM
  2. JLayeredPane - JApplet trouble
    By Dustin T in forum New To Java
    Replies: 4
    Last Post: 09-20-2011, 04:29 AM
  3. Replies: 1
    Last Post: 05-30-2011, 09:37 PM
  4. JLayeredPane not displaying components
    By abc in forum AWT / Swing
    Replies: 3
    Last Post: 01-06-2011, 03:40 PM
  5. How to add JScrolPane to a JLayeredPane?
    By chyrl in forum Advanced Java
    Replies: 5
    Last Post: 08-30-2010, 04:28 AM

Tags for this Thread

Posting Permissions

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