Created
March 27, 2012 16:54
-
-
Save resarahadian/2217883 to your computer and use it in GitHub Desktop.
Program JFileChooser Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* @ author Resa C.R | |
*/ | |
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.*; | |
import javax.swing.UIManager.*; | |
public class OpenFile extends JFrame | |
{ | |
Container konten = getContentPane(); | |
private JButton btnBuka = new JButton("Buka Gambar",new ImageIcon("src/Component/gambar/package.png")); | |
private JPanel panel = new JPanel(); | |
private JFileChooser fc = new JFileChooser(); | |
private JLabel lbl = new JLabel(); | |
private ImageHandler handler = new ImageHandler(); | |
//Konstruktor | |
public OpenFile() | |
{ | |
super("JFileChooser Java"); | |
setSize(1000,900); | |
setVisible(true); | |
setLocationRelativeTo(null); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
panel.add(lbl); | |
setLayout(new FlowLayout(FlowLayout.CENTER)); | |
konten.add(btnBuka,BorderLayout.NORTH); | |
konten.add(panel,BorderLayout.CENTER); | |
btnBuka.addActionListener(handler); | |
}//Akhir Konstruktor | |
//Inner class | |
private class ImageHandler implements ActionListener | |
{ | |
public void actionPerformed(ActionEvent act) | |
{ | |
int open = fc.showOpenDialog(panel); | |
if(open == JFileChooser.APPROVE_OPTION) | |
{ | |
String sumberGambar = fc.getSelectedFile().getPath(); | |
lbl.setIcon(new ImageIcon(sumberGambar)); | |
} | |
} | |
}//Akhir Inner class | |
public static void main(String[] ar) | |
{ | |
//Membuat Look and Feel dengan Java Nimbus | |
try{ | |
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"); | |
} | |
catch (UnsupportedLookAndFeelException e) | |
{ | |
} | |
catch (ClassNotFoundException e) | |
{ | |
} | |
catch (InstantiationException e) | |
{ | |
} | |
catch (IllegalAccessException e) | |
{ | |
} | |
new OpenFile(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment