So in this application I'm making, the user clicks on a button, and that button launches a program. This button will have the title of the application and the icon. I just need to know how to get the icon, kind of like this: Windows http://goo.gl/5WjdT
So what I want to know is this:
Is there any REAL way to do this in Java
If so, how would you do it?
Thanks in advance!
Do you have to get the icon associated to the exe file of the application ? If so it is possible in java, take a look at this tutorial, that explain several ways of extracting app icon from an executable binary file from a Java application.
Take a look at this code :
String s = "c:/windows/regedit.exe";
File file = new File(s);
// Get metadata and create an icon
sun.awt.shell.ShellFolder sf =
sun.awt.shell.ShellFolder.getShellFolder(file);
Icon icon = new ImageIcon(sf.getIcon(true));
You can simply use FileSystemView for that purpose:
public static void main(String[] args) {
Icon icon = FileSystemView.getFileSystemView()
.getSystemIcon(new File("C:\\Windows\\regedit.exe"));
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JLabel(icon));
frame.pack();
frame.setVisible(true);
}
Related
Well basically my image will not display, i'm almost certain it's my file path
(" C:\Users\Alex\Desktop\card.png" is what the image properties say is the file path but unless i put double slashes it confuses them for escape sequences. ) If someone has the answer it will be much appreciated. Here's my code:
import java.awt.*;
import javax.swing.*;
public class IDK extends JFrame {
public static void main(String args[]) {
new IDK();
}
public IDK(){
notsure();
}
public void notsure(){
setBounds(420,100,440,400);
setTitle("Frame");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
JLabel label1 = new JLabel("Tell me something");
JLabel image = new JLabel();
label1.setText("New Text");
JTextField text = new JTextField("Insert text",30);
image.setIcon(new ImageIcon("C:\\Users\\Alex\\Desktop\\card.jpg"));
panel.add(label1,image);
panel.add(text);
add(panel);
validate();
panel.setBackground(Color.CYAN);
setVisible(true);
}
}
There is no problem with using "\" so long as it's escaped, as you have done, if your prefer you can use / instead and on Windows the JRE will correct for it.
How ever, you should be adding the image separately, for example...
image.setIcon(new ImageIcon("C:\\Users\\Alex\\Desktop\\card.jpg"));
panel.add(label1);
panel.add(image);
The way you are doing it now assumes that image is a layout constraint for label1, which it isn't.
You should try and avoid using absolute paths and learn to use relative paths and/or embed the resources within the application context, this makes it easier to locate these resources at runtime.
Create a folder in your project and call it i.e. 'resources' and put your image in that folder. This will ensure that given image is on your classpath and can be easily accessed.
Then you can just do :
new ImageIcon("resources/card.jgp")
Problem with accessing outside resources is that special characters needs to be escaped and also if you export the project and give it to someone else, he/she will not have the image required by software ;)
This question already has answers here:
I took this code straight out of 'Java all in one for Dummies' ....why doesn't it work?
(2 answers)
Closed 8 years ago.
So, I do not know why my image is not showing because I put the image in the right folder and I believe that the code below is right. The result of this code is just a blank white frame background. I thought the image was corrupted but I tried a different image and its the same result. Anyway I will be highly grateful if anyone could solve this.
package fia_project;
import java.awt.Color;
java.awt.*;
import javax.swing.*;
public class Fia_test extends JFrame {
public Frame kenny;
public Fia_test(){
super("");
setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
setVisible(true);
setSize(350, 100);
Container pane = getContentPane();
getContentPane().setBackground(Color.WHITE);
ImageIcon icon = new ImageIcon("speaker.png");
JLabel label2 = new JLabel(icon);
add(label2);
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Fia_test t = new Fia_test();
}
}
Use ImageIO.read instead of ImageIcon as it will generate an error if the image can not be loaded, much more useful for diagnostics. See Reading/Loading an Image
ImageIcon(String) expects that the specified String represents a file on the file system. From the looks of you code, it is possible that you've placed the image within the source directory of your code.
Assuming that the image has been bundled into the resulting Jar file, you will no longer be able to access the image as if it was a file on the file system, but will need to use Class#getResource to load it...
For example...
BufferedImage img = ImageIO.read(getClass().getResource("speaker.png"));
ImageIcon icon = new ImageIcon(img);
Assuming that speaker.png is in the same directory as the class file.
Well I was wondering if I could make an icon image for a JFrame. I do know its posible, because, let me say, I am NOT digging the java logo.
Well if I just hava to use a Frame object I will.
Can someone tell me, I know its possible!
Use an ImageIcon.
ImageIcon icon = new ImageIcon( pathToIcon );
yourFrame.setIconImage(icon.getImage());
Good Luck!
First, you have to have an image file on your computer. It can be named anything. For this example, we will call this one "pic.jpg".
Next, you need to include it in the files that your application is using. For example, if you're using NetBeans, you simply click on "Files" in the left hand side of the IDE (not File as in the menu, mind you). Drag the picture's file over to the folder that houses the main package. This will include it for available use in the code.
Inside the method where you define the JFrame, you can create an image like this:
Image frameImage = new ImageIcon("pic.jpg").getImage();
You can now set it as the IconImage for the frame like this:
JFrame frame = new JFrame("Title");
frame.setIconImage(frameImage);
Hope that's helpful.
Note: the reason that the Image object has to be created like this is because Image is abstract and cannot be instantiated by saying new Image();
Props to you, btw, kid. I wish I would have started learning programming when I was your age. Keep at it!
You can do the following.
public Test {
public static void main(String[] args) {
JFrame frame = new JFrame("My Frame");
frame.setIconImage(new ImageIcon(Test.class.getResource("image.png"));
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.setVisible(true);
frame.setSize(100, 100);
//other stuffs....
}
}
I want to change the icon of the project instead of java icon. When the program icon is being displayed in status bar, it should be displaying the customized icon instead of default java icon.
I am using the following code. Please suggest me what's wrong in this code.
class newframe extends JFrame
{
Container cp;
newframe()
{
cp=this.getContentPane();
cp.setLayout(null);
}
public static void main(String args[])
{
newframe frm= new newframe();
frm.setbounds(0,0,1000,800);
frm.setVisible(true);
ImageIcon im1= new ImageIcon("path upto image");
frm.setIconImage(im1.getImage());
}
}
..new ImageIcon("path upto image");
A frame icon will typically be an embedded-resource, so must be accessed by URL rather than (a String representing a path to) a File.
There are a couple of things that would be keeping it from compiling. First:
frm.setbounds(0,0,1000,800);
Your "setbounds" should have a capital B. Typically, functions will be cased such that the first letter of the first word is lowercased, and subsequent words are upper-cased. See this link for the doc on setBounds: setBounds
There's a second issue in your ImageIcon path. Its hard to say if that came right from your code or if you removed the path for the sake of the example, but Andrew Thompson has addressed that adequately.
I think the problem is the decleration of the imageicon. What you should do is instead of getting the direct path, do something like this:
ImageIcon im1= new ImageIcon("Toolkit.getDefaultToolkit().
getImage(getClass().getResource("path upto image"))");
I do this with all of my applications, and it works every time.
New to NetBeans and just noticed that in the File >> Project Properties >> Application dialog there is a text field labeled Splash Screen that allows you to specify a path to an image that you would like displayed when your program is launching.
I want to customize the way my splash screen works (adding a progress bar, etc.) and would like to code it from the ground up but don't know where to start. What are the best practices for Java/Swing-based splash screens?
Thanks for any and all input!
The project properties -> Application -> Splash Screen allows you to add an image to an application. This property sets a value in the MANIFEST.MF called SplashScreen-Image: e.g. SplashScreen-Image: META-INF/GlassFish316x159.jpg This property will automatically cause the image to display as a splash screen. It does not work inside NetBeans, and must be run outside the IDE.
There is a tutorial Splash Screen Beginner Tutorial that details how to use it more detail. The tutorial was done for NetBeans 6.8, but will work on 7.2.1 which is the latest at the time of this post.
I'm not sure how NetBeans does it, but Splash Screens are supported by the JRE since version 6. See http://java.sun.com/developer/technicalArticles/J2SE/Desktop/javase6/splashscreen/
Splash screen is just a instance of java.awt.Window or undecorated javax.swing.JFrame.
To create window just say new Window(null), then set size and position (using tookit you can calculate where the screen center is) and then say window.setVisible(true)
Due to this is your own window you can do what you want: set layout, image, add process bar to the SOUTH etc.
You can also use JFrame: new JFrame().setUndecorated(true)`
There are a couple of ways to do this.
To do a simple splash screen (an image) you can specify this in the command line of you java application.
Here is a simple example
java -splash:<file name> <class name>
However, if you want a progress bar, you are going to have to do something a little more complicated, and write some code yourself. This is done in the following way.
Create a JWindow (or Window or undecorated JFrame) component with your splash screen elements
Set it to visible
Do the rest of your Swing GUI startup code
Set your JFrame to visible, then immediately follow with setting the JWindow to visible(false)
This should show the splash almost immediately, and then hide once the your application is fully loaded.
To see some splash screen code, take a look here. The implementation in the link only shows how to achieve what you can with the -splash command, but it will give you a good start to also include the progress bar that you requested.
I hope this helps you, it is a small example of how to create yourself a simple splash screen using a dummy Progress Bar:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class SplashScreen extends JWindow
{
private static JProgressBar progressBar = new JProgressBar();
private static SplashScreen execute;
private static int count;
private static Timer timer1;
public SplashScreen()
{
Container container = getContentPane();
container.setLayout(null);
JPanel panel = new JPanel();
panel.setBorder(new javax.swing.border.EtchedBorder());
panel.setBackground(new Color(255,255,255));
panel.setBounds(10,10,348,150);
panel.setLayout(null);
container.add(panel);
JLabel label = new JLabel("Hello World!");
label.setFont(new Font("Verdana",Font.BOLD,14));
label.setBounds(85,25,280,30);
panel.add(label);
progressBar.setMaximum(50);
progressBar.setBounds(55, 180, 250, 15);
container.add(progressBar);
loadProgressBar();
setSize(370,215);
setLocationRelativeTo(null);
setVisible(true);
}
public void loadProgressBar()
{
ActionListener al = new ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
count++;
progressBar.setValue(count);
if (count == 50){
timer1.stop();
execute.setVisible(false);
//load the rest of your application
}
}};
timer1 = new Timer(50, al);
timer1.start();
}
public static void main (String args[]){
execute = new SplashScreen();
}
}
Cheers!
Also consider to build your application on top of the NetBeans Platform (a Swing-based RCP). One of the many benefits: it comes with a customizable splash screen with progress bar.
Sample progress bar:
http://platform.netbeans.org/tutorials/nbm-paintapp.html#wrappingUp
Port a Swing application to the NetBeans Platform:
http://platform.netbeans.org/tutorials/60/nbm-porting-basic.html
Further links:
http://netbeans.org/features/platform/index.html
http://netbeans.org/features/platform/all-docs.html
If your application is build using NetBeans Platform, then here's a tutorial about splash screen customisation: http://wiki.netbeans.org/Splash_Screen_Beginner_Tutorial
There is a sample Javafx equivalent of Splash screen. However this splash screen is basically a java swing applet that is called from javafx to be displayed to the user and simulates more or less eclipse and netbeans splash screen using progress bar and titles for the loaded contents. This is the link.
You must be able to get the code and separate out the splash screen code written in java swings and use it for yourself.
This is a custom java swings splash screen. and hence to center the splash screen it uses the traditional
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension labelSize = l.getPreferredSize();
setLocation(screenSize.width / 2 - (labelSize.width / 2),
screenSize.height / 2 - (labelSize.height / 2));