Skip to content

Instantly share code, notes, and snippets.

@resarahadian
Created March 7, 2013 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save resarahadian/5106220 to your computer and use it in GitHub Desktop.
Save resarahadian/5106220 to your computer and use it in GitHub Desktop.
Icon File FileChooser
import java.io.File;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.filechooser.FileView;
public class FileIcon extends FileView
{
ImageIcon imgFolder = new ImageIcon("src/Gambar/Icon/Folder.png");
ImageIcon imgOffice = new ImageIcon("src/Gambar/Icon/OpenOffice.png");
ImageIcon imgPDF = new ImageIcon("src/Gambar/Icon/pdf.png");
ImageIcon imgPNG = new ImageIcon("src/Gambar/Icon/png.png");
ImageIcon imgZip = new ImageIcon("src/Gambar/Icon/zip.png");
ImageIcon imgFile = new ImageIcon("src/Gambar/Icon/File.png");
public Icon getIcon(File file)
{
Icon i = null;
if(file.isDirectory())
{
i = imgFolder;
}
else
{
String eks = getExtension(file);
if(eks!=null)
{
if(eks.equals("ODT") || eks.equals("ODS"))
{
i = imgOffice; //Memberikan Icon file format Office
}
else if(eks.equals("PDF"))
{
i = imgPDF; //Memberikan Icon file format PDF
}
else if(eks.equals("PNG"))
{
i = imgPNG; //Memberikan Icon file format PNG/Image
}
else if(eks.equals("ZIP"))
{
i = imgZip; //Memberikan Icon file format Zip/Archieve
}
else
{
i = imgFile; //Memberikan Icon file format lainnya
}
}
}
return i;
}
public static String getExtension(File file)
{
String ekstensi = null;
String text = file.getName();
int a = text.lastIndexOf('.');
if(a>0 && a<text.length() -1)
{
ekstensi = text.substring(a+1).toUpperCase();
}
return ekstensi;
}
}
import java.awt.EventQueue;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
@SuppressWarnings("serial")
public class FrameFileIcon extends JFrame
{
private JPanel contentPane;
private JButton btnBukaFile;
private JLabel lblGambar;
private JFileChooser fc = new JFileChooser();
/**
* Create the frame.
*/
public FrameFileIcon()
{
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 659, 564);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
lblGambar = new JLabel("");
lblGambar.setBounds(12, 12, 633, 465);
contentPane.add(lblGambar);
btnBukaFile = new JButton("Buka Gambar");
btnBukaFile.setIcon(new ImageIcon("/home/resa/Aplikasi Java/SwingComponents/src/Gambar/tampilData.png"));
fc.setFileView(new FileIcon());
btnBukaFile.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent act)
{
int buka = fc.showOpenDialog(lblGambar);
if(buka == JFileChooser.APPROVE_OPTION)
{
String src = fc.getSelectedFile().getPath();
lblGambar.setIcon(new ImageIcon(src));
}
}
});
btnBukaFile.setBounds(246, 489, 179, 36);
contentPane.add(btnBukaFile);
setLocationRelativeTo(null);
}
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
FrameFileIcon frame = new FrameFileIcon();
frame.setVisible(true);
}
catch (UnsupportedLookAndFeelException e) {
}
catch (ClassNotFoundException e){
}
catch (InstantiationException e) {
}
catch (IllegalAccessException e) {
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment