Re: JLayeredPane issues?!
How do you know that DP does not show up? What do you expect to see?
Re: JLayeredPane issues?!
Moved from New to Java
db
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?
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"));
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?
Re: JLayeredPane issues?!
Can you Post the code that you just tried.
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.
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();
}
});
}
}
Re: JLayeredPane issues?!
Where in this mess of GUI construction do you expect to see the DPanel object to show?
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
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.
1 Attachment(s)
Re: JLayeredPane issues?!
sorry. here is an Image: Attachment 3634
im going to add comments to my code to help you follow. but did the image answer your question?
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.
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
Re: JLayeredPane issues?!
Go to the tutorial, get some working code and work with it to see how the class works.
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:
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?
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?
Re: JLayeredPane issues?!
Quote:
Originally Posted by
Norm
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 :
Code:
File path1 = new File("src/background5.jpg");
how and where would i print the object's absolute path?
Re: JLayeredPane issues?!
Use: System.out.println(<path1's absolutepath>);
Your path is not the same as in the code.
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?