OPEN SOURCE

Jumat, 10 Februari 2012

Pertemuan 12

1. Contoh Program Database

import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class CobaDatabase {
public static void main (String [] args){
DBMahasiswa mahasiswa = new DBMahasiswa();
}
}

class DBMahasiswa extends JFrame implements ActionListener {
final JLabel lNim,lNama, lAlamat;
final JTextField nim, nama, alamat;
final JButton btnSave, btnHapus;
final JPanel panel1,panel2;

public DBMahasiswa() {
setTitle("Coba Database");
lNim = new JLabel("NIM : ");
lNama = new JLabel("NAMA : ");
lAlamat = new JLabel("ALAMAT : ");
nim = new JTextField(20);
nama = new JTextField(20);
alamat = new JTextField(20);
btnSave = new JButton("SIMPAN");
btnSave.addActionListener(this);
btnHapus = new JButton("HAPUS");
btnHapus.addActionListener(this);
panel1 = new JPanel(new GridLayout(3,1));
panel2 = new JPanel(new GridLayout(3,1));


setLayout(new BorderLayout());
add(panel1, "West");
panel1.add(lNim);
panel1.add(lNama);
panel1.add(lAlamat);
add(panel2, "East");
panel2.add(nim);
panel2.add(nama);
panel2.add(alamat);
add(btnSave, "South");
add(btnHapus, "North");
pack();

setDefaultCloseOperation(3);
setVisible(true);
}

public void actionPerformed(ActionEvent ae) {
if(ae.getSource()==btnSave) {
String sql = "insert into mahasiswa values('"+nim.getText()+"', '"+nama.getText()+"', '"+alamat.getText()+"')";
try {
//Untuk Database SQL
/*Class.forName("org.gjt.mm.mysql.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/praktikum", "root", "");*/
//Untuk Database ACCESS
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:mi90", "", "");
Statement stmt =con.createStatement();
stmt.executeUpdate(sql);
stmt.close();
con.close();
JOptionPane.showMessageDialog(this, "Data berhasil disimpan", "Hasil", JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage(), "Hasil", JOptionPane.ERROR_MESSAGE);
}
}
if(ae.getSource()==btnHapus) {
int pesan = JOptionPane.showConfirmDialog(null, "Anda yakin ingin menghapus pegawai\n"+
"dengan NIM = '"+nim.getText()+"'","Konfirmasi Hapus Data",

JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
if(pesan == JOptionPane.OK_OPTION) {
String sql = "DELETE from mahasiswa where nim='"+nim.getText()+"'";
try {
//Untuk database SQL
/*Class.forName("org.gjt.mm.mysql.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/praktikum", "root", "");*/
//Untuk database ACCESS
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con = DriverManager.getConnection("jdbc:odbc:mi90", "", "");
Statement stmt =con.createStatement();
stmt.executeUpdate(sql);
stmt.close();
con.close();
JOptionPane.showMessageDialog(this, "Data berhasil dihapus", "Hasil", JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage(), "Hasil", JOptionPane.ERROR_MESSAGE);
}
}
}
System.exit(0);
}
}




hasil program

 













































2. Program melihat Database


import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class LihatDatabase {
public static void main (String [] args){
LihatData data = new LihatData();
}
}


class LihatData extends JFrame{

public LihatData(){
setTitle("Lihat Database");

String tdata[] [] =new String[0] [0];
try{
//Untuk Database menggunakan SQL
/*Class.forName("org.gjt.mm.mysql.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/praktikum","root","");*/
//Untuk Database menggunakan ACCESS
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:odbc:mi90", "", "");
Statement stmt = conn.createStatement();
//Sintaks sql yang dibawa
String sql="select * from mahasiswa";
//Hasilnya ditampung disini
ResultSet rs = stmt.executeQuery(sql);
tdata = new String[500][3];
int p=0;
//Jika data berikutnya ada, maka...
while (rs.next()){
//Kita dapatkan data dari kolom 1
tdata[p] [0] = rs.getString(1);
tdata[p] [1] = rs.getString(2);
tdata[p] [2] = rs.getString(3);
p++;
}
//stmt dibebaskan dari memori
stmt.close();
//koneksi ditutp
conn.close();

}
catch (Exception exc){

JOptionPane.showMessageDialog(this,"ERROR","Hasil",JOptionPane.ERROR_MESSAGE);
}
String[] nkolom={"NIM","Nama","Alamat"};
JTable tabel = new JTable (tdata,nkolom);
JScrollPane gridtabel = new JScrollPane(tabel);
tabel.setPreferredScrollableViewportSize(new Dimension(400,300));
setLayout(new FlowLayout());
add(gridtabel);
pack();
setDefaultCloseOperation(3);
setVisible(true);
}
}


hasil program






















































Pertemuan 11

1. Contoh JFileChooser Extends JFrame


import javax.swing.*;
import javax.swing.filechooser.*;
import java.io.File;

public class ContohJFileChooser extends JFrame {
    public static void main(String[]args) {
        ContohJFileChooser apl = new ContohJFileChooser();
    }
    public ContohJFileChooser() {
        setSize(400, 150);

        JFileChooser jfc =
        new JFileChooser();

        FileFilter filterDotJava = new Akhiran(".java", "Berkas*.java");
        jfc.setFileFilter(filterDotJava);

        int hasil = jfc.showOpenDialog(this);
        if (hasil == JFileChooser.APPROVE_OPTION)
            setTitle("Tes JFileChooser. File: " + jfc.getSelectedFile().getName());
        else
            setTitle("Tes JFileChooser. Batal memilih");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
}

class Akhiran extends FileFilter {
    private String ekstensi;
    private String keterangan;

    public Akhiran(String suffix, String deskripsi) {
        this.ekstensi = suffix;
        this.keterangan = deskripsi;
    }

    public boolean accept(File f) {
        if (f.isDirectory())
            return true;

        return f.getName().endsWith(ekstensi);
    }

    public String getDescription() {
        return keterangan;
    }
}

hasil program :





















2. ContohJList

import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;

class cthList extends JFrame {
    JList daftar;
    JTextArea tArea1,tArea2;
    JPanel Panel1, Panel2, Panel3, Panel4;
    JSpinner Spin;
    String daftarBunga[] = { "Melati", "Mawar", "Krisan", "Kenanga", "Cempaka", "Tulip", "Kamboja Jepang" };
    public cthList() {
        super("Tes JList");
        setSize(350, 300);

        Spin = new JSpinner();
        Spin.setValue(new Integer(10));

        Panel1 = new JPanel();
        Panel2 = new JPanel();
        Panel3 = new JPanel();
        Panel4 = new JPanel();

        daftar = new JList(daftarBunga);
        daftar.setSelectedIndex(0);

        tArea1 = new JTextArea("",3, 15);
        tArea1.setEditable(false);
        tArea2 = new JTextArea("",3, 15);
        tArea2.setEditable(false);

        setLayout(new BorderLayout());

        add(Panel1, "North");
        add(Panel2, "West");
        add(Panel3, "East");
        add(Panel4, "South");
        Panel1.add(daftar);
        Panel2.add(tArea1);
        Panel3.add(tArea2);
        Panel4.add(Spin);

        setDefaultCloseOperation(3);
        setVisible(true);

        daftar.addListSelectionListener(new ListSelectionListener()
    {
        public void valueChanged(ListSelectionEvent e) {
            tArea1.setText("Yang terpilih:    \n"+daftar.getSelectedValue());
        }
    }
    );

    Spin.addChangeListener(new ChangeListener() {
        public void stateChanged(ChangeEvent e) {
            tArea2.setText("Nilai : \n" +Spin.getValue());
        }
    }
    );
}
}

public class ContohJList {
    public static void main(String[] args) {
        cthList apl = new cthList();
    }
}

Hasil Program





















3. ContohJProgressBar

import javax.swing.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class CthProgressBar extends JFrame implements ActionListener {
    public final static int SELANG_WAKTU_TUNGGU = 1;
    public static int MAKS =1000;

    JProgressBar pb;
    JButton b;
    Timer timer;
    int posisi = 0;

    public CthProgressBar() {
        super("Tes JProgressBar");
        setSize(300, 120);

        pb = new JProgressBar(0, MAKS);
        b = new JButton("Mulai");

        JPanel p = new JPanel();
        p.add(b);

        setLayout(new BorderLayout());
        add("North", p);
        add("South", pb);

        //-- Set Timer
        timer = new Timer(SELANG_WAKTU_TUNGGU, new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            posisi++;
            pb.setValue(posisi);
            if(posisi == MAKS) {
                timer.stop();
                b.setText("Mulai");
            }
        }
    });
    b.addActionListener(this);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    }
    public void actionPerformed(ActionEvent e) {
        String s = e.getActionCommand();
        if (s.equals("Mulai")) {
            b.setText("Stop");
            posisi = 0;
            pb.setValue(posisi);
            timer.start();
        }
        else {
            b.setText("Mulai");
            timer.stop();
        }
    }
}
public class ContohJProgressBar {
    public static void main(String[] args) {
        ContohJProgressBar apl = new ContohJProgressBar();
    }
}

4.  CthJTabbedPane extends JFrame


import javax.swing.*;

class CthJTabbedPane extends JFrame {
    JLabel lTab1, lTab2, lTab3, lTab4;
    JTabbedPane tab;

    public CthJTabbedPane() {
        super("Tes JTabbedPane");
        setSize(300, 120);

        tab = new JTabbedPane();

        JPanel panel1 = new JPanel();
        tab.addTab("Satu", panel1);
        tab.setSelectedIndex(0);
        lTab1 = new JLabel("Tab Satu");
        panel1.add(lTab1);

        JPanel panel2 = new JPanel();
        tab.addTab("Dua", panel2);
        lTab2 = new JLabel("Tab Dua");
        panel2.add(lTab2);


        JPanel panel3 = new JPanel();
        tab.addTab("Tiga", panel3);
        lTab3 = new JLabel("Tab Tiga");
        panel3.add(lTab3);

        JPanel panel4 = new JPanel();
        tab.addTab("Empat", panel4);
        lTab4 = new JLabel("Tab Empat");
        panel4.add(lTab4);

        add(tab);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
}

public class ContohJTabbedPane {
public static void main(String[] args) {
    CthJTabbedPane apl = new CthJTabbedPane();
    }
}

hasil program













5.  ContohJTable extends JFrame

import javax.swing.*;
import java.awt.*;
public class ContohJTable extends JFrame {
public static void main (String[] args) {
ContohJTable apl = new ContohJTable();
}

public ContohJTable() {
super("Tes JTable");
setSize(500,500);

Object[][] data = {
{"11234", "Asti Damayanti", "Akunting", new Long (2200000)},
{"11235", "Erittria", "Pemasaran", new Long(1200000)},
{"11236", "Satria", "Personalia", new Long(1200000)},
{"11237", "Tri Setyowati", "EDP", new Integer(1500000)},
{"11238", "Fahmi Basya", "Akunting", new Long(1250000)},
{"11239", "Suadi Marwan", "Akunting", new Long(1000000)},
{"11240", "Rani Ardiani", "Pemasaran", new Long(110000)},
{"11238", "Listya", "Pemasaran", new Long(110000)},
};
String[] judulKolom = {"NIP", "Nama", "Bagian", "Gaji"};
JTable table= new JTable (data, judulKolom);
JScrollPane scrollPane= new JScrollPane(table);
add(scrollPane);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}


hasil program











6. ContohJTree extends JFrame


import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;

public class ContohJTree extends JFrame {

public static void main (String [] args){
ContohJTree ap1 = new ContohJTree();
}

public ContohJTree(){
super ("Tes JTable");
setSize (400,400);

//---Buat Akar - Level 0
DefaultMutableTreeNode akar = new DefaultMutableTreeNode("Benua");

//---Level 1
DefaultMutableTreeNode afrika = new DefaultMutableTreeNode("Afrika");

DefaultMutableTreeNode asia = new DefaultMutableTreeNode("Asia");

DefaultMutableTreeNode amerika = new DefaultMutableTreeNode("Amerika");

DefaultMutableTreeNode eropa = new DefaultMutableTreeNode("Eropa");


//---Level 2
DefaultMutableTreeNode mesir = new DefaultMutableTreeNode("Mesir");

DefaultMutableTreeNode libya = new DefaultMutableTreeNode("Libya");

DefaultMutableTreeNode indonesia= new DefaultMutableTreeNode("Indonesia");

DefaultMutableTreeNode jepang = new DefaultMutableTreeNode("Jepang");

DefaultMutableTreeNode malaysia = new DefaultMutableTreeNode("Malaysia");

DefaultMutableTreeNode kanada= new DefaultMutableTreeNode("Kanada");

DefaultMutableTreeNode peru = new DefaultMutableTreeNode("Peru");

DefaultMutableTreeNode bolivia= new DefaultMutableTreeNode("Bolivia");

DefaultMutableTreeNode brazilia = new DefaultMutableTreeNode("Brazilia");

DefaultMutableTreeNode australia = new DefaultMutableTreeNode("australia");

DefaultMutableTreeNode jerman= new DefaultMutableTreeNode("Jerman");


//--- Membuat JTree
JTree struktur = new JTree (akar);
add(struktur);
akar.add(afrika);
akar.add(asia);
akar.add(amerika);
akar.add(eropa);
afrika.add(mesir);
afrika.add(libya);
asia.add(indonesia);
asia.add(jepang);
asia.add(malaysia);
amerika.add(kanada);
amerika.add(peru);
amerika.add(bolivia);
amerika.add(brazilia);
eropa.add(australia);
eropa.add(jerman);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// --- Tampilkan frame
setVisible(true);
}
}



hasil program

 





7.  cthIcon

import javax.swing.*;
import java.awt.*;
public class cthIcon {
    public static void main (String[] args) {
        JFrame myFrame = new JFrame("Contoh Program AWT");
        Icon ikon = new ImageIcon("1.GIF");
        JLabel myLabel = new JLabel("Selamat belajar JAVA", ikon,SwingConstants.CENTER);
        JButton myButton = new JButton("SELESAI", ikon);
        myButton.setToolTipText("ini adalah Button");
        myLabel.setToolTipText("ini adalah Label");
        myFrame.setLayout(new BorderLayout());
        myFrame.add(myLabel, "South");
        myFrame.add(myButton, "North");
        myFrame.setSize(500,500);
        myFrame.setVisible(true);
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

hasil program