Private Void BtnDeleteActionPerformed

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

import javax.swing.

JOptionPane;
import javax.swing.table.DefaultTableModel;

/*
* 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.
*/

/**
*
* @author lab609
*/
public class tableframe extends javax.swing.JFrame {
String Orders[][]={
{"1", "Coke", "12.00"},
{"2", "Sandwich", "25.00"},
{"3","Candy", "5.00"}};

/**
* Creates new form tableframe
*/
public tableframe() {
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() {

jLabel1 = new javax.swing.JLabel();


jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
txtDesc = new javax.swing.JTextField();
txtPrice = new javax.swing.JTextField();
btnAdd = new javax.swing.JButton();
btnUpdate = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
TableItems = new javax.swing.JTable();
jLabel4 = new javax.swing.JLabel();
lblTotal = new javax.swing.JLabel();
txtID = new javax.swing.JTextField();
btnDelete = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setAlwaysOnTop(true);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jLabel1.setText("ID:");

jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N


jLabel2.setText("Description:");

jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N


jLabel3.setText("Price:");

btnAdd.setText("Add");
btnAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAddActionPerformed(evt);
}
});

btnUpdate.setText("Update");
btnUpdate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnUpdateActionPerformed(evt);
}
});

TableItems.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.bor
der.BevelBorder.RAISED));
TableItems.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {

},
new String [] {
"ID", "Description", "Price"
}
));
jScrollPane1.setViewportView(TableItems);

jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N


jLabel4.setText("TOTAL:");

lblTotal.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N


lblTotal.setText("0");

txtID.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtIDActionPerformed(evt);
}
});

btnDelete.setText("Delete");
btnDelete.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnDeleteActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new


javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addGroup(layout.createSequentialGroup()
.addContainerGap()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING
)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addGap(11, 11, 11)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(txtDesc)
.addComponent(txtPrice)
.addComponent(txtID,
javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)))
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(btnAdd)
.addGap(18, 18, 18)
.addComponent(btnUpdate)
.addGap(36, 36, 36)
.addComponent(btnDelete)))
.addGap(28, 130, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1,
javax.swing.GroupLayout.DEFAULT_SIZE, 472, Short.MAX_VALUE)))
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(jLabel4)
.addGap(18, 18, 18)
.addComponent(lblTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 55,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(72, 72, 72))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E)
.addComponent(jLabel1)
.addComponent(txtID, 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.BASELIN
E)
.addComponent(jLabel2)
.addComponent(txtDesc, 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.BASELIN
E)
.addComponent(jLabel3)
.addComponent(txtPrice, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E)
.addComponent(btnAdd)
.addComponent(btnUpdate)
.addComponent(btnDelete))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
135, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E)
.addComponent(jLabel4)
.addComponent(lblTotal))
.addGap(83, 83, 83))
);

pack();
}// </editor-fold>

private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {


double newTotal= Double.parseDouble(lblTotal.getText())-
Double.parseDouble();
lblTotal.setText(newTotal+"");// TODO add your handling code here:
}

private void formWindowOpened(java.awt.event.WindowEvent evt) {


int n= Orders.length;
DefaultTableModel model = (DefaultTableModel) TableItems.getModel();
double total=0.0;
for(int ctr=0; ctr<=n-1; ctr++){
model.addRow(new Object[] {Orders[ctr][0], Orders[ctr][1],Orders[ctr][2]});
total=total+ Double.parseDouble(Orders[ctr][2]);

// System.out.println(Orders[ctr][0] + " "+ Orders[ctr][1]+" "


// +Orders[ctr][2]);

}
lblTotal.setText(total+"");
}

private void txtIDActionPerformed(java.awt.event.ActionEvent evt) {


// TODO add your handling code here:
}

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {


//validation
if(txtID.getText().equals("") || txtDesc.getText().equals("")||
txtPrice.getText().equals("")){
JOptionPane.showMessageDialog(rootPane, "Please enter values to ID, Desc,
Price", "Invalid",HEIGHT);
}else{
DefaultTableModel model = (DefaultTableModel) TableItems.getModel();
model.addRow(new Object[] {txtID.getText(),
txtDesc.getText(),txtPrice.getText()});
//add program here to update Orders array
int n=Orders.length;
Orders[n-1][0]=txtID.getText();
Orders[n-1][1]=txtDesc.getText();
Orders[n-1][2]=txtPrice.getText();

// add program here to update total (lblTotal


double newTotal= Double.parseDouble(lblTotal.getText())+
Double.parseDouble(txtPrice.getText());
lblTotal.setText(newTotal+"");

private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {


DefaultTableModel model = (DefaultTableModel) TableItems.getModel();
int numRows = TableItems.getSelectedRows().length;
for(int i=0; i<numRows; i++){
model.removeRow(TableItems.getSelectedRow());

double newTotal= Double.parseDouble(lblTotal.getText())-


String.valueOf(TableItems.getSelectedRow(), numRows);
lblTotal.setText(newTotal+"");
}
}

/**
* @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(tableframe.class.getName()).log(java.util.logging.
Level.SEVERE, null, ex);
} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(tableframe.class.getName()).log(java.util.logging.
Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(tableframe.class.getName()).log(java.util.logging.
Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(tableframe.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 tableframe().setVisible(true);
}
});
}

// Variables declaration - do not modify


private javax.swing.JTable TableItems;
private javax.swing.JButton btnAdd;
private javax.swing.JButton btnDelete;
private javax.swing.JButton btnUpdate;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel lblTotal;
private javax.swing.JTextField txtDesc;
private javax.swing.JTextField txtID;
private javax.swing.JTextField txtPrice;
// End of variables declaration
}

You might also like