1. Melanjutkan project aplikasi kasir dan aplikasi tiket wisata di java Netbeans, pertama buat Package baru dengan nama tiket. Kita akan buat aplikasi tiket di package ini
Klik enter, Kenapa already exists karena saya sudah membuat package dengan nama tiket.
2. Selanjutnya buat JFrame Form di Package tiket dengan nama TiketWisata
Klik Finish
3. Buka JFrame tiket wisata kemudian sesuaikan desain seperti dibawah
Keterangan :
1. jButton : btnLihatData
2. jComboBox : cJenisWisata
3. jButton : btnBeli
4. jMenu Bar didalam nya ada jMenu Item : jMenuItem1
4. Setelah desain sudah selesai tulis kodingan di tab source
Pertama import fungsi yang akan kita gunakan di bagian atas bawahnya package
5. Deklarasi ComboBox model
6. Kemudian saat jMenu Item Home di klik akan beralih ke Frame Home tambahkan kode berikut :
Home home = new Home();
home.setLocationRelativeTo(this);
home.setVisible(true);
this.setVisible(false);
7. Saat frame dijalankan akan menampilkan model ComboBox tambahkan kode dibawah ini :
mdlJenisWisata.addElement("Pilih Wisata");
mdlJenisWisata.addElement("Jatim Park 1");
mdlJenisWisata.addElement("Jatim Park 2");
mdlJenisWisata.addElement("Jatim Park 3");
mdlJenisWisata.addElement("Eco Green Park");
cJenisWisata.setModel(mdlJenisWisata);
8. Saat btnBeli diklik akan beralih ke Frame yang dipilih tambahkan kode berikut :
Kita akan buat frame nya setelah ini jika ada yang merah abaikan dulu
JTP1 jtp1 = new JTP1();
JTP2 jtp2 = new JTP2();
JTP3 jtp3 = new JTP3();
EGP egp = new EGP();
if(cJenisWisata.getSelectedItem().equals("Jatim Park 1")){
jtp1.setLocationRelativeTo(this);
jtp1.setVisible(true);
}else if(cJenisWisata.getSelectedItem().equals("Jatim Park 2")){
jtp2.setLocationRelativeTo(this);
jtp2.setVisible(true);
}else if(cJenisWisata.getSelectedItem().equals("Jatim Park 3")){
jtp3.setLocationRelativeTo(this);
jtp3.setVisible(true);
}else if(cJenisWisata.getSelectedItem().equals("Eco Green Park")){
egp.setLocationRelativeTo(this);
egp.setVisible(true);
}else{
JOptionPane.showMessageDialog(this, "Silahkan pilih jenis wisata", "Pilih jenis wisata !", JOptionPane.WARNING_MESSAGE);
}
9. Saat btnLihatData di klik akan menampilkan data penjualan.
DataPenjualan data = new DataPenjualan();
data.setLocationRelativeTo(this);
data.setVisible(true);
10. Sekarang kita buat frame yang dibutuhkan kemudian membuat class untuk funsi dan data
Cara nya sama saat kita membuat frame TiketWisata tapi namanya sesuaikan seperi pada gambar dibawah :
Keterangan :
Data.java adalah Class
DataPenjualan.java : JFrame Form
EGP.java : JFrame Form
Fungsi.java : Class
JTP1,2,3 : JFrame Form
11. Kemudian pertama kita isi kodingan untuk Fungsi.java
package tiket;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
import static tiket.Data.jenisWisata;
import static tiket.Data.ArrSubTotal;
import static tiket.Data.ArrJumlahTiket;
import static tiket.Data.ArrTanggal;
/**
*
* @author djavu
*/
public class Fungsi {
public static int getSubTotal(int a,int b){
int total = a*b;
return total;
}
public static int getJumlah(int a,int b){
int total = a+b;
return total;
}
public static int getTotal(int a,int b,int c){
int total = a+b+c;
return total;
}
public static void setArray(String jenis,String tgl, int subs, int jumlah){
jenisWisata.add(jenis);
ArrSubTotal.add(subs);
ArrJumlahTiket.add(jumlah);
ArrTanggal.add(tgl);
}
public static void delArray(int i){
if(jenisWisata.size()>0){
jenisWisata.remove(i);
ArrSubTotal.remove(i);
ArrJumlahTiket.remove(i);
ArrTanggal.remove(i);
}
}
public static void delAll(){
if(jenisWisata.size()>0){
jenisWisata.removeAll(jenisWisata);
ArrSubTotal.removeAll(ArrSubTotal);
ArrJumlahTiket.removeAll(ArrJumlahTiket);
ArrTanggal.removeAll(ArrTanggal);
}
}
public static String setIDR(int var){
//rupiah format
String idr;
DecimalFormat rupiah = (DecimalFormat) DecimalFormat.getCurrencyInstance();
DecimalFormatSymbols rp = new DecimalFormatSymbols();
rp.setCurrencySymbol("IDR. ");
rp.setMonetaryDecimalSeparator(',');
rp.setGroupingSeparator('.');
rupiah.setDecimalFormatSymbols(rp);
//end
idr = rupiah.format(var);
return idr;
}
public static JTable setKolom(JTable tblPembelian){
TableColumn column;
tblPembelian.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
column = tblPembelian.getColumnModel().getColumn(0);
column.setPreferredWidth(40);
column = tblPembelian.getColumnModel().getColumn(1);
column.setPreferredWidth(450);
column = tblPembelian.getColumnModel().getColumn(2);
column.setPreferredWidth(120);
column = tblPembelian.getColumnModel().getColumn(3);
column.setPreferredWidth(148);
column = tblPembelian.getColumnModel().getColumn(4);
column.setPreferredWidth(182);
return tblPembelian;
}
}
12. Setelah itu kita isi kode untuk Data.java ini isinya adalah Array yang akan kita gunakan untuk menyimpan data untuk ditampilkan ke tabel
package tiket;
import java.util.ArrayList;
/**
*
* @author djavu
*/
public class Data {
public static ArrayList<String> jenisWisata = new ArrayList<>();
public static ArrayList<Integer> ArrSubTotal = new ArrayList<>();
public static ArrayList<Integer> ArrJumlahTiket = new ArrayList<>();
public static ArrayList<String> ArrTanggal = new ArrayList<>();
}
13. Setelah itu kita beralih ke JTP1.java
Desain seperti gambar dibawah
Kemudian dibawah ini ada nama nama pallete yang digunakan beserta variabel nya
14. Isi kodingan JTP1.java seperti dibawah ini.
Full source code nya :
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tiket;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import static tiket.Fungsi.getJumlah;
import static tiket.Fungsi.getSubTotal;
import static tiket.Fungsi.getTotal;
import static tiket.Fungsi.setArray;
import static tiket.Fungsi.setIDR;
/**
*
* @author djavu
*/
public class JTP1 extends javax.swing.JFrame {
int baris = 0;
static Object kolom [] = {"Jenis","Jumlah","Total Harga","Tanggal"};
DefaultTableModel tbl = new DefaultTableModel(kolom,baris);;
/**
* Creates new form JTP1
*/
public JTP1() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
buttonGroup1 = new javax.swing.ButtonGroup();
cTiket1 = new javax.swing.JCheckBox();
lblTiket1 = new javax.swing.JLabel();
txtJumlahTiket1 = new javax.swing.JTextField();
txtSubTiket1 = new javax.swing.JTextField();
cTiket2 = new javax.swing.JCheckBox();
lblTiket2 = new javax.swing.JLabel();
txtSubTiket2 = new javax.swing.JTextField();
lblTiket3 = new javax.swing.JLabel();
txtTotal = new javax.swing.JTextField();
cTiket3 = new javax.swing.JCheckBox();
txtSubTiket3 = new javax.swing.JTextField();
btnBayar = new javax.swing.JButton();
txtJumlahTiket3 = new javax.swing.JTextField();
txtJumlahTiket2 = new javax.swing.JTextField();
btnHitung = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentShown(java.awt.event.ComponentEvent evt) {
formComponentShown(evt);
}
});
cTiket1.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
cTiket1.setText("TIKET PAKET 1 (JATIMPARK 1+MUSEUM TUBUH)");
cTiket1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cTiket1ActionPerformed(evt);
}
});
lblTiket1.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
lblTiket1.setText("120000");
txtJumlahTiket1.setFont(new java.awt.Font("Dialog", 0, 14)); // NOI18N
txtJumlahTiket1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
txtJumlahTiket1KeyReleased(evt);
}
});
txtSubTiket1.setFont(new java.awt.Font("Dialog", 0, 14)); // NOI18N
cTiket2.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
cTiket2.setText("TIKET SAKTI (BERLAKU 2 HARI)");
lblTiket2.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
lblTiket2.setText("375000");
txtSubTiket2.setFont(new java.awt.Font("Dialog", 0, 14)); // NOI18N
lblTiket3.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
lblTiket3.setText("450000");
txtTotal.setFont(new java.awt.Font("Dialog", 0, 14)); // NOI18N
cTiket3.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
cTiket3.setText("TIKET SUPER SAKTI (BERLAKU 3 HARI)");
txtSubTiket3.setFont(new java.awt.Font("Dialog", 0, 14)); // NOI18N
btnBayar.setBackground(new java.awt.Color(0, 153, 255));
btnBayar.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
btnBayar.setForeground(new java.awt.Color(255, 255, 255));
btnBayar.setText("BAYAR");
btnBayar.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnBayarActionPerformed(evt);
}
});
txtJumlahTiket3.setFont(new java.awt.Font("Dialog", 0, 14)); // NOI18N
txtJumlahTiket3.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
txtJumlahTiket3KeyReleased(evt);
}
});
txtJumlahTiket2.setFont(new java.awt.Font("Dialog", 0, 14)); // NOI18N
txtJumlahTiket2.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyReleased(java.awt.event.KeyEvent evt) {
txtJumlahTiket2KeyReleased(evt);
}
});
btnHitung.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
btnHitung.setText("Hitung");
btnHitung.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnHitungActionPerformed(evt);
}
});
jLabel1.setFont(new java.awt.Font("Dialog", 1, 14)); // NOI18N
jLabel1.setText("Total Harga");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addComponent(cTiket1)
.addGap(30, 30, 30)
.addComponent(lblTiket1, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(cTiket2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(lblTiket2, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(50, 50, 50)))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(txtJumlahTiket2, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(56, 56, 56)
.addComponent(txtSubTiket2, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(txtJumlahTiket1, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(55, 55, 55)
.addComponent(txtSubTiket1, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGroup(layout.createSequentialGroup()
.addComponent(cTiket3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(lblTiket3, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(50, 50, 50)
.addComponent(txtJumlahTiket3, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(56, 56, 56)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(btnHitung)
.addComponent(txtSubTiket3, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addComponent(btnBayar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jLabel1)
.addGap(56, 56, 56)
.addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(lblTiket1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(cTiket1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtSubTiket1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtJumlahTiket1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(cTiket2, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblTiket2, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtSubTiket2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtJumlahTiket2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(cTiket3, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lblTiket3, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtSubTiket3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(txtJumlahTiket3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnHitung)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1))
.addGap(18, 18, 18)
.addComponent(btnBayar, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void txtJumlahTiket1KeyReleased(java.awt.event.KeyEvent evt) {
if(txtJumlahTiket1.getText().equals("")){
txtJumlahTiket1.setText("0");
txtSubTiket1.setText("0");
}else{
int hrg = Integer.parseInt(lblTiket1.getText());
int jml = Integer.parseInt(txtJumlahTiket1.getText());
int sub_total = getSubTotal(jml,hrg);
txtSubTiket1.setText(""+sub_total);
}
}
private void formComponentShown(java.awt.event.ComponentEvent evt) {
txtJumlahTiket1.setText("0");
txtJumlahTiket2.setText("0");
txtJumlahTiket3.setText("0");
txtSubTiket1.setText("0");
txtSubTiket2.setText("0");
txtSubTiket3.setText("0");
txtTotal.setText("0");
txtSubTiket2.setEditable(false);
txtSubTiket1.setEditable(false);
txtSubTiket3.setEditable(false);
txtTotal.setEditable(false);
}
private void txtJumlahTiket3KeyReleased(java.awt.event.KeyEvent evt) {
if(txtJumlahTiket3.getText().equals("")){
txtJumlahTiket3.setText("0");
txtSubTiket3.setText("0");
}else{
int hrg = Integer.parseInt(lblTiket3.getText());
int jml = Integer.parseInt(txtJumlahTiket3.getText());
int sub_total = getSubTotal(jml,hrg);
txtSubTiket3.setText(""+sub_total);
}
}
private void txtJumlahTiket2KeyReleased(java.awt.event.KeyEvent evt) {
// TODO add your handling code here:
if(txtJumlahTiket2.getText().equals("")){
txtJumlahTiket2.setText("0");
txtSubTiket2.setText("0");
}else{
int hrg = Integer.parseInt(lblTiket2.getText());
int jml = Integer.parseInt(txtJumlahTiket2.getText());
int sub_total = getSubTotal(jml,hrg);
txtSubTiket2.setText(""+sub_total);
}
}
private void btnHitungActionPerformed(java.awt.event.ActionEvent evt) {
int a,b,c,hasil;
a = Integer.parseInt(txtSubTiket1.getText());
b = Integer.parseInt(txtSubTiket2.getText());
c = Integer.parseInt(txtSubTiket3.getText());
if(cTiket1.isSelected()&&txtJumlahTiket1.getText().equals("0")){
JOptionPane.showMessageDialog(this, "Silahkan isi jumlah tiket !");
}else if(cTiket2.isSelected()&&txtJumlahTiket2.getText().equals("0")){
JOptionPane.showMessageDialog(this, "Silahkan isi jumlah tiket !");
}else if(cTiket3.isSelected()&&txtJumlahTiket3.getText().equals("0")){
JOptionPane.showMessageDialog(this, "Silahkan isi jumlah tiket !");
}else{
if(cTiket1.isSelected()&&cTiket2.isSelected()&&cTiket3.isSelected()){
hasil = getTotal(a,b,c);
txtTotal.setText(""+hasil);
}else if(cTiket1.isSelected()&&cTiket2.isSelected()){
hasil = getJumlah(a,b);
txtTotal.setText(""+hasil);
}else if(cTiket1.isSelected()&&cTiket3.isSelected()){
hasil = getJumlah(a,c);
txtTotal.setText(""+hasil);
}else if(cTiket1.isSelected()){
txtTotal.setText(""+a);
}else if(cTiket2.isSelected()){
txtTotal.setText(""+b);
}else if(cTiket3.isSelected()){
txtTotal.setText(""+c);
}else{
JOptionPane.showMessageDialog(this, "Silahkan pilih jenis tiket !");
}
}
}
private void btnBayarActionPerformed(java.awt.event.ActionEvent evt) {
int jml,jml2,jml3,subs,jumlah;
String jenis1,jenis2,jenis3,tgl,jenis,tanggal;
Date date = new Date();
jml = Integer.parseInt(txtJumlahTiket1.getText());
jml2 = Integer.parseInt(txtJumlahTiket2.getText());
jml3 = Integer.parseInt(txtJumlahTiket3.getText());
jenis1 = cTiket1.getText();
jenis2 = cTiket2.getText();
jenis3 = cTiket3.getText();
if(txtTotal.getText().equals("0")){
JOptionPane.showMessageDialog(this, "Silahkan hitung terlebih dahulu !");
}else{
if(cTiket1.isSelected()&&cTiket2.isSelected()&&cTiket3.isSelected()){
jumlah = getTotal(jml,jml2,jml3);
jenis = jenis1+","+jenis2+","+jenis3;
tgl = date.toString();
subs = Integer.parseInt(txtTotal.getText());
setArray(jenis,tgl,subs,jumlah);
JOptionPane.showMessageDialog(this, "Jenis : "+jenis+"\n"
+"Jumlah Tiket : "+jumlah+"\n"
+"Total : "+setIDR(subs)+"\n"
+"Tanggal Pembelian : "+tgl);
}else if(cTiket1.isSelected()&&cTiket2.isSelected()){
jumlah = getJumlah(jml,jml2);
jenis = jenis1+","+jenis2;
tgl = date.toString();
subs = Integer.parseInt(txtTotal.getText());
setArray(jenis,tgl,subs,jumlah);
JOptionPane.showMessageDialog(this, "Jenis : "+jenis+"\n"
+"Jumlah Tiket : "+jumlah+"\n"
+"Total : "+setIDR(subs)+"\n"
+"Tanggal Pembelian : "+tgl);
}else if(cTiket1.isSelected()&&cTiket3.isSelected()){
jumlah = getJumlah(jml,jml3);
jenis = jenis1+","+jenis3;
tgl = date.toString();
subs = Integer.parseInt(txtTotal.getText());
setArray(jenis,tgl,subs,jumlah);
JOptionPane.showMessageDialog(this, "Jenis : "+jenis+"\n"
+"Jumlah Tiket : "+jumlah+"\n"
+"Total : "+setIDR(subs)+"\n"
+"Tanggal Pembelian : "+tgl);
}else if(cTiket1.isSelected()){
jumlah = jml;
jenis = jenis1;
tgl = date.toString();
subs = Integer.parseInt(txtTotal.getText());
setArray(jenis,tgl,subs,jumlah);
JOptionPane.showMessageDialog(this, "Jenis : "+jenis+"\n"
+"Jumlah Tiket : "+jumlah+"\n"
+"Total : "+setIDR(subs)+"\n"
+"Tanggal Pembelian : "+tgl);
}else if(cTiket2.isSelected()){
jumlah = jml2;
jenis = jenis2;
tgl = date.toString();
subs = Integer.parseInt(txtTotal.getText());
setArray(jenis,tgl,subs,jumlah);
JOptionPane.showMessageDialog(this, "Jenis : "+jenis+"\n"
+"Jumlah Tiket : "+jumlah+"\n"
+"Total : "+setIDR(subs)+"\n"
+"Tanggal Pembelian : "+tgl);
}else if(cTiket3.isSelected()){
jumlah = jml3;
jenis = jenis3;
tgl = date.toString();
subs = Integer.parseInt(txtTotal.getText());
setArray(jenis,tgl,subs,jumlah);
JOptionPane.showMessageDialog(this, "Jenis : "+jenis+"\n"
+"Jumlah Tiket : "+jumlah+"\n"
+"Total : "+setIDR(subs)+"\n"
+"Tanggal Pembelian : "+tgl);
}else{
JOptionPane.showMessageDialog(this, "Silahkan pilih jenis tiket !");
}
txtJumlahTiket1.setText("0");
txtJumlahTiket2.setText("0");
txtJumlahTiket3.setText("0");
txtSubTiket1.setText("0");
txtSubTiket2.setText("0");
txtSubTiket3.setText("0");
txtTotal.setText("0");
cTiket1.setSelected(false);
cTiket2.setSelected(false);
cTiket3.setSelected(false);
txtTotal.setEditable(false);
}
}
private void cTiket1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(JTP1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(JTP1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JTP1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JTP1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JTP1().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnBayar;
private javax.swing.JButton btnHitung;
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JCheckBox cTiket1;
private javax.swing.JCheckBox cTiket2;
private javax.swing.JCheckBox cTiket3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel lblTiket1;
private javax.swing.JLabel lblTiket2;
private javax.swing.JLabel lblTiket3;
private javax.swing.JTextField txtJumlahTiket1;
private javax.swing.JTextField txtJumlahTiket2;
private javax.swing.JTextField txtJumlahTiket3;
private javax.swing.JTextField txtSubTiket1;
private javax.swing.JTextField txtSubTiket2;
private javax.swing.JTextField txtSubTiket3;
private javax.swing.JTextField txtTotal;
// End of variables declaration
}
15. Untuk JTP2.java, JTP3.java, dan EGP.java sama in desain dan kodingan dengan JTP1.java
Cuma bedakan nama paket dan label harga
JTP2
JTP3
EGP
16. Setelah itu kita beralih ke DataPenjualan.java
Pertama Anda harus menambahkan Libraries Apache POI Excel untuk Export data tabel ke file Excel. Caranya download dulu libraries nya di https://www-eu.apache.org/dist/poi/release/bin/poi-bin-4.1.1-20191023.zip
Setelah di download ekstrak file zip nyaMasuk ke netbeans project yang kita buatKlik kanan di Package LibrariesAdd JAR/FolderPilih file .jar yang sudah di download
Desain :
Source code nya :
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tiket;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import static tiket.Data.ArrJumlahTiket;
import static tiket.Data.ArrSubTotal;
import static tiket.Data.ArrTanggal;
import static tiket.Data.jenisWisata;
import static tiket.Fungsi.delAll;
import static tiket.Fungsi.delArray;
import static tiket.Fungsi.setIDR;
import static tiket.Fungsi.setKolom;
/**
*
* @author djavu
*/
public class DataPenjualan extends javax.swing.JFrame {
int baris = 0;
static Object kolom[] = {"No.", "Jenis", "Jumlah", "Total Harga", "Tanggal"};
DefaultTableModel tbl = new DefaultTableModel(kolom, baris);
;
/**
* Creates new form DataPengunjung
*/
public DataPenjualan() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
tblPembelian = new javax.swing.JTable();
btnClear = new javax.swing.JButton();
btnDel = new javax.swing.JButton();
btnExport = new javax.swing.JButton();
jMenuBar1 = new javax.swing.JMenuBar();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentShown(java.awt.event.ComponentEvent evt) {
formComponentShown(evt);
}
});
tblPembelian.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(tblPembelian);
btnClear.setText("Clear");
btnClear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnClearActionPerformed(evt);
}
});
btnDel.setText("Delete");
btnDel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDelActionPerformed(evt);
}
});
btnExport.setText("Export (Excel)");
btnExport.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnExportActionPerformed(evt);
}
});
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(btnDel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnClear)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btnExport)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 950, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 267, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btnDel)
.addComponent(btnClear)
.addComponent(btnExport))
.addGap(0, 11, Short.MAX_VALUE))
);
pack();
setLocationRelativeTo(null);
}// </editor-fold>
private void btnDelActionPerformed(java.awt.event.ActionEvent evt) {
int baris = tblPembelian.getSelectedRow();
if (tblPembelian.isRowSelected(baris)) {
tbl.removeRow(baris);
}
delArray(baris);
tbl.getDataVector().removeAllElements();
String jns = null, tgl = null;
int subs = 0, jml = 0;
int no;
if (jenisWisata.isEmpty()) {
JOptionPane.showMessageDialog(this, "Data Kosong");
} else {
for (int i = 0; i < jenisWisata.size(); i++) {
no = i + 1;
jns = jenisWisata.get(i);
subs = ArrSubTotal.get(i);
jml = ArrJumlahTiket.get(i);
tgl = ArrTanggal.get(i);
tbl.addRow(new Object[]{no, jns, jml, setIDR(subs), tgl});
}
}
}
private void btnClearActionPerformed(java.awt.event.ActionEvent evt) {
tbl.getDataVector().removeAllElements();
delAll();
tbl.fireTableDataChanged();
}
private void formComponentShown(java.awt.event.ComponentEvent evt) {
tblPembelian.setModel(tbl);
setKolom(tblPembelian);
String jns = null, tgl = null;
int subs = 0, jml = 0;
int no;
if (jenisWisata.isEmpty()) {
JOptionPane.showMessageDialog(this, "Data Kosong");
} else {
for (int i = 0; i < jenisWisata.size(); i++) {
no = i + 1;
jns = jenisWisata.get(i);
subs = ArrSubTotal.get(i);
jml = ArrJumlahTiket.get(i);
tgl = ArrTanggal.get(i);
tbl.addRow(new Object[]{no, jns, jml, setIDR(subs), tgl});
}
}
}
private void btnExportActionPerformed(java.awt.event.ActionEvent evt) {
//choose locatioan for save
FileOutputStream excelFou = null;
XSSFWorkbook exExporter = null;
BufferedOutputStream excelBou = null;
JFileChooser chooser = new JFileChooser("C:\\Users\\djavu\\Desktop");
chooser.setDialogTitle("Save As");
FileNameExtensionFilter ex = new FileNameExtensionFilter("EXCEL FILE", "xls", "xlsx", "xlsm");
chooser.setFileFilter(ex);
int exChooser = chooser.showSaveDialog(null);
if (exChooser == JFileChooser.APPROVE_OPTION) {
try {
exExporter = new XSSFWorkbook();
XSSFSheet exSheet = exExporter.createSheet("JTable Sheet");
for (int i = 0; i < tblPembelian.getRowCount(); i++) {
XSSFRow excelRow = exSheet.createRow(i);
for (int j = 0; j < tblPembelian.getColumnCount(); j++) {
XSSFCell excelCell = excelRow.createCell(j);
excelCell.setCellValue(tblPembelian.getValueAt(i, j).toString());
}
}
excelFou = new FileOutputStream(chooser.getSelectedFile() + ".xlsx");
excelBou = new BufferedOutputStream(excelFou);
exExporter.write(excelBou);
JOptionPane.showMessageDialog(null, "Success");
} catch (FileNotFoundException ex1) {
ex1.printStackTrace();
} catch (IOException ex1) {
ex1.printStackTrace();
} finally {
try {
if (excelBou != null) {
excelBou.close();
}
if (excelFou != null) {
excelFou.close();
}
if (exExporter != null) {
exExporter.close();
}
} catch (IOException ex1) {
ex1.printStackTrace();
}
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(DataPenjualan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(DataPenjualan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(DataPenjualan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(DataPenjualan.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
//</editor-fold>
//</editor-fold>
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new DataPenjualan().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnClear;
private javax.swing.JButton btnDel;
private javax.swing.JButton btnExport;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tblPembelian;
// End of variables declaration
}
17. Jika sudah tidak ada error coba run project. Jika ada error mungkin ada kesalahan Syntax atau typo
0 Comments