Loading an ImageIcon onto an existing JLabel - java

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"));

Related

how to add the same JLabel to multiply JLabels in java?

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

how to Put JLabel text over a JTextField

how to add a JLabel simple text over/above a JTextField.
I tried many commands as you can see in my code but nothings works.
this is a snapshot of my code
private JPanel createTextPanel() {
int panelWidth = PANEL_SIZE.width;
int panelHeight = PANEL_SIZE.height/3;
Dimension panelSize = new Dimension(panelWidth,panelHeight);
JPanel textPanel = new JPanel();
textPanel.setPreferredSize(panelSize);
//textPanel.setLayout(null);
/* Add text */
JLabel Text_RED = new JLabel();
Text_RED.setText("Red");
//Text_RED = new JLabel("\nRED\n");
//Text_RED.setHorizontalTextPosition(SwingConstants.TOP);
//Text_RED.setVerticalAlignment(SwingConstants.TOP);
Red = new JTextField(3);
//Red.setVerticalAlignment(JTextField.TRAILING );
Red.setLocation(100,100);
//Red.setLocation(50, 50);
JLabel Text_Green = new JLabel("Green");
Green = new JTextField(3);
JLabel Text_Blue = new JLabel("Blue");
Blue = new JTextField(3);
//setLayout(new GridLayout(2,2,10,10));
textPanel.add(Text_RED);
textPanel.add(Red);
textPanel.add(Text_Green);
textPanel.add(Green);
textPanel.add(Text_Blue);
textPanel.add(Blue);
return textPanel;
}
Suggestions:
Don't use null layouts and absolute positioning. While it seems initially that using these tools is the easiest way to create complex GUI's, it's really a newbie fallacy, as the more you understand and use the layout managers, the more you'll find that they make the job of creating GUI's much easier, and the results much more attractive.
Learn about and use the layout managers. Tutorial link.
Consider using a BorderLayout and adding your JLabel BorderLayout.CENTER and the JTextField at BorderLayout.PAGE_END. As a side note, I generally avoid placing JTextFields in a BorderLayout.CENTER position since this will cause horizontal stretching of the field if the GUI changes size, which I don't think is aesthetically pleasing.

Adding a JLabel to a JPanel creates a NullPointerException

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.

Java - how to use methods

I have a list of JLabels that I am insterting into my JPanel:
avatarGridPanel.add(new JLabel(new ImageIcon("images/gui/orc_male.png", "Orc Male")));
avatarGridPanel.add(new JLabel(new ImageIcon("images/gui/human_male.png", "Human Male")));
avatarGridPanel.add(new JLabel(new ImageIcon("images/gui/tundrian_male.png", "Tundrian Male")));
avatarGridPanel.add(new JLabel(new ImageIcon("images/gui/brimlock_male.png", "Brimlock Male")));
I want to add tooltiptext to each of them. Is there any better way than to use a temp variable to hold one of their values and then keep re-using it?
JLabel temp = new JLabel();
temp = new JLabel(new ImageIcon("images/gui/human_male.png", "Human Male"));
temp.setToolTipText("Human Male");
avatarGridPanel.add(temp);
I tried doing something like this(below) but could not get it to work. Thanks for any help!
avatarGridPanel.add(new JLabel(new ImageIcon("images/gui/human_male.png", "Human Male")).setToolTipText("Human Male"));
You could create a function to create these for you. I do it sometimes when I have a big array and need to the same thing over and over:
private static JLabel makeLabel(String path, String name) {
JLabel label = new JLabel(new ImageIcon(path, name));
label.setToolTipText(name);
return label;
}
Then elsewhere in that class:
avatarGridPanel.add(makeLabel("images/gui/orc_male.png", "Orc Male"));
You can use a "temp" variable, but if you do you don't want to first create an empty JLabel and then right after that a new JLabel with icon and text.
But how about creating a helper method?
...
avatarGridPanel.add(createLabel("images/gui/human_male.png", "Human Male"));
...
private JLabel createLabel(String iconPath, String description) {
JLabel label = new JLabel(new ImageIcon(iconPath, description));
label.setToolTipText(description);
return label;
}
You could create a method where you pass in the image location, the text and the tooltip text to avoid all that code repetition.
This should work
private static JLabel createImageLabel(String imageLocation, String name, String toolTipText) {
JLabel temp = new JLabel(new ImageIcon(imageLocation, name));
temp.setToolTipText(toolTipText);
return temp;
}
There are ways to do that in one line but nothing really clean nor elegant.
Anyway, in most cases it isn't a good idea to create new instances of a class and not storing them in, at least, a local variable (even for a temporary usage).
So, the best thing to do here is to have this temp variable or delegate the code to something else (method/builder).

Best way to add a String in a JFrame

which is the best way to add formated string in jframe.
Previously I was trying to add jlabel
If you want to display some text in a window, yes, adding a JLabel to your JFrame is fine.
Just create an instance of the font you want and assign it to the JLabel using setFont.
Here is a code samle (taken from here):
Font font = new Font("Jokerman", Font.PLAIN, 35);
JLabel textLabel = new JLabel(textMessage);
textLabel.setFont(font);

Categories

Resources