Jdialog box Title bar icon change - java

I need to change the Jdialog box title bar icon. By default it uses a java coffee image.
I have searched in internet and used many codes
1. Image im = Toolkit.getDefaultToolkit().getImage("/org/qmon/generate/Images/JDialog -2.ico");
dialog.setIconImage(im);
2. Toolkit kit = Toolkit.getDefaultToolkit ();
Image img = kit.getImage ("/org/qmon/generate/Images/Create File Tag-16x16.png");
dialog.setIconImage(img);
nothing works properly.. Kindly help me.. Thanks in Advance

Firtsly, ico is not a support image format for Java.
The likely reason you're having issues with the second approach is that getImage is expecting a file reference and the image you seem to referencing looks like it's embedded (stored within your application)
Try using something more like...
Image img = kit.getImage (getClass().getResource("/org/qmon/generate/Images/Create File Tag-16x16.png"));
Instead.
Personally, I prefer ImageIO.read as it throws a IOException when something goes wrong...
Image img = ImageIO.read(getClass().getResource("/org/qmon/generate/Images/Create File Tag-16x16.png"));
But that's me...
You should also consider taking a look at Convert List<BufferedImage> to Image which demonstrates the use of ico file (from a 3rd party API) and setIconImages method

Image image = ImageIO.read(new URL(
"http://www.gravatar.com/avatar/f1d58f7932b6ae8027c4e1d84f440ffe?s=128&d=identicon&r=PG"));
dialog.setIconImage( image );
dialog.setVisible(true);

I am using this in my application and working fine
java.net.URL url = ClassLoader.getSystemResource("res/java.png");
ImageIcon icon = new ImageIcon(url);
JOptionPane.showMessageDialog(null, jep, "UroSync",JOptionPane.INFORMATION_MESSAGE, icon);

To improve what MadProgrammer has said, I met the problem and I solved it instantiating a JDialog but using the static class Toolkit method getDefaultToolkit().getImage(Image img).
JDialog dialog = new JDialog();
dialog.setIconImage(Toolkit.getDefaultToolkit().getImage(MyMainClass.class.getResource("/myIcon.png")));
To do that you need to add before the image into the build path of the Project.

Related

How to set an icon image for an application?

When I start my application, the icon on the desktop looks like this:
I want to know if there is a way I can change that image to an image of my choosing?
frame.setIconImage(new ImageIcon(getClass().getResource("logo.png")).getImage()
This works when the image is in the same package as the class.
Note: It is better to have images in a resource folder
Give this a try.
frame.setIconImage(new ImageIO.read(new File("res/game.png")));
where res/game.png is the icon image you want to use
https://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html#ImageIcon(java.lang.String)
https://docs.oracle.com/javase/tutorial/uiswing/components/frame.html

Creating an Image Icon from a local file for eclipse

So I am trying to add an image to a JLabel object which is added to a JPanel called "topPanel" which is part of a JFrame called
"primaryWindow". I have already declared the "topPanel" and "primaryWindow". I found on other forms that you have to create a separate source folder and add the image file in that folder in order to access it and I did so.
However, when I execute the following, the image does not appear to be on the Label. I know that it has nothing to do with adding the JLabel to the panel properly because when I enter a String into the JLabel constructor, the String appears on the panel. An image however does not appear. Am I properly adding the image? I am using a mac if this helps.
private JLabel image = new JLabel();
image.setIcon(new ImageIcon("Check.png"));
topPanel.add(image);
primaryWindow.add(topPanel, BorderLayout.NORTH);
You can also use BufferedImage
BufferedImage myPicture = ImageIO.read(new File("C:\\xx\\xxx\\Check.png.jpg"));
Image scaled = myPicture.getScaledInstance(100,70,Image.SCALE_SMOOTH);
image = new JLabel(new ImageIcon(scaled));
topPanel.add(image);
primaryWindow.add(topPanel, BorderLayout.NORTH);
Note that "C:\\xx\\xxx\\Check.png.jpg" is the path where you save Check.png.
Hope this helped.
The ImageIcon that you passed into the setIcon method of the JLabel could be null. Have you tried to check if it is null before calling setIcon? If you create a "res" resource folder in the root directory of your project, you could try the following:
image.setIcon(new ImageIcon(ImageIO.read(new File("res/Check.png"))));
Additionally, if you are using Eclipse, you should try and refresh the project directory by right clicking and pressing refresh; sometimes Eclipse doesn't register files added.
Lastly, try setting the background of the JPanel to a certain color to see if it is displaying it and make sure its width and height are not 0 (it is possible that the layout you are using changed its size).

Custom ToolBar with Java Swing for Desktop

I have created a GUI with Java Swing and wanting to create a custom toolbar according to my modules. Below are the images am wanting to use:
These images are placed in the same level as the src folder within my application. I am aware that I can perhaps create a jar with these images so that I can easily access them from within my application but do not know how. I have spent hours trying to make this work.
Below is my GUI that I have created ad wanting to beautify with these images for the toolbar else create an array of labels that will act as a navigation but either approach I couldn't get it to work.
The code below was my last attempt on this:
JToolBar toolbar1 = new JToolBar();
ImageIcon client = new ImageIcon("clients.png");
ImageIcon timesheet = new ImageIcon("timesheets.png");
JButton clientTB = new JButton(client);
JButton timesheetTB = new JButton(timesheet);
toolbar1.add(clientTB );
toolbar1.add(timesheetTB);
add(toolbar1, BorderLayout.NORTH);
I even moved these images and placed them within the class that's calling them.
What could I be doing wrong, please help?
You have a look at the JavaDocs for ImageIcon(String), the String value is "a String specifying a filename or path"
This is a problem, because your images aren't actually files, any more, they have been embedded within your application (typically within the resulting jar file) and no longer be treated like "normal files".
Instead, you need to use Class#getResource which searches the application's classpath for the named resource, something like...
// This assumes that the images are in the default package
// (or the root of the src directory)
ImageIcon client = new ImageIcon(getClass().getResource("/clients.png"));
Now, I have a personal dislike for ImageIcon, because it won't tell you when the image is loaded for some reason, like it can't be found or it's the wrong format.
Instead, I'd use ImageIO to read the image
ImageIcon client = new ImageIcon(ImageIO.read(getClass().getResource("/clients.png")));
which will do two things, first, it will throw a IOException if the image can't be loaded for some reason and two, it won't return until the image is fully loaded, which is helpful.
See Reading/Loading an Image for more details

How to change java icon in a JFrame

Ok so I've been researching this one quiet a bit. I am fairly new to java but thought that this one would be easy. Ive tried just about every way that has been answered on this site and still no luck, and usually when I look here I am able to find a answer that fits what I am looking for. Does anyone know how to change the Java icon in the top corner of the JFrame. I'm pretty positive that its not my file path either because all my images are in the same folder and they all work, this is the only one that I can't seem to get to work.
This is the first part my code for the main menu of my program, everything works except when i try to add the icon image. The code I've entered below does not have anything in it for the JFrame IconImage, I removed it since it didn't work. So if there is someone who knows how to get it working with this code that would be highly appreciated, thank you very much in advanced!
public class MainFrame
{
private MyPanel main;
private MyPanel2 create;
private MyPanel3 update;
private MyPanel4 find;
JFrame frame = new JFrame("Main Menu:");
public void displayGUI()
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel contentPane = new JPanel();
contentPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
contentPane.setLayout(new CardLayout());
main = new MyPanel(contentPane, this);
create = new MyPanel2(contentPane);
update = new MyPanel3(contentPane);
find = new MyPanel4(contentPane);
contentPane.add(main, "Main Menu");
contentPane.add(create, "Create Part");
contentPane.add(update, "Update Part");
contentPane.add(find, "Find Part");
frame.setLocation(200, 200);
frame.setSize(700, 580);
frame.setContentPane(contentPane);
frame.setVisible(true);
}
I have an answer for you. First, make sure that the images are in a folder, not a package. Next, insert this line of code:
Image image = Toolkit.getDefaultToolkit().getImage(getClass().getResource("path/to/image.png"));
ImageIcon icon = new ImageIcon( );
setIconImage(icon.getImage());
This code gets the image from the class path, and returns it as a image icon, and then it sets it. This should add the image icon to the application. If it doesn't, then tell me.
EDIT: After you told me that that didn't work then I decided to take a second crack at it...
First, put your images into a completely separate folder. I usually call this /res. Next, put your image in there. Now, for loading I took a completely different route. I decided to use ImageIO instead of default loading. To load the image, you use this code:
try {
frame.setIconImage(ImageIO.read(new File("res/icon.png")));
}
catch (IOException exc) {
exc.printStackTrace();
}
ImageIO works a lot better for loading images. If this still doesn't work then please tell me.
If you want to export this as a JAR then put a folder the same name as you used in the program in the same directory as the JAR.
For example in a NetBeans project, create a resources folder in the src folder.
Put your images (jpg, ...) in there.
Whether you use ImageIO or Toolkit (including getResource),
you must include a leading / in your path to the image file:
Image image = Toolkit.getDefaultToolkit().getImage(getClass().getResource("/resources/agfa_icon.jpg"));
setIconImage(image);
If this code is inside your JFrame class, the image is added to the frame as an icon in your title bar.
This works pretty fine for me.
Just add this after you've created your JFrame.
try {
Image image = new ImageIcon("/icons/image.jpg").getImage();
frame.setIconImage(image);
}catch(Exception e){
System.out.println("Application icon not found");
}
Paste your image icon (fav.png) in the same package first,
Write following code in constructor of JFrame:
setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("fav.png")));
Note:- fav.png is the name of icon
this.setIconImage(new ImageIcon(getClass().getResource("/iconsfolder/iconsname.jpg")).getImage());
// sets the Global icon for the system
try this code put after this code:
public void displayGUI()
{

Unable to get image icon in runnable jar

I wrote a method in order to get icon for my swing:
public Icon getIcon(String iconName) {
Icon icon = null;
if(iconName.equals("NEXT")){
icon = new ImageIcon( getClass().getResource("resources/img/next.png" ) );
}
return icon;
}
but
icon = new ImageIcon( getClass().getResource("resources/img/next.png" ) );
goes in null pointer
I created a source folder "resources" and a folder "img" inside it with "next.png" icon
Where's the problem?
Thanks
For this to work, the resources folder should be in the same folder as the folder corresponding to the package of this.getClass(). To start from the root of the classpath, use getClass().getResource("/resources/img/next.png"). (with a leading /)
so, I found the right method:
public static ImageIcon getImageIcon(String iconName) {
ImageIcon imageIcon = null;
if(iconName.equals("DOWNLOAD")){
imageIcon = new ImageIcon(ImagesLocation.class.getResource("/img/download.png"));
}
return imageIcon;
}
with a "resources" source folder at the same level of the project and with an img folder inside (package styled)
ImagesLocation is a generic class containing this method
For those in need of help that have come across this page in Google - I wrote an answer in another StackOverflow question giving the best way to handle images in JAVA apps so that you can easily access the images for all image method types in Java:
This IS the best way to handle all images and icons in a JAR App.
Once you've zipped up all of your images and icons into its own JAR file - Configure your build path by adding the images JAR file into your libraries tab so that its now included in your classpath.
Then simply use the following 3x lines of code at the start of your constuctor to access any image you need for anything including a SystemTray image which doesn't accept the simple ImageIcon's as its main icon (weird I know). The 3x lines are:
URL iconUrl = this.getClass().getResource("/image-iconb.png");
Toolkit tk = this.getToolkit();
someimgicon = tk.getImage(iconUrl);
(someimgicon is just a constructor declared Image variable)
Now you can set a window icon as simply as:
setIconImage(someimgicon);
and at the same time use the same variable when setting the System TrayIcon by declaring:
trayIcon = new TrayIcon(someimgicon, "SystemTray Demo", popupMenu);
The above allows you to declare Images or ImageIcons easily and centrally without running the risk of not keeping image resources in the right place. It keeps it nice and tidy, with the JAR containing all your images automatically compiled at run time and distribution of your program.
As a bonus, once the JAR is registered in your classpath - you can keep adding any other images into the same JAR at any time without any fuss too - Everything just works and the added images are instantly available to your app.
Much better in my view.

Categories