Saturday, 28 November 2009

JfileChooser

Mula-mula kita buat form seperti di bawah ini :



Kita Tambahkan Event Jbuttonnya, sebagai berikut :

private void bbrowseActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser file = new JFileChooser();
file.setAcceptAllFileFilterUsed(true);
file.setFileFilter(new FileNameExtensionFilter("Gambar", "jpg"));
file.showOpenDialog(this);
}

kode di atas artinya ketika kita mengklik tombol browse maka akan menampilkan jfilechooser, jfilechooser hanya untuk menampilkan gambar dengan extensi jpg.

Berikut Source Kode Langkapnya :
public class postingan extends javax.swing.JFrame {

/** Creates new form postingan */
public postingan() {
initComponents();
}

@SuppressWarnings("unchecked")
//
private void initComponents() {

bbrowse = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Demo JfileChooser");
setLocationByPlatform(true);
getContentPane().setLayout(null);

bbrowse.setText("Browse");
bbrowse.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bbrowseActionPerformed(evt);
}
});
getContentPane().add(bbrowse);
bbrowse.setBounds(140, 100, 110, 23);

java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-416)/2, (screenSize.height-272)/2, 416, 272);
}//


private void bbrowseActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser file = new JFileChooser();
file.setAcceptAllFileFilterUsed(true);
file.setFileFilter(new FileNameExtensionFilter("Gambar", "jpg"));
file.showOpenDialog(this);
}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new postingan().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.JButton bbrowse;
// End of variables declaration

}

Hasilnya :

No comments:

Post a Comment