Get Image/Icon associated with file extension in Java - java

How can I get the icon/image associated with an file extension in Java?

This should do the trick:
Icon ico = javax.swing.filechooser.FileSystemView.getFileSystemView().getSystemIcon( file )
To show it on a label, just add it to a JLabel:
myJpanel.add(new JLabel(ico));

Related

How can I specify the icon's path if icon doesn't have a specified address?

I have a frame and want to set an icon for it. I use this code : JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setLayout(null); f.setTitle("add icon example"); f.setBounds(200,200,200,200); Image icon = Toolkit.getDefaultToolkit().getImage("D:\\icon.png"); f.setIconImage(icon); f.setVisible(true); In this code , the address of image is specific , but what can I do if image and jar file are in a zip file and icon will be with jar file. I think I can write a code to unzip the file and then save the image in a specified address and then use it. But , please anyone help me to do it. Thanks.
The answer is something like
BufferedImage im = ImageIO.read(myClass.getResourceAsString("iconName"));
If you're trying to load an image that's within a JAR, this is the most simple approach to this problem. This will also work if your jar is executed from within a zip archive.
BufferedImage image = ImageIO.read(MyClass.class.getClassLoader().getResourceAsStream("image_name.png"));
Where MyClass is the name of the class you're calling this code from.

how to write tags,meta data to a file with android java(when we right click on windows>properties)

I want to add a comment to a file(video,txt,etc...). right click on a file on windows the comment should appear.
File tempf= new File (Environment.getExternalStorageDirectory().getAbsolutePath()+"/picedittemp");
tempf.setcommentOrTittle("mycomment to file");
Not all files contain a comment field so there is no standard way of doing it. Each file type however will have a library which will allow you to modify its tags. An mp3 for example has the JAudioTagger library which allows you to set tags like this.
//No guarantee this will work (just an example)
File track = new File("C:/path/to/song.mp3");
AudioFile f = AudioFileIO.read(track);
f.getTag().setField(FieldKey.COMMENT,"My Comment");

JasperViewer: How to set filename while saving report? [duplicate]

This question already has answers here:
JasperViewer - How to set file name and set the extension (format)?
(2 answers)
Closed 6 years ago.
I am creating Java desktop application. I am using DynamicReports API to create report.
The report was viewed in a JasperViewer. When I click save button, I want to display a name for report to save by default. How to set name for the report by default?
it is necessary to import the package com.jasperassistant.designer.viewer.ViewerComposite
so on that viewer composite you can see a save icon on you left top corner. you can provide any name and location where you want to save. i think that the good way of saving report through your jasper report.
Then i think that you have to change in JFileChooser instead of looking into API.
You can give default name to be displayed.
This way the approach would be:
JFileChooser jFileChooser = new JFileChooser();
jFileChooser.setSelectedFile(new File("fileToSave.txt"));
jFileChooser.showSaveDialog(parent);
And If you pass a File with an absolute path, JFileChooser will try to position itself in that directory (if it exists).
I got this source from following place

get path of image file that is outside the project in java

I use this code to display image that locates outside the my java project, but I got NullPointerException every time and I can only use images that are inside my project directory. why ?
Icon welcomeImg = new ImageIcon(getClass().getResource("D:/img/welcome.png"));
or
Icon welcomeImg = new ImageIcon(getClass().getResource("D://img/welcome.png"));
JLabel welcomingLb = new JLabel(welcomeImg);
You do not need to use the ClassLoader class to access your file since you are giving its full path.
Try instead :
Icon welcomeImg = new ImageIcon("D:/img/welcome.png");
Source : Javadoc of ImageIcon(String filename)
getResource expects the resource to be on the classpath.
If you want to read from a random file, use a File, or use the ImageIcon constructor that takes a file name.
See: Loading resources using getClass().getResource()
Basically when you use getResource it is expecting the file to be on the Classpath. There is also a constructor for ImageIcon that takes a String filename, so you can pass the path to the Icon file directly.
Check out the JavaDoc for ImageIcon
You can try using a File, like so:
File f = new File("C:\\folder\\stuff\\MyFile.class");

JFileChooser extension [duplicate]

This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
I need to display the file name without the extension in JFileChooser(open mode). How?
Can anyone tell me how switch off extensions in view openDialog ?
I want see only name file without his extension in this view.
thanks
you can use a custom writen javax.swing.filechooser.FileView that you set in your filechooser
The Customizing the File View section in the Java Tutorial includes an example of defining your own FileView object to control how the files are displayed in a JFileChooser.
http://download.oracle.com/javase/tutorial/uiswing/components/filechooser.html
Override the FileView.getName() method to display a customized name for each file.
Download the FileChooserDemo2 demo from this web page to try the example and change method ImageFileView.getName() to return the filename with the file extension removed.

Categories

Resources