// TEST CODES FOR CREATING A TABBED PANE /* This test code creates a swing applet containing a tabbed pane to which two GUI panels are added. */ /* To test the program, compile this code using the command javac TabbedShopper.java. Then, create a html file containing the applet tag and specifying "TabbedShopper.class" as code and using height and width of 400 and 600 respectively. Save the file as TabbedShopper.html and run it using the command appletviewer TabbedShopper.html. */ import javax.swing.*; import java.awt.*; public class TabbedShopper extends JApplet { JPanel shopperPanel; JPanel recipientPanel; JTabbedPane shopperTabbedPane; GridBagLayout gbl; GridBagConstraints gbc; public TabbedShopper() { // Make new tabbed pane and add it to content pane shopperTabbedPane = new JTabbedPane(); getContentPane().add(shopperTabbedPane); // Call methods to build two panels createShopperPanel(); createRecipientPanel(); // Add two panels to tabbed pane shopperTabbedPane.addTab("Shopper", null, shopperPanel, "Shopper"); shopperTabbedPane.addTab("Recipient", null, recipientPanel, "Recipient"); } public void createShopperPanel() // Build one panel { shopperPanel = new JPanel(); gbl = new GridBagLayout(); gbc = new GridBagConstraints(); shopperPanel.setLayout(gbl); gbc.anchor = GridBagConstraints.NORTHWEST; String [] countryIds = {"USA", "Japan", "Philippines" }; String [] creditCardType = { "MasterCard", "Visa", "BPI" }; JLabel labFirstName, labLastName, labAddress; JLabel labCity, labState, labCountryId; JLabel labZipCode, labPhone; JLabel labShopperId, labPassword, labEmailId; JLabel labCreditCardNo, labCreditCardType, labExpiryDate; JTextField txtFirstName, txtLastName; JTextField txtAddress, txtCity, txtState; JComboBox cmbCountryId; JTextField txtZipCode, txtPhone, txtShopperId; JPasswordField pwdPassword; JTextField txtEmailId, txtCreditCardNo; JComboBox cmbCreditCardType; JTextField txtExpiryDate; JButton butAccept, butCancel; 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: "); txtFirstName = new JTextField(15); txtLastName = new JTextField(15); txtAddress = new JTextField(20); txtCity = new JTextField(15); txtState = new JTextField(15); cmbCountryId = new JComboBox(countryIds); txtZipCode = new JTextField(10); txtPhone = new JTextField(15); txtShopperId = new JTextField(10); pwdPassword = new JPasswordField(10); txtEmailId = new JTextField(20); txtCreditCardNo = new JTextField(15); cmbCreditCardType = new JComboBox(creditCardType); txtExpiryDate = new JTextField(10); butAccept = new JButton("Accept"); butCancel = new JButton("Cancel"); gbc.gridx = 0; gbc.gridy = 0; gbl.setConstraints(labFirstName, gbc); shopperPanel.add(labFirstName); gbc.gridx = 1; gbc.gridy = 0; gbl.setConstraints(txtFirstName, gbc); shopperPanel.add(txtFirstName); gbc.gridx = 0; gbc.gridy = 1; gbl.setConstraints(labLastName, gbc); shopperPanel.add(labLastName); gbc.gridx = 1; gbc.gridy = 1; gbl.setConstraints(txtLastName, gbc); shopperPanel.add(txtLastName); gbc.gridx = 0; gbc.gridy = 2; gbl.setConstraints(labAddress, gbc); shopperPanel.add(labAddress); gbc.gridx = 1; gbc.gridy = 2; gbl.setConstraints(txtAddress, gbc); shopperPanel.add(txtAddress); gbc.gridx = 0; gbc.gridy = 3; gbl.setConstraints(labCity, gbc); shopperPanel.add(labCity); gbc.gridx = 1; gbc.gridy = 3; gbl.setConstraints(txtCity, gbc); shopperPanel.add(txtCity); gbc.gridx = 0; gbc.gridy = 4; gbl.setConstraints(labState, gbc); shopperPanel.add(labState); gbc.gridx = 1; gbc.gridy = 4; gbl.setConstraints(txtState, gbc); shopperPanel.add(txtState); gbc.gridx = 0; gbc.gridy = 5; gbl.setConstraints(labCountryId, gbc); shopperPanel.add(labCountryId); gbc.gridx = 1; gbc.gridy = 5; gbl.setConstraints(cmbCountryId, gbc); cmbCountryId.setSelectedIndex(-1); shopperPanel.add(cmbCountryId); gbc.gridx = 0; gbc.gridy = 6; gbl.setConstraints(labZipCode, gbc); shopperPanel.add(labZipCode); gbc.gridx = 1; gbc.gridy = 6; gbl.setConstraints(txtZipCode, gbc); shopperPanel.add(txtZipCode); gbc.gridx = 0; gbc.gridy = 7; gbl.setConstraints(labPhone, gbc); shopperPanel.add(labPhone); gbc.gridx = 1; gbc.gridy = 7; gbl.setConstraints(txtPhone, gbc); shopperPanel.add(txtPhone); gbc.gridx = 0; gbc.gridy = 8; gbl.setConstraints(labShopperId, gbc); shopperPanel.add(labShopperId); gbc.gridx = 1; gbc.gridy = 8; gbl.setConstraints(txtShopperId, gbc); shopperPanel.add(txtShopperId); gbc.gridx = 0; gbc.gridy = 9; gbl.setConstraints(labPassword, gbc); shopperPanel.add(labPassword); gbc.gridx = 1; gbc.gridy = 9; gbl.setConstraints(pwdPassword, gbc); shopperPanel.add(pwdPassword); gbc.gridx = 0; gbc.gridy = 10; gbl.setConstraints(labEmailId, gbc); shopperPanel.add(labEmailId); gbc.gridx = 1; gbc.gridy = 10; gbl.setConstraints(txtEmailId, gbc); shopperPanel.add(txtEmailId); gbc.gridx = 0; gbc.gridy = 11; gbl.setConstraints(labCreditCardNo, gbc); shopperPanel.add(labCreditCardNo); gbc.gridx = 1; gbc.gridy = 11; gbl.setConstraints(txtCreditCardNo, gbc); shopperPanel.add(txtCreditCardNo); gbc.gridx = 0; gbc.gridy = 12; gbl.setConstraints(labCreditCardType, gbc); shopperPanel.add(labCreditCardType); gbc.gridx = 1; gbc.gridy = 12; gbl.setConstraints(cmbCreditCardType, gbc); cmbCreditCardType.setSelectedIndex(-1); shopperPanel.add(cmbCreditCardType); gbc.gridx = 0; gbc.gridy = 13; gbl.setConstraints(labExpiryDate, gbc); shopperPanel.add(labExpiryDate); gbc.gridx = 1; gbc.gridy = 13; gbl.setConstraints(txtExpiryDate, gbc); shopperPanel.add(txtExpiryDate); gbc.gridx = 2; gbc.gridy = 14; gbl.setConstraints(butAccept, gbc); shopperPanel.add(butAccept); gbc.gridx = 3; gbc.gridy = 14; gbl.setConstraints(butCancel, gbc); shopperPanel.add(butCancel); } public void createRecipientPanel() // Build another panel { gbl = new GridBagLayout(); gbc = new GridBagConstraints(); recipientPanel = new JPanel(); recipientPanel.setLayout(gbl); gbc.anchor = GridBagConstraints.NORTHWEST; String [] countryIds = {"USA", "Japan", "Philippines" }; JLabel labFirstName, labLastName, labAddress; JLabel labCity, labState, labCountryId; JLabel labZipCode, labPhone, labOrderNo; JTextField txtFirstName, txtLastName; JTextField txtAddress, txtCity, txtState; JComboBox cmbCountryId; JTextField txtZipCode, txtPhone, txtOrderNo; JButton butAccept, butCancel; 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: "); txtFirstName = new JTextField(15); txtLastName = new JTextField(15); txtAddress = new JTextField(20); txtCity = new JTextField(15); txtState = new JTextField(15); cmbCountryId = new JComboBox(countryIds); txtZipCode = new JTextField(10); txtPhone = new JTextField(15); txtOrderNo = new JTextField(10); butAccept = new JButton("Accept"); butCancel = new JButton("Cancel"); gbc.gridx = 0; gbc.gridy = 0; gbl.setConstraints(labFirstName, gbc); recipientPanel.add(labFirstName); gbc.gridx = 1; gbc.gridy = 0; gbl.setConstraints(txtFirstName, gbc); recipientPanel.add(txtFirstName); gbc.gridx = 0; gbc.gridy = 1; gbl.setConstraints(labLastName, gbc); recipientPanel.add(labLastName); gbc.gridx = 1; gbc.gridy = 1; gbl.setConstraints(txtLastName, gbc); recipientPanel.add(txtLastName); gbc.gridx = 0; gbc.gridy = 2; gbl.setConstraints(labAddress, gbc); recipientPanel.add(labAddress); gbc.gridx = 1; gbc.gridy = 2; gbl.setConstraints(txtAddress, gbc); recipientPanel.add(txtAddress); gbc.gridx = 0; gbc.gridy = 3; gbl.setConstraints(labCity, gbc); recipientPanel.add(labCity); gbc.gridx = 1; gbc.gridy = 3; gbl.setConstraints(txtCity, gbc); recipientPanel.add(txtCity); gbc.gridx = 0; gbc.gridy = 4; gbl.setConstraints(labState, gbc); recipientPanel.add(labState); gbc.gridx = 1; gbc.gridy = 4; gbl.setConstraints(txtState, gbc); recipientPanel.add(txtState); gbc.gridx = 0; gbc.gridy = 5; gbl.setConstraints(labCountryId, gbc); recipientPanel.add(labCountryId); gbc.gridx = 1; gbc.gridy = 5; gbl.setConstraints(cmbCountryId, gbc); cmbCountryId.setSelectedIndex(-1); recipientPanel.add(cmbCountryId); gbc.gridx = 0; gbc.gridy = 6; gbl.setConstraints(labZipCode, gbc); recipientPanel.add(labZipCode); gbc.gridx = 1; gbc.gridy = 6; gbl.setConstraints(txtZipCode, gbc); recipientPanel.add(txtZipCode); gbc.gridx = 0; gbc.gridy = 7; gbl.setConstraints(labPhone, gbc); recipientPanel.add(labPhone); gbc.gridx = 1; gbc.gridy = 7; gbl.setConstraints(txtPhone, gbc); recipientPanel.add(txtPhone); gbc.gridx = 0; gbc.gridy = 8; gbl.setConstraints(labOrderNo, gbc); recipientPanel.add(labOrderNo); gbc.gridx = 1; gbc.gridy = 8; gbl.setConstraints(txtOrderNo, gbc); recipientPanel.add(txtOrderNo); gbc.gridx = 2; gbc.gridy = 9; gbl.setConstraints(butAccept, gbc); recipientPanel.add(butAccept); gbc.gridx = 3; gbc.gridy = 9; gbl.setConstraints(butCancel, gbc); recipientPanel.add(butCancel); } public void init() // Override init method { TabbedShopper myTabbedShopper = new TabbedShopper(); } } /* To add another panel, define a new panel immediately ater class declaration statement, then create a new method to build the whole panel. Instantiate the panel inside this method, specify its layout, and add the fields into the panel. Then add the method inside the class constructor. */