// @topic T11710 MVC Demo - Food Order // @brief Main Window extends JFrame <a href="http://www.c-jump.com/bcc/c257c/c257jsample/junk/wk11_mvc/WindowMenu.form"><tt>WindowMenu.form</tt></a> package mvc_demo; public class WindowMenu extends javax.swing.JFrame { //-------------------------------------- // data attributes //-------------------------------------- private ControllerMenu controller; private javax.swing.JRadioButton[] rdoButtons = new javax.swing.JRadioButton[4]; //-------------------------------------- // constructors //-------------------------------------- /** * Creates new form WindowMenu */ public WindowMenu( ControllerMenu controller ) { this.controller = controller; initComponents(); rdoButtons[0] = rdoBreakfast; rdoButtons[1] = rdoLunch; rdoButtons[2] = rdoSnack; rdoButtons[3] = rdoDinner; // calculate time of day and select // appropriate menu item // pretend we decided to activate lunch: int idx = 1; rdoButtons[ idx ].setSelected(true); }//WindowMenu //-------------------------------------- // operations //-------------------------------------- /** * 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() { grpMenu = new javax.swing.ButtonGroup(); pnlContainer = new javax.swing.JPanel(); rdoBreakfast = new javax.swing.JRadioButton(); rdoLunch = new javax.swing.JRadioButton(); rdoSnack = new javax.swing.JRadioButton(); rdoDinner = new javax.swing.JRadioButton(); btnOrder = new javax.swing.JButton(); lblOrderInfo = new javax.swing.JLabel(); btnShowOrders = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("Place your order"); grpMenu.add(rdoBreakfast); rdoBreakfast.setText("Breakfast"); grpMenu.add(rdoLunch); rdoLunch.setText("Lunch"); grpMenu.add(rdoSnack); rdoSnack.setText("Snack"); grpMenu.add(rdoDinner); rdoDinner.setText("Dinner"); btnOrder.setText("Order"); btnOrder.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnOrderActionPerformed(evt); } }); lblOrderInfo.setText("Order Info"); btnShowOrders.setText("Show Orders"); btnShowOrders.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnShowOrdersActionPerformed(evt); } }); javax.swing.GroupLayout pnlContainerLayout = new javax.swing.GroupLayout(pnlContainer); pnlContainer.setLayout(pnlContainerLayout); pnlContainerLayout.setHorizontalGroup( pnlContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlContainerLayout.createSequentialGroup() .addGroup(pnlContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlContainerLayout.createSequentialGroup() .addGap(17, 17, 17) .addGroup(pnlContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(rdoSnack) .addComponent(rdoLunch) .addComponent(rdoBreakfast) .addGroup(pnlContainerLayout.createSequentialGroup() .addComponent(rdoDinner) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 117, Short.MAX_VALUE) .addComponent(btnShowOrders)))) .addGroup(pnlContainerLayout.createSequentialGroup() .addGap(56, 56, 56) .addComponent(btnOrder) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(lblOrderInfo))) .addGap(23, 23, 23)) ); pnlContainerLayout.setVerticalGroup( pnlContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlContainerLayout.createSequentialGroup() .addGap(15, 15, 15) .addComponent(rdoBreakfast) .addGap(18, 18, 18) .addComponent(rdoLunch) .addGap(18, 18, 18) .addComponent(rdoSnack) .addGroup(pnlContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(pnlContainerLayout.createSequentialGroup() .addGap(18, 18, 18) .addComponent(rdoDinner) .addGap(18, 18, 18) .addGroup(pnlContainerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(btnOrder) .addComponent(lblOrderInfo)) .addContainerGap(49, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlContainerLayout.createSequentialGroup() .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnShowOrders) .addGap(47, 47, 47)))) ); 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(pnlContainer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(81, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(pnlContainer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(38, Short.MAX_VALUE)) ); pack(); }// </editor-fold>//GEN-END:initComponents private void btnOrderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOrderActionPerformed for ( javax.swing.JRadioButton button : rdoButtons ) { if ( button.isSelected() ) { int idx = radioButtonToIndex( button ); lblOrderInfo.setText("Order " + idx + " placed"); // store this index in history controller.placeOrder(idx); } } }//GEN-LAST:event_btnOrderActionPerformed private void btnShowOrdersActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnShowOrdersActionPerformed controller.showOrders(); }//GEN-LAST:event_btnShowOrdersActionPerformed int radioButtonToIndex( javax.swing.JRadioButton button ) { for ( int idx = 0 ; idx < rdoButtons.length; ++idx ) { if ( rdoButtons[ idx ] == button ) return idx; } return -1; } /** * @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(WindowMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(WindowMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(WindowMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(WindowMenu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> ControllerMenu controller = new ControllerMenu(); /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { // this code runs on Swing // Event Dispatch Thread (EDT): WindowOrders windowOrders = new WindowOrders(controller); WindowMenu window = new WindowMenu( controller ); controller.setWindowMenu(window); controller.setWindowOrders(windowOrders); window.setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnOrder; private javax.swing.JButton btnShowOrders; private javax.swing.ButtonGroup grpMenu; private javax.swing.JLabel lblOrderInfo; private javax.swing.JPanel pnlContainer; private javax.swing.JRadioButton rdoBreakfast; private javax.swing.JRadioButton rdoDinner; private javax.swing.JRadioButton rdoLunch; private javax.swing.JRadioButton rdoSnack; // End of variables declaration//GEN-END:variables }