Membuat Aplikasi Search Dengan Java


/*
* Program : Searching File
* Author : Eko Kurniawan Khannedy
* Contact : echo_khannedy@yahoo.co.id
*/public class FindFiles extends javax.swing.JFrame{

private javax.swing.JFileChooser browse;
private javax.swing.JButton browse_btn, search_btn, reset_btn;
private javax.swing.JTextField browse_text, search_text;
private javax.swing.JTextArea area_search;
private javax.swing.JScrollPane pane_area;
private PanelFind panel;
private java.util.concurrent.ExecutorService tread;

private java.io.File dir;
private String keyword;
private RunFind findNow;

public FindFiles(){
super(“FindFiles”);

browse = new javax.swing.JFileChooser(“.”);
browse.setAcceptAllFileFilterUsed(false);
browse.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);

browse_btn = new javax.swing.JButton(“Browse”);
browse_btn.addActionListener(new Aksi() );
search_btn = new javax.swing.JButton(“Search”);
search_btn.addActionListener(new Aksi() );
reset_btn = new javax.swing.JButton(“Reset”);
reset_btn.addActionListener(new Aksi() );

browse_text = new javax.swing.JTextField();
browse_text.setForeground(java.awt.Color.BLACK);
browse_text.setEnabled(false);
search_text = new javax.swing.JTextField();

area_search = new javax.swing.JTextArea();
area_search.setEditable(false);
pane_area = new javax.swing.JScrollPane(area_search);
tread = java.util.concurrent.Executors.newCachedThreadPool();

panel = new PanelFind(this);
add(panel);

setLocation(200,200);
pack();

setResizable(false);
setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
}

public void browse(){
int valid = browse.showOpenDialog(this);
if(valid == javax.swing.JFileChooser.APPROVE_OPTION){
dir = browse.getSelectedFile();
browse_text.setText(dir.getPath());
}else{
return ;
}
}

public void search(){
keyword = search_text.getText();
area_search.setText(“”);
if(keyword.length()<1){
return ;
}else if(dir == null){
return ;
}else{
findNow = new RunFind(this);
java.util.concurrent.ExecutorService ex = java.util.concurrent.Executors.newCachedThreadPool();
ex.submit(findNow);
ex.shutdown();
}
}

public void reset(){
keyword = “”;
dir = null;
search_text.setText(“”);
browse_text.setText(“”);
area_search.setText(“”);
}

public static void main (String[] args) {
FindFiles baru = new FindFiles();
baru.setVisible(true);
}

private class Aksi implements java.awt.event.ActionListener{

public void actionPerformed(java.awt.event.ActionEvent event){
String command = event.getActionCommand();
if(command.equals(“Browse”)){
browse();
}else if(command.equals(“Reset”)){
reset();
}else if(command.equals(“Search”)){
search();
}
}

}

private class PanelFind extends javax.swing.JPanel{

private javax.swing.JFrame parent;
private java.awt.GridBagLayout layout;
private java.awt.GridBagConstraints constrait;

public PanelFind(javax.swing.JFrame parent){

this.parent = parent;
layout = new java.awt.GridBagLayout();
constrait = new java.awt.GridBagConstraints();
constrait.insets = new java.awt.Insets(5,5,5,5);
constrait.fill = java.awt.GridBagConstraints.BOTH;

setLayout(layout);
addComp(browse_text,0,0,2,1,0,0);
addComp(browse_btn,2,0,1,1,0,0);
addComp(search_text,0,1,1,1,200,0);
addComp(reset_btn,1,1,1,1,0,0);
addComp(search_btn,2,1,1,1,0,0);
addComp(pane_area,0,2,3,1,0,400);

}

public void addComp(javax.swing.JComponent comp, int kolom, int baris, int lebar, int tinggi, int lebarmanual, int tinggimanual){
constrait.gridx = kolom;
constrait.gridy = baris;
constrait.gridwidth = lebar;
constrait.gridheight = tinggi;
constrait.ipadx = lebarmanual;
constrait.ipady = tinggimanual;
layout.setConstraints(comp, constrait);
add(comp);
}

}

private class RunFind implements Runnable{

private String key;
private int count;
private FindFiles parent;

public RunFind(FindFiles parent){
this.key = keyword.startsWith(“.”) ? keyword : “.”+keyword;
this.count = 0;
this.parent = parent;
}

public synchronized void AddFiletoArea(java.io.File folder){
if(folder.isDirectory()){
java.io.File[] files = folder.listFiles();
for(java.io.File i : files){
AddFiletoArea(i);
try{
Thread.sleep(1);
}catch(InterruptedException e){
}
}
}else{
if(folder.getName().toUpperCase().endsWith(key.toUpperCase())){
area_search.append(folder.getPath()+”n”);
count += 1;
}
}
}

public void run(){
parent.setEnabled(false);
java.io.File[] files = dir.listFiles();
for(java.io.File i : files){
AddFiletoArea(i);
try{
Thread.sleep(1);
}catch(InterruptedException e){
}
}
area_search.append(“—-FINISH—-n”);
area_search.append(“There Are “+count+” Files Found”);
parent.setEnabled(true);
}
}

}

About these ads

29 pemikiran pada “Membuat Aplikasi Search Dengan Java

    • mas aQ ne msh awam banget tp pengen ngerti ttg java n bwt app java kayak mas,
      misalnya aQ tar TA aq minta bantuan m,as yaw….
      ok???
      aQ mw bwt app tentang cara mentransfer apapun lwt device bluetooth ok mas…
      tu dulu selebiihnya tlg tar aq d bantu yah…
      bls lwt e-mail aQ yah mass…

  1. numpang nanya mas, saya guru kimia, sy lagi mempelajari netbeans mobile untuk membuat aplikasi pembelajaran di hp. mohon bantuan untuk tutorial search n find text atau cara bikin kamus dengan netbean 6.5. tolong kirim lewat email. terima kasih

  2. mas echo…………………..
    he he he….. kayaknya ucapan makasih aja ga ckup Q ucapi buat mas.
    lantaran tulisan-tulisan mas usaha Q untuk belajar java menjadi jauh lebih ringa
    He he he …
    mas q masih binggung dengan speech recongnitionya java. error terus waktu Q coba.
    bisa tolong bantuin g,,,,,,,,,,
    pli………………….s
    Q.cubee@gmail.com
    makasih

  3. mantaaaaaaaaaap,,mau bikin OS mini ntar pas TA…cocok buat nambah referensi..

    boleh ya mas?

    ijin bookmark..makasih berat!!!!

  4. saya guru kimia, ingin coba buat aplikasi kamus kimia. saya dah coba cari2 n yg paling mudah pake netbeans.
    tolong tutorial aplikasi kamus sederhana dengan find text

  5. aQ bNun Nech mw bUat apLiksi Ap bwt PI aq????
    mCh bNun?????????

    kra2 java ssh nda C yaH????

    pLizzz bantu dOnk????

  6. kasih contoh “search” yang bisa mencari suatu field dalam table dan ditampilin di tableModel.
    itu baru serruuu.

  7. Wah…mantap…
    Oh yach….itu program buat pake apa….apa IDE yg visual spt Netbeans,Eclipse atau sejenis Jcreator / notepad….

  8. Kang echo… minta bantuannya donk… ada fb gak??

    klo misalnya kita ngetik di jText. misal kita masukkan huruf “Y”. trus biru2 di jTable langsg ngarah ke nm yang depannya “Y”. seterusnya sampe kita ketik slese namanya.

    tu gmn crnya kang??? help me please…

  9. asslmlkm.wr.wb

    mas Ecko.mau nanya gimana cara nampilin gambar n cara nyimpen file gambar di database mysql.thanks atas jawabanya

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Logout / Ubah )

Twitter picture

You are commenting using your Twitter account. Logout / Ubah )

Facebook photo

You are commenting using your Facebook account. Logout / Ubah )

Google+ photo

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s