Results 1 to 20 of 22
Thread: JLayeredPane issues?!
- 04-25-2012, 12:49 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 16
- Rep Power
- 0
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
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 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(); } }); } }
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
- 04-25-2012, 01:40 AM #2
Re: JLayeredPane issues?!
How do you know that DP does not show up? What do you expect to see?
If you don't understand my response, don't ignore it, ask a question.
- 04-25-2012, 07:13 AM #3
Re: JLayeredPane issues?!
Moved from New to Java
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-25-2012, 11:07 PM #4
Member
- Join Date
- Apr 2012
- Posts
- 16
- Rep Power
- 0
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?
- 04-25-2012, 11:12 PM #5
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.
- 04-25-2012, 11:25 PM #6
Member
- Join Date
- Apr 2012
- Posts
- 16
- Rep Power
- 0
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?
- 04-25-2012, 11:30 PM #7
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.
- 04-25-2012, 11:37 PM #8
Member
- Join Date
- Apr 2012
- Posts
- 16
- Rep Power
- 0
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(); } }); } }
- 04-25-2012, 11:45 PM #9
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.
- 04-25-2012, 11:55 PM #10
Member
- Join Date
- Apr 2012
- Posts
- 16
- Rep Power
- 0
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
- 04-25-2012, 11:58 PM #11
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.
- 04-26-2012, 12:06 AM #12
Member
- Join Date
- Apr 2012
- Posts
- 16
- Rep Power
- 0
- 04-26-2012, 12:14 AM #13
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.
- 04-26-2012, 12:18 AM #14
Member
- Join Date
- Apr 2012
- Posts
- 16
- Rep Power
- 0
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
- 04-26-2012, 12:21 AM #15
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.
- 04-29-2012, 12:21 AM #16
Member
- Join Date
- Apr 2012
- Posts
- 16
- Rep Power
- 0
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:
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?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(); } }); } }
- 04-29-2012, 12:31 AM #17
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.
- 04-29-2012, 12:55 AM #18
Member
- Join Date
- Apr 2012
- Posts
- 16
- Rep Power
- 0
- 04-29-2012, 12:59 AM #19
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.
- 04-29-2012, 01:10 AM #20
Member
- Join Date
- Apr 2012
- Posts
- 16
- Rep Power
- 0
Similar Threads
-
JLabel Dragging with Container as opposed to JLayeredPane
By 5myl in forum New To JavaReplies: 3Last Post: 01-02-2012, 05:35 AM -
JLayeredPane - JApplet trouble
By Dustin T in forum New To JavaReplies: 4Last Post: 09-20-2011, 04:29 AM -
Playing cards on other than a JLayeredPane, or not?
By Markgm in forum AWT / SwingReplies: 1Last Post: 05-30-2011, 09:37 PM -
JLayeredPane not displaying components
By abc in forum AWT / SwingReplies: 3Last Post: 01-06-2011, 03:40 PM -
How to add JScrolPane to a JLayeredPane?
By chyrl in forum Advanced JavaReplies: 5Last Post: 08-30-2010, 04:28 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks