Persoalan:
membuat program berfungsi untuk di tampilkan menjadi lebih menarik dengan menggunakan GUI
Source Code :
import javax.swing.table.DefaultTableModel;
/*
* To change this template, choose Tools |
Templates
* and open the template in the editor.
*/
/**
*
* @author User-NB
*/
public class NewJFrame extends javax.swing.JFrame {
int pilihan, index;
String penjualan[][] = new String
[100][4];
String jenisbarangstring = null;
String namabarang = null;
String jumlahbarang = null;
String hargasatuan = null;
int rowselected;
public NewJFrame() {
initComponents();
index = 0;
}
/**
* 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();
buttonGroup2 = new
javax.swing.ButtonGroup();
jScrollPane1 = new
javax.swing.JScrollPane();
jTable1 = new
javax.swing.JTable();
jPanel1 = new
javax.swing.JPanel();
jLabel1 = new
javax.swing.JLabel();
jLabel2 = new
javax.swing.JLabel();
jComboBox1JenisBarang
= new javax.swing.JComboBox();
jLabel3 = new
javax.swing.JLabel();
jLabel4 = new
javax.swing.JLabel();
jLabel5 = new
javax.swing.JLabel();
jLabel6 = new
javax.swing.JLabel();
jTextField1Jumlah =
new javax.swing.JTextField();
jTextField2HargaSatuan
= new javax.swing.JTextField();
jTextField3NamaBarang
= new javax.swing.JTextField();
jLabel7 = new
javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jButton1Tambah = new
javax.swing.JButton();
jButton2Ubah = new
javax.swing.JButton();
jButton3Hapus = new
javax.swing.JButton();
jScrollPane2 = new
javax.swing.JScrollPane();
jTable2Belanja = new
javax.swing.JTable();
jTable1.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(jTable1);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Nama Barang");
jLabel2.setText(":");
jComboBox1JenisBarang.setModel(new javax.swing.DefaultComboBoxModel(new
String[] { "", "Beras", "Gula", "Minyak
Goreng" }));
jComboBox1JenisBarang.addItemListener(new
java.awt.event.ItemListener() {
public
void itemStateChanged(java.awt.event.ItemEvent evt) {
jComboBox1JenisBarangItemStateChanged(evt);
}
});
jComboBox1JenisBarang.addActionListener(new java.awt.event.ActionListener() {
public
void actionPerformed(java.awt.event.ActionEvent evt) {
jComboBox1JenisBarangActionPerformed(evt);
}
});
jLabel3.setText("Jenis Barang");
jLabel4.setText("Jumlah
Barang");
jLabel5.setText("Harga Satuan");
jLabel6.setText(":");
jTextField1Jumlah.addActionListener(new java.awt.event.ActionListener() {
public
void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1JumlahActionPerformed(evt);
}
});
jTextField2HargaSatuan.addActionListener(new java.awt.event.ActionListener() {
public
void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField2HargaSatuanActionPerformed(evt);
}
});
jLabel7.setText(":");
jLabel8.setText(":");
jButton1Tambah.setText("Tambah");
jButton1Tambah.addActionListener(new java.awt.event.ActionListener() {
public
void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1TambahActionPerformed(evt);
}
});
jButton2Ubah.setText("Ubah");
jButton2Ubah.addMouseListener(new java.awt.event.MouseAdapter() {
public
void mouseClicked(java.awt.event.MouseEvent evt) {
jButton2UbahMouseClicked(evt);
}
});
jButton3Hapus.setText("Hapus");
jTable2Belanja.setModel(new javax.swing.table.DefaultTableModel(
new
Object [][] {
},
new
String [] {
"ID Transaksi", "Nama Barang", "Jumlah
Barang", "Harga Satuan", "Total Harga"
}
));
jScrollPane2.setViewportView(jTable2Belanja);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(26, 26, 26)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel4)
.addGap(0, 0, Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel3)
.addComponent(jLabel1))
.addGap(18, 18, 18)
.addComponent(jLabel2,
javax.swing.GroupLayout.PREFERRED_SIZE, 9,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 23,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 23,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jComboBox1JenisBarang,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel5)
.addGap(10, 10, 10)
.addComponent(jLabel6)
.addGap(18, 18, 18)
.addComponent(jTextField2HargaSatuan,
javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jTextField3NamaBarang, javax.swing.GroupLayout.PREFERRED_SIZE,
162, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField1Jumlah, javax.swing.GroupLayout.PREFERRED_SIZE, 68,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jButton1Tambah)
.addGap(27, 27,
27)
.addComponent(jButton2Ubah)
.addGap(26, 26,
26)
.addComponent(jButton3Hapus)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jComboBox1JenisBarang,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel5)
.addComponent(jLabel3)
.addComponent(jLabel6,
javax.swing.GroupLayout.PREFERRED_SIZE, 25,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jTextField2HargaSatuan,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(34, 34, 34)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField3NamaBarang,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE,
32, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(17, 17, 17)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(jTextField1Jumlah,
javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel8,
javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(39, 39, 39)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1Tambah)
.addComponent(jButton2Ubah)
.addComponent(jButton3Hapus))
.addGap(18, 18, 18)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void
jTextField1JumlahActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your
handling code here:
}
private void jTextField2HargaSatuanActionPerformed(java.awt.event.ActionEvent
evt) {
// TODO add your
handling code here:
}
private void jComboBox1JenisBarangItemStateChanged(java.awt.event.ItemEvent
evt) {
// TODO add your
handling code here:
cekhargasatuan();
}
private void
jButton1TambahActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your
handling code here:
tambahbarang();
jComboBox1JenisBarang.setSelectedIndex(0);
jTextField3NamaBarang.setText("");
jTextField1Jumlah.setText("");
jTextField2HargaSatuan.setText("");
}
private void
jComboBox1JenisBarangActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your
handling code here:
}
private void
jButton2UbahMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your
handling code here:
rowselected =
jTable2Belanja.getSelectedRow();
System.out.println("rowselected : "+rowselected);
namabarang = (String)
jTable2Belanja.getValueAt(rowselected, 1);
jumlahbarang =
(String) jTable2Belanja.getValueAt(rowselected, 2);
hargasatuan = (String)
jTable2Belanja.getValueAt(rowselected, 3);
jTextField3NamaBarang.setText(namabarang);
jTextField3NamaBarang.setText(jumlahbarang);
jTextField3NamaBarang.setText(hargasatuan);
}
/**
* @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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch
(InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch
(IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE,
null, ex);
} catch
(javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.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 NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not
modify
private javax.swing.ButtonGroup
buttonGroup1;
private javax.swing.ButtonGroup
buttonGroup2;
private javax.swing.JButton
jButton1Tambah;
private javax.swing.JButton
jButton2Ubah;
private javax.swing.JButton
jButton3Hapus;
private javax.swing.JComboBox
jComboBox1JenisBarang;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane
jScrollPane1;
private javax.swing.JScrollPane
jScrollPane2;
private javax.swing.JTable jTable1;
private javax.swing.JTable
jTable2Belanja;
private javax.swing.JTextField
jTextField1Jumlah;
private javax.swing.JTextField
jTextField2HargaSatuan;
private javax.swing.JTextField
jTextField3NamaBarang;
// End of variables declaration
private void tambahbarang(){
penjualan[index][0] =
jTextField3NamaBarang.getText();
System.out.println(penjualan[index][0]);
penjualan[index][1] =
jTextField1Jumlah.getText();
System.out.println(penjualan[index][1]);
penjualan[index][2] =
jTextField2HargaSatuan.getText();
System.out.println(penjualan[index][2]);
int a = Integer.parseInt(jTextField1Jumlah.getText());
int b =
Integer.parseInt(jTextField2HargaSatuan.getText());
int total = a*b;
penjualan[index][3] =
String.valueOf(total);
System.out.println(penjualan[index][3]);
DefaultTableModel model =
(DefaultTableModel) jTable2Belanja.getModel();
model.addRow(new Object[] {(index +
1), penjualan[index][0],
penjualan[index][1],
penjualan[index][2], penjualan[index][3]});
}
private void cekhargasatuan()
{
jenisbarangstring =
(String)jComboBox1JenisBarang.getSelectedItem();
if(jenisbarangstring.equalsIgnoreCase("beras"))
{
hargasatuan =
"3000";
}
else if
(jenisbarangstring.equalsIgnoreCase("gula"))
{
hargasatuan =
"4000";
}
else if
(jenisbarangstring.equalsIgnoreCase("minyak goreng"))
{
hargasatuan =
"5000";
}
else
{
hargasatuan = null;
}
jTextField2HargaSatuan.setText(hargasatuan);
}
}