I have the following package: projectname/src/main/java/net/is/lms/project/frames
In the frame folder there is a java class called testframe and an icon called icontest.png (it's 87x84px)
I have the following code snippet:
package net.is.lms.project.frames;
import javax.swing.*;
public class testframe {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(530, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
String url = "src/main/java/net/is/lms/project/frames/icontest.png";
ImageIcon icon = new ImageIcon(url);
JLabel label = new JLabel();
label.setIcon(icon);
frame.add(label);
}
}
When I create the frame, no icon gets displayed
I tried different paths and also adding it like that:
URL url = testframe.class.getResource("/net/is/lms/project/frames/icontest.png");
I don't have enough reputation to leave a comment.
Have you tried to put the images in the src/main/resources folder and loading them like in this example: link
Related
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.
I'm trying to add a .gif image to a JButton, but can't seem to get the image to load when i run the code. I've included a screenshot. Included is the frame that's created. I'd really appreciate any help that can be provided. Stack is telling me I can't enter images yet, so it created a link for it. I'm also going to enclose the actual code here:
package java21days;
import javax.swing.*;
import java.awt.*;
public class ButtonsIcons extends JFrame {
JButton load, save, subscribe, unsubscribe;
public ButtonsIcons() {
super("Icon Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
//Icons
ImageIcon loadIcon = new ImageIcon("load.gif");
ImageIcon saveIcon = new ImageIcon("save.gif");
ImageIcon subscribeIcon = new ImageIcon("subscribe.gif");
ImageIcon unsubscribeIcon = new ImageIcon("unsubscribe.gif");
//Buttons
load = new JButton("Load", loadIcon);
save = new JButton("Save", saveIcon);
subscribe = new JButton("Subscribe", subscribeIcon);
unsubscribe = new JButton("Unsubscribe", unsubscribeIcon);
//Buttons To Panel
panel.add(load);
panel.add(save);
panel.add(subscribe);
panel.add(unsubscribe);
//Panel To A Frame
add(panel);
pack();
setVisible(true);
} //end ButtonsIcon Constructor
public static void main(String[] arguments) {
ButtonsIcons ike = new ButtonsIcons();
}
} //end ButtonsIcon Class
enter image description here
The easiest way is.
Label or Jbutton and what ever else supports HTML 3.5
JLabel a = new JLabel("");
add that to your container.
Haven't figured out how to enter code sorry
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);
}
I'm trying to add the image "Pic.png" to this JLabel "label1" and display it on the JPanel "panel1" on a JFrame "window1". But it when I hit run it doesn't display my image. Anyone help? (I read about adding it to the source file or something but I'm not really sure what I'm doing because I'm new to Java. Will it not be able to access the picture without the image being in the source?)
public class UIForIshidaQuery {
public static void main(String[] args) {
System.out.println("Running...");
JFrame window1 = new JFrame();
window1.setVisible(true);
window1.setSize(1080, 720);
window1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = (JPanel) window1.getContentPane();
JLabel label1 = new JLabel();
panel1.setLayout(null);
ImageIcon image = new ImageIcon("C:\\Users\\BC03\\Pictures\\Saved Pictures\\Other\\Pic.png");
label1.setIcon(image);
label1.setBounds(500, 500, 500, 500);
panel1.add(label1);
}
}
The window should be set visible as the last call. Don't use null layouts1. This works.
import java.net.*;
import javax.swing.*;
public class UIForIshidaQuery {
public static String url = "http://i.stack.imgur.com/gJmeJ.png";
public static void main(String[] args) throws MalformedURLException {
System.out.println("Running...");
JFrame window1 = new JFrame();
window1.setSize(1080, 720);
window1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel1 = (JPanel) window1.getContentPane();
JLabel label1 = new JLabel();
//panel1.setLayout(null);
ImageIcon image = new ImageIcon(new URL(url));
label1.setIcon(image);
//label1.setBounds(500, 500, 500, 500);
panel1.add(label1);
window1.setVisible(true);
}
}
Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or combinations of them along with layout padding and borders for white space.
If you are using IntelliJ IDEA:
Right click your project root directory and select New > Directory;
Call the new directory 'resources' for example;
Right click the newly made directory and select Mark Directory As > Resources Root;
Add your image file in the directory;
Access your file properly:
CurrentClass.class.getClassLoader().getResource("pic.png").getFile();
The ImageIcon could be initialized like this:
File file = new File(CurrentClass.class.getClassLoader().getResource("pic.png").getFile());
BufferedImage image = null;
try {
image = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
ImageIcon imageIcon = new ImageIcon(image);
Hey there, i have just tried to put an image that is taken with JFileChooser on a label; but it did not work the way i want to. Here is the code that i tried;
import java.io.*;
import javax.swing.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
JFileChooser chooser = new JFileChooser();
JFrame frame = new JFrame("My Frame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
chooser.showOpenDialog(null);
File file = chooser.getSelectedFile();
ImageIcon icon = new ImageIcon(file.getName());
JLabel label = new JLabel(icon);
// JLabel label2 = new JLabel("try try catch it");
panel.add(label);
// panel.add(label2);
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
Any suggestion?
Close.
You will notice that when you look at file.getName() you see that it will give you the name of the file that you selected. You're looking for the path instead of the name of the file.
See if you can look in the API for File for how to get the path.
You should be using file.getPath() instead of file.getName(). You should also be doing your painting work in the EDT.