remove image in Jlabel in java - java

I have a question and I know it may seem simple but I spent 3 hours and still I am having trouble:
I am trying to add and remove image in Jlabel in java dynamically I am trying this code but I cant see any image loding on label what is wrong with my code and what can I do else?
public static void main (String[] args)
{
ImageIcon icon = new ImageIcon ("1.gif");
JFrame frame = new JFrame ("Nested Panels");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
// Set up first subpanel
JPanel subPanel1 = new JPanel();
subPanel1.setPreferredSize (new Dimension(450, 100));
//subPanel1.setBackground (Color.green);
JLabel label1 ;
label1 = new JLabel ("Devil Left", icon, SwingConstants.CENTER);
label1.setHorizontalTextPosition (SwingConstants.LEFT);
label1.setVerticalTextPosition (SwingConstants.BOTTOM);
subPanel1.add (label1);
JPanel primary = new JPanel();
primary.setBackground (Color.blue);
primary.add (subPanel1);
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}

ImageIcon(String) assumes that the value is a File.
If the image is stored within the context of the Jar (or project if you're using NetBeans), then you will need to access the image via Java's resource management API, for example.
ImageIcon icon = new ImageIcon (YourProject.class.getResource("1.gif"));
If you're using Eclipse, the resource will need to be stored within the projects resource folder.

Related

ImageIcon in swing package of Java not functioning correctly

I am having issues with creating an custom imageIcon in Java using swing. The file-name works, but it appears that the ImageIcon isn't changing at all. Here is the code:
ImageIcon icon = new ImageIcon("Hnet.com-image.png");
JFrame frame = new JFrame("NumberPad");
frame.setIconImage(icon.getImage());
frame.setName("Pin Pad");
frame.setContentPane(new NumberPad().NumberPadPanel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
Anyone got suggestions for what I should do?
You should make a compilable example.
public class IconCheck{
public static void main(String[] args){
JFrame frame = new JFrame("icon test");
ImageIcon icon = new ImageIcon("Hnet.com-image.png");
JLabel label = new JLabel(icon);
frame.setIconImage( icon.getImage() );
frame.add(label);
frame.pack();
frame.setVisible(true);
}
}
Does that show your icon in the JFrame? If yes, then does it update your JFrame icon?
You probably want your png to be used as a resource.
ImageIcon icon = new ImageIcon( IconCheck.class.getResource("/sample.png") );
It looks up the path on the classpath, which intellij knows how to include.

When using a local file for a JComponent, is there a way to not hard code the file path?

I wanna upload my project, a card game to Github, and it has a picture from my hard drive as the game card table. Is there a way to compile/run and upload it without the code having the entire file path hard coded into it? As of now, its:
public static void main(String[] args){
JFrame frame = new JFrame();
JLabel panel = new JLabel(new ImageIcon("C:\\Users\\MyName\\Documents\\javaprojects\\Cardgame\\cardgameProject\\cardgameTableCanvas.jpg"));
frame.setSize(WIDTH,HEIGHT);
panel.setSize(WIDTH,HEIGHT);
frame.add(panel);
frame.setTitle("Test Canvas");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CardgameTable sampleTable = new CardgameTable();
frame.add(sampleTable);
frame.setVisible(true);
}
I also don't want issues if this is pulled from Git by someone else, and I think that file path could do it.
This really depends on where your java code is located! There are several tools that allow you to create a URL object of a local file path, and these can be fed into ImageIcon creation!
If the image is in the same directory (folder) as you java code, then the following should work to ensure the file is referenced even on other machines:
URL cardgameCanvas = new File("cardgameTableCanvas.jpg").toURI().toURL();
JLabel panel = new JLabel(new ImageIcon(cardgameCanvas));
(For java 7+: Paths.get("cardgameTableCanvas.jpg").toUri().toURL())
This should allow you to reference the image by first creating a URL object that links to it, and passing that URL object to the new ImageIcon object!
Hope this helps!
Additional Source
Edit
You can also just do
JLabel panel = new JLabel(new ImageIcon("cardgameTableCanvas.jpg"));
;)
Edit 2
Your main would look something like this (if you wanted to go the "hard way"):
public static void main(String[] args){
JFrame frame = new JFrame();
URL cardgameCanvas = new File("cardgameTableCanvas.jpg").toURI().toURL();
JLabel panel = new JLabel(new ImageIcon(cardgameCanvas));
frame.setSize(WIDTH,HEIGHT);
panel.setSize(WIDTH,HEIGHT);
frame.add(panel);
frame.setTitle("Test Canvas");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CardgameTable sampleTable = new CardgameTable();
frame.add(sampleTable);
frame.setVisible(true);
}
But since it works for you I would definetaly recommend just referencing the local document itself like this:
public static void main(String[] args){
JFrame frame = new JFrame();
JLabel panel = new JLabel(new ImageIcon("cardgameTableCanvas.jpg"));
frame.setSize(WIDTH,HEIGHT);
panel.setSize(WIDTH,HEIGHT);
frame.add(panel);
frame.setTitle("Test Canvas");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
CardgameTable sampleTable = new CardgameTable();
frame.add(sampleTable);
frame.setVisible(true);
}

Java SWING: How to fix size of components in GridLayout

I am creating a simple Window in GridLayout and here is my code:
package com.company.app;
import javax.swing.*;
import java.awt.*;
public class SpamGUI {
public static void main(String[] args) {
System.out.println("Loading Program..");
JFrame frame = new JFrame("My awesome Program");
frame.setVisible(true);
frame.setSize(800, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 3));
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
JLabel lblTrainPath = new JLabel("Enter Training Folder Path");
lblTrainPath.setSize(100,10);
panel.add(lblTrainPath);
JTextField txtTrainPath = new JTextField();
txtTrainPath.setSize(100,10);
panel.add(txtTrainPath);
JButton btnTrainPath = new JButton("Select");
panel.add(btnTrainPath);
JLabel lblTrainPath3 = new JLabel("Enter Training3 Folder Path");
panel.add(lblTrainPath3);
JTextField txtTestPath = new JTextField();
panel.add(txtTestPath);
JButton btnTestPath = new JButton("Select");
panel.add(btnTestPath);
frame.add(panel);
}
}
It generates screen like this:
As you can see,it is not respecting size of components, showing quite broad on screens. Also when I run program from INtelliJ IDEA, it does not render components unless I resize screen.
I also want a fixed size window having component with custom size.
Please guide
The Font class allows you to specify font size.
So, to create a font you should code like this:
Font f = new Font("serif", Font.PLAIN, fontSize);
The fontSize parameter will determine the size of the Font
So, here enter font size you want ..after that set that font instance variable f to your JLabel instance variable like this:
lblTrainPath.setFont(f);
and for having component with custom size.
you should try pack() instead of frame.setSize(800,200)
and use this pack() after adding all the components to your frame and for fixed size window use setResizable(false)...

java add image, Image is not showing up on Jpanel

I am having trouble getting an image to show up on my application. Can you see where I am going wrong? I just need the image to show up on the pane like everything else. The JPanel is named contentPane. Everything else shows up.
books = new ImageIcon("books.png");
imgLabel = new JLabel();
imgLabel.setIcon(books);
imgLabel.setBounds(300, 315, 203, 141);
contentPane.add(imgLabel);
I copied and added your code to a JFrame and it ran without a problem.
See
public static void main(String[] args) {
JFrame jFrame = new JFrame();
ImageIcon books = new ImageIcon("books.png");
JLabel imgLabel = new JLabel("test");
imgLabel.setIcon(books);
imgLabel.setBounds(300, 315, 203, 141);
jFrame.getContentPane().add(imgLabel);
jFrame.setVisible(true);
}
My suggestion would be to double check your books file path.

Java adding ImageIcon to JLabel

I am trying to make a very basic game with Java and I am having trouble displaying an image on a JFrame. It has worked in the past for me and now is not, i can't see what I did wrong.
I have tried printing the current working directory and changing where I get my image to match that. It is likely that the problem is not getting the image, since my (filefinder or filereader or something like that) can find it without problems, but I cannot correctly add it (the ImageIcon) to the JLabel, or that to the JFrame.
This is my code...
JFrame frame = new JFrame("no image");
ImageIcon image = new ImageIcon("C:/Documents and Settings/user/Desktop/hi/xD/JavaApplication2/image.png");
JLabel imagelabel = new JLabel(image);
frame.add(imagelabel);
The JFrame has been setVisible(true) and pack().
Could someone please help me understand what is wrong.
Your problem lies here:
ImageIcon image = new ImageIcon("C:/Documents and Settings/user/Desktop/hi/xD/JavaApplication2/image.png");
JLabel imagelabel = new JLabel(character);
You create an ImageIcon "image" but you create your JLabel with "character".
It should be:
JLabel imagelabel = new JLabel(image);
Try,
ImageIcon image = new ImageIcon("c:\\path\\image.png");
imagelabel = new JLabel(character, image, JLabel.CENTER);
frame.add(imagelabel);
Take a look at Tutorial - How to use Icons
import javax.awt.*;
import java.awt.*;
import java.awt.event*;
//class name image
class image {
image()
//constructor {
Frame f=new Frame("Image");
//Frame
f.setSize(500,500);
f.setVisible(true);
Panel p =new Panel();
//Panel
f.add(p);
p.addLayout(null);
ImageIcon ii=new ImageIcon("set your image path");
//ImageIcon is used to image Display .
Label l =new Label(ii);
p.add(ii);
p.setBounds(set you bounds);
//Like that(20,20,500,40);
}
public static void main(String [] args) {
image obj = new
}
}

Categories