// @topic T11716 MVC Demo - Account Balance // @brief Main Window extends JFrame <a href="http://www.c-jump.com/bcc/c257c/c257jsample/junk/wk10_banking/MainWindow.form"><tt>MainWindow.form</tt></a>. NetBeans project: <a href="http://www.c-jump.com/bcc/c257c/c257jsample/junk/Week10_banking.zip"><tt>Week10_banking.zip</tt></a> package banking; public class MainWindow extends javax.swing.JFrame { // data attributes ControllerTransaction controller; // constructors public MainWindow( ControllerTransaction controller ) { this.controller = controller; initComponents(); } //operations public void updateBalance( double balance ) { txtAmount.setText(""+balance); } /** * 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">//GEN-BEGIN:initComponents private void initComponents() { pnlMain = new javax.swing.JPanel(); lblAmount = new javax.swing.JLabel(); txtAmount = new javax.swing.JTextField(); btnDeposit = new javax.swing.JButton(); btnWithdraw = new javax.swing.JButton(); btnBalance = new javax.swing.JButton(); lblStatus = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Banking Demo"); lblAmount.setText("Amount:"); btnDeposit.setText("Deposit"); btnDeposit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnDepositActionPerformed(evt); } }); btnWithdraw.setText("Withdraw"); btnWithdraw.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnWithdrawActionPerformed(evt); } }); btnBalance.setText("Balance"); btnBalance.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnBalanceActionPerformed(evt); } }); javax.swing.GroupLayout pnlMainLayout = new javax.swing.GroupLayout(pnlMain); pnlMain.setLayout(pnlMainLayout); pnlMainLayout.setHorizontalGroup( pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlMainLayout.createSequentialGroup() .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlMainLayout.createSequentialGroup() .addGap(51, 51, 51) .addComponent(lblAmount) .addGap(18, 18, 18) .addComponent(txtAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)) .addGroup(pnlMainLayout.createSequentialGroup() .addGap(30, 30, 30) .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlMainLayout.createSequentialGroup() .addComponent(lblStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)) .addGroup(pnlMainLayout.createSequentialGroup() .addComponent(btnDeposit) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnWithdraw) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE) .addComponent(btnBalance))))) .addContainerGap()) ); pnlMainLayout.setVerticalGroup( pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlMainLayout.createSequentialGroup() .addGap(56, 56, 56) .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblAmount) .addComponent(txtAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(43, 43, 43) .addGroup(pnlMainLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnDeposit) .addComponent(btnWithdraw) .addComponent(btnBalance)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 39, Short.MAX_VALUE) .addComponent(lblStatus) .addGap(24, 24, 24)) ); 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(pnlMain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(108, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(pnlMain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(84, Short.MAX_VALUE)) ); pack(); }// </editor-fold>//GEN-END:initComponents private void btnWithdrawActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnWithdrawActionPerformed String strAmount = txtAmount.getText(); try { // perform basic valdations: double amount = Double.parseDouble(strAmount); if ( amount < 0.01 ) { throw new RuntimeException("positive amount expected"); } controller.makeWithdrawal(amount); lblStatus.setText("Everything is OK"); } catch ( NumberFormatException ex ) { // handle the bad input error lblStatus.setText("bad number format"); } catch ( RuntimeException ex ) { // handle the overdraft error lblStatus.setText(ex.getMessage()); } }//GEN-LAST:event_btnWithdrawActionPerformed private void btnDepositActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDepositActionPerformed // TODO add your handling code here: }//GEN-LAST:event_btnDepositActionPerformed private void btnBalanceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBalanceActionPerformed controller.checkBalance(); }//GEN-LAST:event_btnBalanceActionPerformed /** * @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(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(MainWindow.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() { Account account = new Account( 100.00 ); // hardcoded for demo ControllerTransaction controller = new ControllerTransaction( account ); MainWindow window = new MainWindow( controller ); WindowAccountBalance balanceWindow = new WindowAccountBalance(controller); controller.setMainWindow(window); controller.setBalanceWindow(balanceWindow); window.setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnBalance; private javax.swing.JButton btnDeposit; private javax.swing.JButton btnWithdraw; private javax.swing.JLabel lblAmount; private javax.swing.JLabel lblStatus; private javax.swing.JPanel pnlMain; private javax.swing.JTextField txtAmount; // End of variables declaration//GEN-END:variables }