Selasa, 17 Januari 2012

Pertemuan 12 KOMPONEN-KOMPONEN SWING LANJUTAN


LAPORAN PERTEMUAN 12

KOMPONEN-KOMPONEN SWING LANJUTAN


A. Membuat program contoh dari JLable dan JButton menggunakan icon dan Tool tip

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 ("a.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);

  }
}




B. Membuat program menggunakan JFileChooser

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

public class ContohJFileChooser extends JFrame{
public static void main (String[]args){
ContohJFileChooser ap1 = 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;
}
}

Outputnya :

Penjelasan Program :




C. Menggunakan program dengan menggunakan JTextArea, JList dan JSpinner

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, "Earth");
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 ();
}
}



D. Membuat program dengan menggunakan JProgressBar

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

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

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

public CthJProgressBar () {
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 actonPerformed (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) {
CthJProgressBar apl = new CthJProgressBar ();
}
}




E. Membuat program dengan menggunakan JTabbedPane

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",panel2);
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 ap1 = new CthJTabbedPane();
}
}

Outputnya :
Penjelasan Program :

F. Membuat program dengan menggunakan JTable

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

public class ContohJTable extends JFrame{
public static void main(String []args){
ContohJTable ap1 = new ContohJTable();
}

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

Object[][] data = {
{"11234", "Asti Damayanti", "Akunting", new Long (2200000)},
{"11235", "Eritria", "Pemasaran", new Long (1200000)},
{"11236", "Dony Kilana", "Personalia", new Long (1200000)},
{"11237", "Tika Srihartini", "Setyowati", "EDP", new Integer (1500000)},
{"11238", "Riri Kirana", "Akunting", new Long (2000000)},
{"11239", "Galih Zikri", "Akunting", new Long (1000000)},
{"11240", "Rani Ardini", "Pemasaran", new Long (1100000)},
{"11241", "Listya", "Pemasaran", new Long (1100000)},
};

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);
}
}




G. Membuat program dengan mengunakan JTree

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);
}
}


Contohnya: