Sorry to ask such a dumb question, but I can't figure out why this is crashing. I'm just trying to add a JLabel on top of my JPanel. The JLabel simply is there to display an image. I'm getting a NullPointerException.
(prediction: this is going to wind up being one of those "duh" moments.)
public JDlgTable() {
ImageIcon myImg = new ImageIcon("image.png");
JLabel myLabel = new JLabel(myImg);
myPanel.add(myLabel); <-- error happens here
}
Thanks.
Related
I'm working on a GUI for a Tic Tac Toe project, in which I want to get an image (the player's move) to show up when clicking the corresponding area. However, I can't figure out how to display a JLabel (the move) on top of another JLabel (the background).
I have searched and tried to use layeredpanes, but it just fails to show the entire frame when I run it. I am new to GUI, so I am not quite sure if I implemented it correctly.
layer = new JLayeredPane();
//set up the board as background
String path = "sampleUrl";
URL url = new URL(path);
BufferedImage image = ImageIO.read(url);
bg4 = new JLabel(new ImageIcon(image));
layer.add(bg4, new Integer(-1));
frame.add(layer);
frame.pack();
frame.addMouseListener(new MouseListener4());
String xPath = "sampleUrl";
URL xUrl = new URL(xPath);
BufferedImage x = ImageIO.read(xUrl);
icon = new JLabel(new ImageIcon(x));
layer.add(icon);
It just shows up a window with minimum width and height, without displaying the background. Is there something wrong with this code, or any other ways for me to put a JLabel on top of another JLabel?
You can add one label object into another, as JLabel is a container. Try something like this.
JLabel parentLabel = new JLabel("Parent");
JLabel childLabel = new JLabel("Child");
parentLabel.add(childLabel );
I highly recommend you to read about javafx if you are new in GUI.you can merge nodes in any way you need.you can put labels on each other using stackpane,and display them top and down of each other using gridpane very easily.
more information on:
https://docs.oracle.com/javafx/2/
i writing gui for my program in java, and i want to show some jlabel in multiply jlabels. im using gridlayout.
parts of code:
protected JLabel myLbl = new JLabel("1");
protected JLabel first = new JLabel("2");
protected JLabel second = new Jlabel("3");
in the constructor:
this.first.add(myLbl);
this.second.add(myLbl);
I saw a few questions on the subject, here in StackOverFlow, but I could not find a solution there without creating a duplicate JLabels.. And without creating another layout.
Is there a way to do this without creating a new layout or duplicate JLabels?
Thanks
Here is the code that I am trying to edit:
game = new JPanel();
ImageIcon bbb = new ImageIcon("bbb.gif");
JLabel bbbl = new JLabel(bbb);
ImageIcon bbbH = new ImageIcon("bbbH.gif");
JLabel bbbHl = new JLabel(bbbH);
game.setLayout(new GridLayout(2,2));
game.add(bbol);
game.add(bbgl);
game.add(bbgrl);
game.add(bbbl);
if (flashed == 1)
{
game.remove(bbol);
game.add(bboHl);
}
else
{
}
I want the JLabel bboHl to go in the same position as the JLabel bbol however there are other JLabels after this one, 3 more to be exact, therefor explaining why the layout is (GridLayout(2,2))
Would I need to change the layout?
Removing/adding components to the layout is way too expensive.
From what I understand, you just want to toogle an image :
Add only one JLabel, and use setIcon on it to change the image.
game = new JPanel();
ImageIcon bbb = new ImageIcon("bbb.gif");
ImageIcon bbbH = new ImageIcon("bbbH.gif");
JLabel bbbl = new JLabel(bbb);
game.setLayout(new GridLayout(2, 2));
game.add(bbol);
game.add(bbgl);
game.add(bbgrl);
game.add(bbbl);
if (flashed == 1) {
bbbl.setIcon(bbbH);
} else {
bbbl.setIcon(bbb);
}
Let say if there are multiple labels like label1 ,label2 , label3etc. And you want to set them on the position of label bbo1. Then it can be done by getting the location of label bbo1 and setting it to all other labels.
For Example
label1.setLocation(bbo1.getLocation());
label2.setLocation(bbo1.getLocation());
label3.setLocation(bbo1.getLocation());
I already know that a JLabel can use an ImageIcon in the declaration of the JLabel, like this.
JLabel stick = new JLabel(new ImageIcon("stickPicture.gif"));
My question is, how to assign the picture to the JLabel after declaration. For example (this isn't actually the code that works, this is just the technique that I thought might have worked)
JLabel stick = new JLabel();
stick = new ImageIcon("stickPicture.gif"));
Kind of an odd question, just wondering if it can be done.
Use JLabel#setIcon(Icon).
JLabel stick = new JLabel();
stick.setIcon(new ImageIcon("stickPicture.gif"));
I am trying to align the bottom of 3 JLabels that contain an image. The 3 JLabels are held in one big JPanel.
I found a tutorial about GUI using Java Swing here. But for some reason if i apply the example code (that is given for buttons) it doesn't work on the JLabels or JPanel.
This is the example code from the Oracle website:
button1.setAlignmentY(Component.BOTTOM_ALIGNMENT);
button2.setAlignmentY(Component.BOTTOM_ALIGNMENT);
Any idea what went wrong? I could send my code, but I thought maybe that would make it too confusing for what might be a simple answer too an easy question for most of you out here.
Thanks in advance.
EDIT:
public class LayoutOef_01 extends JFrame{
JPanel paneel;
JLabel label1, label2, label3;
ImageIcon pic1, pic2, pic3;
Border panelBord, labelBord;
public Layout_01(String titel){
super(titel);
paneel = new JPanel();
pic1 = new ImageIcon("images/simon1.png");
pic2 = new ImageIcon("images/simon2.png");
pic3 = new ImageIcon("images/simon3.png");
label1 = new JLabel(pic1);
label2 = new JLabel(pic2);
label3 = new JLabel(pic3);
paneel.add(label1);
paneel.add(label2);
paneel.add(label3);
panelBoord = BorderFactory.createLineBorder(Color.WHITE, 30);
paneel.setBorder(panelBord);
paneel.setBackground(Color.WHITE);
labelBoord = BorderFactory.createLineBorder(Color.BLACK, 2);
label1.setBorder(labelBord);
label2.setBorder(labelBord);
label3.setBorder(labelBord);
this.getContentPane().add(paneel);
this.pack();
}
public static void main(String[] args) {
Layout_01 lay1 = new LayoutOef_01("Layout_01");
lay1.setVisible(true);
}
}
So i tried placing the following code -in different places- inside the code above, but nothing changes:
label1.setAlignmentY(Component.BOTTOM_ALIGNMENT);
label2.setAlignmentY(Component.BOTTOM_ALIGNMENT);
label3.setAlignmentY(Component.BOTTOM_ALIGNMENT);
Check this sample: http://www.java2s.com/Code/JavaAPI/java.awt/ComponentBOTTOMALIGNMENT.htm
Remember to:
- set the layout on the panel.
- set the alignment on the button
- add the button to the panel.