Assalamualaikum...
Apa kabar sobat pintar, pastinya sehat dong. Nah langsung saja ya kita lanjut ke part 2 dalam tutorial belajar java dalam membuat komponen pada java swing lanjutan.
1.
Membuat program dengan
menggunakan JTabbedPane
Source
Code :
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();
}
}
Output
Program :
2.
Membuat program dengan
menggunakan JTable
Source
Code :
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, 150);
Object[][] data = {
{"11234", "Asti Damayanti", "Akunting", new
Long(2200000)},
{"11235", "Eritria", "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 Ardini", "Pemasaran", new
Long(1100000)},
{"11241", "Listya", "Pemasaran", new Long(1100000)},
};
String[] judulKolom = {"NIP", "Nama", "Bagian", "Gaji"};
JTable tabel = new JTable(data, judulKolom);
JScrollPane scrollPane = new JScrollPane(tabel);
add(scrollPane);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
Output
Program :
3.
Membuat program dengan
menggunakan Jtree
Source
Code :
import javax.swing.*;
import javax.swing.tree.*;
import javax.swing.event.*;
public class ContohJTree extends JFrame {
public static void main(String[] args) {
ContohJTree apl = 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 austria = new
DefaultMutableTreeNode("Austria");
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(austria);
eropa.add(jerman);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// --- Tampilkan frame
setVisible(true);
}
}
Output
Program :