// TEST CODES FOR USING TABBED PANES /* A JApplet containing two GUI panes having limited processing capabilities, ie, validation/printing only. */ /* To test the codes, save this file as TabbedShopper.java and compile it using the command javac TabbedShopper.java. Crete a html file containing the applet tag and specifying height and width of 400 and 600 respectively. Save the html file as TabbedShopper.html and run it using the command appletviewer TabbedShopper.html. */ import javax.swing.*; import java.awt.*; import java.awt.event.*; // Create a new class public class TabbedShopper extends JApplet implements ActionListener { protected String shopperFirstName, shopperLastName, shopperAddress; protected String shopperCity, shopperState, shopperCountryId; protected String shopperZipCode, shopperPhone; protected String shopperShopperId, shopperPassword, shopperEmailId; protected String shopperCreditCardNo, shopperCreditCardType; protected String shopperExpiryDate; protected String recipientFirstName, recipientLastName, recipientAddress; protected String recipientCity, recipientState, recipientCountryId; protected String recipientZipCode, recipientPhone, recipientOrderNo; int arrayIndex; // For use in reading string arrays JPanel shopperPanel; JPanel recipientPanel; JTabbedPane shopperTabbedPane; GridBagLayout gbl; GridBagConstraints gbc; String [] countryIds = {"USA", "Japan", "Philippines" }; String [] creditCardTypes = { "MasterCard", "Visa", "BPI" }; JLabel labFirstName, labLastName, labAddress; JLabel labCity, labState, labCountryId; JLabel labZipCode, labPhone, labOrderNo; JLabel labShopperId, labPassword, labEmailId; JLabel labCreditCardNo, labCreditCardType, labExpiryDate; JTextField txtShopperFirstName, txtShopperLastName; JTextField txtShopperAddress, txtShopperCity, txtShopperState; JComboBox cmbShopperCountryId; JTextField txtShopperZipCode, txtShopperPhone, txtShopperShopperId; JPasswordField pwdShopperPassword; JTextField txtShopperEmailId, txtShopperCreditCardNo; JComboBox cmbShopperCreditCardType; JTextField txtShopperExpiryDate; JTextField txtRecipientFirstName, txtRecipientLastName; JTextField txtRecipientAddress, txtRecipientCity; JTextField txtRecipientState; JComboBox cmbRecipientCountryId; JTextField txtRecipientZipCode, txtRecipientPhone; JTextField txtRecipientOrderNo; JButton btnShopperAccept, btnShopperCancel; JButton btnRecipientAccept, btnRecipientCancel; public TabbedShopper() // The class constructor { shopperTabbedPane = new JTabbedPane(); getContentPane().add(shopperTabbedPane); createShopperPanel(); // Build a panel createRecipientPanel(); // Build another panel shopperTabbedPane.addTab("Shopper", null, shopperPanel, "Shopper"); shopperTabbedPane.addTab("Recipient", null, recipientPanel, "Recipient"); } public void createShopperPanel() { shopperPanel = new JPanel(); gbl = new GridBagLayout(); gbc = new GridBagConstraints(); shopperPanel.setLayout(gbl); gbc.anchor = GridBagConstraints.NORTHWEST; labFirstName = new JLabel("First Name: "); labLastName = new JLabel("Last Name: "); labAddress = new JLabel("Address: "); labCity = new JLabel("City: "); labState = new JLabel("State: "); labCountryId = new JLabel("Country Id: "); labZipCode = new JLabel("Zip Code: "); labPhone = new JLabel("Phone: "); labShopperId = new JLabel("Shopper Id: "); labPassword = new JLabel("Password: "); labEmailId = new JLabel("Email Id: "); labCreditCardNo = new JLabel("Credit Card No.: "); labCreditCardType = new JLabel("Credit Card Type: "); labExpiryDate = new JLabel("Expiry Date: "); txtShopperFirstName = new JTextField(15); txtShopperLastName = new JTextField(15); txtShopperAddress = new JTextField(20); txtShopperCity = new JTextField(15); txtShopperState = new JTextField(15); cmbShopperCountryId = new JComboBox(countryIds); txtShopperZipCode = new JTextField(10); txtShopperPhone = new JTextField(15); txtShopperShopperId = new JTextField(10); pwdShopperPassword = new JPasswordField(10); txtShopperEmailId = new JTextField(20); txtShopperCreditCardNo = new JTextField(15); cmbShopperCreditCardType = new JComboBox(creditCardTypes); txtShopperExpiryDate = new JTextField(10); btnShopperAccept = new JButton("Accept"); btnShopperCancel = new JButton("Cancel"); gbc.gridx = 0; gbc.gridy = 0; gbl.setConstraints(labFirstName, gbc); shopperPanel.add(labFirstName); gbc.gridx = 1; gbc.gridy = 0; gbl.setConstraints(txtShopperFirstName, gbc); shopperPanel.add(txtShopperFirstName); gbc.gridx = 0; gbc.gridy = 1; gbl.setConstraints(labLastName, gbc); shopperPanel.add(labLastName); gbc.gridx = 1; gbc.gridy = 1; gbl.setConstraints(txtShopperLastName, gbc); shopperPanel.add(txtShopperLastName); gbc.gridx = 0; gbc.gridy = 2; gbl.setConstraints(labAddress, gbc); shopperPanel.add(labAddress); gbc.gridx = 1; gbc.gridy = 2; gbl.setConstraints(txtShopperAddress, gbc); shopperPanel.add(txtShopperAddress); gbc.gridx = 0; gbc.gridy = 3; gbl.setConstraints(labCity, gbc); shopperPanel.add(labCity); gbc.gridx = 1; gbc.gridy = 3; gbl.setConstraints(txtShopperCity, gbc); shopperPanel.add(txtShopperCity); gbc.gridx = 0; gbc.gridy = 4; gbl.setConstraints(labState, gbc); shopperPanel.add(labState); gbc.gridx = 1; gbc.gridy = 4; gbl.setConstraints(txtShopperState, gbc); shopperPanel.add(txtShopperState); gbc.gridx = 0; gbc.gridy = 5; gbl.setConstraints(labCountryId, gbc); shopperPanel.add(labCountryId); gbc.gridx = 1; gbc.gridy = 5; gbl.setConstraints(cmbShopperCountryId, gbc); cmbShopperCountryId.setSelectedIndex(-1); shopperPanel.add(cmbShopperCountryId); gbc.gridx = 0; gbc.gridy = 6; gbl.setConstraints(labZipCode, gbc); shopperPanel.add(labZipCode); gbc.gridx = 1; gbc.gridy = 6; gbl.setConstraints(txtShopperZipCode, gbc); shopperPanel.add(txtShopperZipCode); gbc.gridx = 0; gbc.gridy = 7; gbl.setConstraints(labPhone, gbc); shopperPanel.add(labPhone); gbc.gridx = 1; gbc.gridy = 7; gbl.setConstraints(txtShopperPhone, gbc); shopperPanel.add(txtShopperPhone); gbc.gridx = 0; gbc.gridy = 8; gbl.setConstraints(labShopperId, gbc); shopperPanel.add(labShopperId); gbc.gridx = 1; gbc.gridy = 8; gbl.setConstraints(txtShopperShopperId, gbc); shopperPanel.add(txtShopperShopperId); gbc.gridx = 0; gbc.gridy = 9; gbl.setConstraints(labPassword, gbc); shopperPanel.add(labPassword); gbc.gridx = 1; gbc.gridy = 9; gbl.setConstraints(pwdShopperPassword, gbc); shopperPanel.add(pwdShopperPassword); gbc.gridx = 0; gbc.gridy = 10; gbl.setConstraints(labEmailId, gbc); shopperPanel.add(labEmailId); gbc.gridx = 1; gbc.gridy = 10; gbl.setConstraints(txtShopperEmailId, gbc); shopperPanel.add(txtShopperEmailId); gbc.gridx = 0; gbc.gridy = 11; gbl.setConstraints(labCreditCardNo, gbc); shopperPanel.add(labCreditCardNo); gbc.gridx = 1; gbc.gridy = 11; gbl.setConstraints(txtShopperCreditCardNo, gbc); shopperPanel.add(txtShopperCreditCardNo); gbc.gridx = 0; gbc.gridy = 12; gbl.setConstraints(labCreditCardType, gbc); shopperPanel.add(labCreditCardType); gbc.gridx = 1; gbc.gridy = 12; gbl.setConstraints(cmbShopperCreditCardType, gbc); cmbShopperCreditCardType.setSelectedIndex(-1); shopperPanel.add(cmbShopperCreditCardType); gbc.gridx = 0; gbc.gridy = 13; gbl.setConstraints(labExpiryDate, gbc); shopperPanel.add(labExpiryDate); gbc.gridx = 1; gbc.gridy = 13; gbl.setConstraints(txtShopperExpiryDate, gbc); shopperPanel.add(txtShopperExpiryDate); gbc.gridx = 2; gbc.gridy = 14; gbl.setConstraints(btnShopperAccept, gbc); shopperPanel.add(btnShopperAccept); btnShopperAccept.addActionListener(this); // Watch for events gbc.gridx = 3; gbc.gridy = 14; gbl.setConstraints(btnShopperCancel, gbc); shopperPanel.add(btnShopperCancel); btnShopperCancel.addActionListener(this); // Watch for events } public void createRecipientPanel() { recipientPanel = new JPanel(); recipientPanel.setLayout(gbl); gbc.anchor = GridBagConstraints.NORTHWEST; labFirstName = new JLabel("First Name: "); labLastName = new JLabel("Last Name: "); labAddress = new JLabel("Address: "); labCity = new JLabel("City: "); labState = new JLabel("State: "); labCountryId = new JLabel("Country Id: "); labZipCode = new JLabel("Zip Code: "); labPhone = new JLabel("Phone: "); labOrderNo = new JLabel("Order No: "); txtRecipientFirstName = new JTextField(15); txtRecipientLastName = new JTextField(15); txtRecipientAddress = new JTextField(20); txtRecipientCity = new JTextField(15); txtRecipientState = new JTextField(15); cmbRecipientCountryId = new JComboBox(countryIds); txtRecipientZipCode = new JTextField(10); txtRecipientPhone = new JTextField(15); txtRecipientOrderNo = new JTextField(10); btnRecipientAccept = new JButton("Accept"); btnRecipientCancel = new JButton("Cancel"); gbc.gridx = 0; gbc.gridy = 0; gbl.setConstraints(labFirstName, gbc); recipientPanel.add(labFirstName); gbc.gridx = 1; gbc.gridy = 0; gbl.setConstraints(txtRecipientFirstName, gbc); recipientPanel.add(txtRecipientFirstName); gbc.gridx = 0; gbc.gridy = 1; gbl.setConstraints(labLastName, gbc); recipientPanel.add(labLastName); gbc.gridx = 1; gbc.gridy = 1; gbl.setConstraints(txtRecipientLastName, gbc); recipientPanel.add(txtRecipientLastName); gbc.gridx = 0; gbc.gridy = 2; gbl.setConstraints(labAddress, gbc); recipientPanel.add(labAddress); gbc.gridx = 1; gbc.gridy = 2; gbl.setConstraints(txtRecipientAddress, gbc); recipientPanel.add(txtRecipientAddress); gbc.gridx = 0; gbc.gridy = 3; gbl.setConstraints(labCity, gbc); recipientPanel.add(labCity); gbc.gridx = 1; gbc.gridy = 3; gbl.setConstraints(txtRecipientCity, gbc); recipientPanel.add(txtRecipientCity); gbc.gridx = 0; gbc.gridy = 4; gbl.setConstraints(labState, gbc); recipientPanel.add(labState); gbc.gridx = 1; gbc.gridy = 4; gbl.setConstraints(txtRecipientState, gbc); recipientPanel.add(txtRecipientState); gbc.gridx = 0; gbc.gridy = 5; gbl.setConstraints(labCountryId, gbc); recipientPanel.add(labCountryId); gbc.gridx = 1; gbc.gridy = 5; gbl.setConstraints(cmbRecipientCountryId, gbc); cmbRecipientCountryId.setSelectedIndex(-1); recipientPanel.add(cmbRecipientCountryId); gbc.gridx = 0; gbc.gridy = 6; gbl.setConstraints(labZipCode, gbc); recipientPanel.add(labZipCode); gbc.gridx = 1; gbc.gridy = 6; gbl.setConstraints(txtRecipientZipCode, gbc); recipientPanel.add(txtRecipientZipCode); gbc.gridx = 0; gbc.gridy = 7; gbl.setConstraints(labPhone, gbc); recipientPanel.add(labPhone); gbc.gridx = 1; gbc.gridy = 7; gbl.setConstraints(txtRecipientPhone, gbc); recipientPanel.add(txtRecipientPhone); gbc.gridx = 0; gbc.gridy = 8; gbl.setConstraints(labOrderNo, gbc); recipientPanel.add(labOrderNo); gbc.gridx = 1; gbc.gridy = 8; gbl.setConstraints(txtRecipientOrderNo, gbc); recipientPanel.add(txtRecipientOrderNo); gbc.gridx = 2; gbc.gridy = 9; gbl.setConstraints(btnRecipientAccept, gbc); recipientPanel.add(btnRecipientAccept); btnRecipientAccept.addActionListener(this); // Watch for events gbc.gridx = 3; gbc.gridy = 9; gbl.setConstraints(btnRecipientCancel, gbc); recipientPanel.add(btnRecipientCancel); btnRecipientCancel.addActionListener(this); // Watch for events } // Methods, some for one panel, others for the other panel public String getShopperCountryId() { arrayIndex = cmbShopperCountryId.getSelectedIndex(); String country = countryIds [arrayIndex]; return country; } public String getRecipientCountryId() { arrayIndex = cmbRecipientCountryId.getSelectedIndex(); String country = countryIds [arrayIndex]; return country; } public String getCreditCardType() { arrayIndex = cmbShopperCreditCardType.getSelectedIndex(); String creditCard = creditCardTypes [arrayIndex]; return creditCard; } public void getShopperDetails() { shopperFirstName = txtShopperFirstName.getText(); shopperLastName = txtShopperLastName.getText(); shopperAddress = txtShopperAddress.getText(); shopperCity = txtShopperCity.getText(); shopperState = txtShopperState.getText(); shopperCountryId = getShopperCountryId(); shopperZipCode = txtShopperZipCode.getText(); shopperPhone = txtShopperPhone.getText(); shopperShopperId = txtShopperShopperId.getText(); shopperPassword = pwdShopperPassword.getText(); shopperEmailId = txtShopperEmailId.getText(); shopperCreditCardNo = txtShopperCreditCardNo.getText(); shopperCreditCardType = getCreditCardType(); shopperExpiryDate = txtShopperExpiryDate.getText(); } public void getRecipientDetails() { recipientFirstName = txtRecipientFirstName.getText(); recipientLastName = txtRecipientLastName.getText(); recipientAddress = txtRecipientAddress.getText(); recipientCity = txtRecipientCity.getText(); recipientState = txtRecipientState.getText(); recipientCountryId = getRecipientCountryId(); recipientZipCode = txtRecipientZipCode.getText(); recipientPhone = txtRecipientPhone.getText(); recipientOrderNo = txtRecipientOrderNo.getText(); } public void printShopperDetails() { System.out.println("\nFirst Name: " + shopperFirstName); System.out.println("Last Name: " + shopperLastName); System.out.println("Address: " + shopperAddress); System.out.println("City: " + shopperCity); System.out.println("State: " + shopperState); System.out.println("Country Id: " + shopperCountryId); System.out.println("Zip Code: " + shopperZipCode); System.out.println("Phone: " + shopperPhone); System.out.println("Shopper Id: " + shopperShopperId); System.out.println("Password: " + shopperPassword); System.out.println("Email Id: " + shopperEmailId); System.out.println("Credit Card No.: " + shopperCreditCardNo); System.out.println("Credit Card Type: " + shopperCreditCardType); System.out.println("Expiry Date: " + shopperExpiryDate); } public void printRecipientDetails() { System.out.println("\nFirst Name: " + recipientFirstName); System.out.println("Last Name: " + recipientLastName); System.out.println("Address: " + recipientAddress); System.out.println("City: " + recipientCity); System.out.println("State: " + recipientState); System.out.println("Country Id: " + recipientCountryId); System.out.println("Zip Code: " + recipientZipCode); System.out.println("Phone: " + recipientPhone); System.out.println("Order No.: " + recipientOrderNo); } public void resetShopper() { txtShopperFirstName.setText(""); txtShopperLastName.setText(""); txtShopperAddress.setText(""); txtShopperCity.setText(""); txtShopperState.setText(""); cmbShopperCountryId.setSelectedIndex(-1); txtShopperZipCode.setText(""); txtShopperPhone.setText(""); txtShopperShopperId.setText(""); pwdShopperPassword.setText(""); txtShopperEmailId.setText(""); txtShopperCreditCardNo.setText(""); cmbShopperCreditCardType.setSelectedIndex(-1); txtShopperExpiryDate.setText(""); getAppletContext().showStatus(""); } public void resetRecipient() { txtRecipientFirstName.setText(""); txtRecipientLastName.setText(""); txtRecipientAddress.setText(""); txtRecipientCity.setText(""); txtRecipientState.setText(""); cmbRecipientCountryId.setSelectedIndex(-1); txtRecipientZipCode.setText(""); txtRecipientPhone.setText(""); txtRecipientOrderNo.setText(""); getAppletContext().showStatus(""); } public void showError(String msg) { getAppletContext().showStatus(msg); } public boolean shopperHasNoEmptyFields() { String tmp; tmp = txtShopperFirstName.getText(); if(tmp.length() == 0) { showError("First Name cannot be empty."); return false; } tmp = txtShopperLastName.getText(); if(tmp.length() == 0) { showError("Last Name cannot be empty."); return false; } tmp = txtShopperAddress.getText(); if(tmp.length() == 0) { showError("Address cannot be empty."); return false; } tmp = txtShopperCity.getText(); if(tmp.length() == 0) { showError("City cannot be empty."); return false; } tmp = txtShopperState.getText(); if(tmp.length() == 0) { showError("State cannot be empty."); return false; } if(cmbShopperCountryId.getSelectedIndex() == -1 ) { showError("A Country should be chosen." ); return false; } tmp = txtShopperZipCode.getText(); if(tmp.length() == 0) { showError("Zip Code cannot be empty."); return false; } tmp = txtShopperPhone.getText(); if(tmp.length() == 0) { showError("Phone cannot be empty."); return false; } tmp = txtShopperShopperId.getText(); if(tmp.length() == 0) { showError("Shopper Id cannot be empty."); return false; } tmp = pwdShopperPassword.getText(); if(tmp.length() == 0) { showError("Password cannot be empty."); return false; } tmp = txtShopperEmailId.getText(); if(tmp.length() == 0) { showError("Email Id cannot be empty."); return false; } tmp = txtShopperCreditCardNo.getText(); if(tmp.length() == 0) { showError("Credit Card No cannot be empty."); return false; } if(cmbShopperCreditCardType.getSelectedIndex() == -1 ) { showError("A Credit Card Type should be chosen." ); return false; } tmp = txtShopperExpiryDate.getText(); if(tmp.length() == 0) { showError("Expiry Date cannot be empty."); return false; } return true; } public boolean recipientHasNoEmptyFields() { String tmp; tmp = txtRecipientFirstName.getText(); if(tmp.length() == 0) { showError("First Name cannot be empty."); return false; } tmp = txtRecipientLastName.getText(); if(tmp.length() == 0) { showError("Last Name cannot be empty."); return false; } tmp = txtRecipientAddress.getText(); if(tmp.length() == 0) { showError("Address cannot be empty."); return false; } tmp = txtRecipientCity.getText(); if(tmp.length() == 0) { showError("City cannot be empty."); return false; } tmp = txtRecipientState.getText(); if(tmp.length() == 0) { showError("State cannot be empty."); return false; } if(cmbRecipientCountryId.getSelectedIndex() == -1 ) { showError("A Country should be chosen." ); return false; } tmp = txtRecipientZipCode.getText(); if(tmp.length() == 0) { showError("Zip Code cannot be empty."); return false; } tmp = txtRecipientPhone.getText(); if(tmp.length() == 0) { showError("Phone cannot be empty."); return false; } tmp = txtRecipientOrderNo.getText(); if(tmp.length() == 0) { showError("Order No cannot be empty."); return false; } return true; } public void actionPerformed(ActionEvent e) // Event handler { if(e.getSource() == btnShopperAccept) { if(shopperHasNoEmptyFields()) { getShopperDetails(); printShopperDetails(); } } else if(e.getSource() == btnShopperCancel) { resetShopper(); } if(e.getSource() == btnRecipientAccept) { if(recipientHasNoEmptyFields()) { getRecipientDetails(); printRecipientDetails(); } } else if(e.getSource() == btnRecipientCancel) { resetRecipient(); } } public void init() { TabbedShopper myTabbedShopper = new TabbedShopper(); } }