Hello, I\'m supposed to create a JApplet application with a five classes: 1 Regi
ID: 3677150 • Letter: H
Question
Hello, I'm supposed to create a JApplet application with a five classes: 1 Registration Panel, 2 Workshop Panel, 3 GUI Panel, 4 Event Handler Panel, 5 Conference Client to initialize the program. these are the classes i have so far they compile but will once initialized i have a blank applet any help would be appreciated thanks.
Registration Panel
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.text.DecimalFormat;
public class RegPanel extends JPanel
{
//instance variables
protected final String [] REGISTRATION_TYPE = {"Please select a type",
"Business",
"Student",
"Complimentary"};
protected final double [] REGISTRATION_COST = {0, 895, 295, 0};
protected final int KEY_NOTE_COST = 30;
protected String typeOfRegistration;
protected ConferenceGUI gui;
protected JTextField regNameTextField;
protected JComboBox typeRegComboBox;
protected JCheckBox keyNoteCheckBox;
//Constructor
public RegPanel()
{
//sets gridlayout
setLayout(new GridLayout(2, 1));
JTextField regNameTextField = new JTextField(20);
add(regNameTextField);
//creates new combo box
typeRegComboBox = new JComboBox();
String selection = typeRegComboBox.getSelectedItem().toString();
int index = typeRegComboBox.getSelectedIndex();
JCheckBox keyNoteCheckBox = new JCheckBox("Dinner and Keynote Speech", false);
add(keyNoteCheckBox);
//sets window title
setBorder(BorderFactory.createTitledBorder("Registrant's Name and Type"));
}
public double getRegistrationCost()
{
double cost = 0;
int index = typeRegComboBox.getSelectedIndex();
switch (index)
{
case 1:
cost = 895;
break;
case 2:
cost = 295;
break;
case 3:
cost = 0;
break;
}
return cost;
}
public double getKeyNoteCost()
{
return KEY_NOTE_COST;
}
public String getRegType()
{
String typeOfRegistration = typeRegComboBox.getSelectedItem().toString();
return typeOfRegistration;
}
}
Workshop Panel class
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.text.DecimalFormat;
public class WorkshopPanel extends JPanel
{
//instance variables
protected final String [] WORKSHOP_TYPE = {"IT Trends in Manitoba",
"Creating a Dream Career",
"Advanced Java Programming",
"Ethics: The Challenge Continues"};
protected final double [] WORKSHOP_COST ={295, 295, 395, 395};
protected ConferenceGUI gui;
protected JList listOptions;
protected DefaultListModel listModel;
protected JScrollPane listPane;
//Constructor
public WorkshopPanel()
{
setLayout(new FlowLayout());
//creates Jlist
listModel = new DefaultListModel();
JList listOptions = new JList(listModel);
for(int i =0; i < WORKSHOP_TYPE.length; i++)
{
listModel.addElement(WORKSHOP_TYPE[i]);
}
listOptions.setVisibleRowCount(4);
listOptions.setSelectionMode(2);
listPane = new JScrollPane(listOptions);
//sets window title
setBorder(BorderFactory.createTitledBorder("Workshops"));
}
public double getWorkshopCost()
{
double workshopCost =0;
int [] workshopArray = listOptions.getSelectedIndices();
for(int i = 0; i < workshopArray.length; i++)
{
workshopCost = WORKSHOP_COST[i];
}
return workshopCost;
}
public String getWorkshopList()
{
String workshopList = "";
int [] workshopListArray = listOptions.getSelectedIndices();
for(int i = 0; i < workshopListArray.length; i++)
{
workshopList = WORKSHOP_TYPE[i];
}
return workshopList;
}
}
GUI Class
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class ConferenceGUI extends JPanel
{
// instance variables
protected JPanel conference;
protected RegPanel regPanel;
protected WorkshopPanel workshopPanel;
protected JPanel titlePanel,
buttonPanel;
protected JButton calcButton,
clearButton;
protected JTextArea textArea;
public ConferenceGUI()
{
//Create new JPanel
conference = new JPanel();
conference.setLayout(new BorderLayout());
//create a RegPanel Panel
regPanel = new RegPanel();
//new workshopPanel
workshopPanel = new WorkshopPanel();
conference.add(titlePanel, BorderLayout.NORTH);
conference.add(regPanel, BorderLayout.WEST);
conference.add(workshopPanel, BorderLayout.EAST);
conference.add(buttonPanel, BorderLayout.SOUTH);
//listen to combo box
ConferenceHandler listenerHandler = new ConferenceHandler(this);
regPanel.typeRegComboBox.addItemListener(listenerHandler);
//add focus listener
regPanel.regNameTextField.addFocusListener(listenerHandler);
//build button panel and title panel
buildButtonPanel();
buildTitlePanel();
}
private void buildTitlePanel()
{
setLayout(new FlowLayout());
//create a title panel
titlePanel = new JPanel(new FlowLayout());
//sets font
titlePanel.setFont(new Font("sansserif", Font.BOLD, 18));
//add title label
titlePanel.add(new Label("Select Registration Options"));
}
private void buildButtonPanel()
{
buttonPanel = new JPanel(new FlowLayout());
//create buttons
calcButton = new JButton("Calculate Charges");
clearButton = new JButton("Clear");
//listeners added to buttons
ConferenceHandler actionHandler = new ConferenceHandler(this);
calcButton.addActionListener(actionHandler);
clearButton.addActionListener(actionHandler);
//text area
JTextArea textArea = new JTextArea(5, 30);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
//add all buttons to Panel
buttonPanel.add(calcButton);
buttonPanel.add(clearButton);
buttonPanel.add(new JScrollPane(this.textArea));
}
}
Event Handler class
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
import java.text.DecimalFormat;
public class ConferenceHandler implements ActionListener, FocusListener, ItemListener
{
//instance variables
protected ConferenceGUI gui;
//Constructor
public ConferenceHandler(ConferenceGUI gui)
{
this.gui = gui;
}
private double getCalcTotalCharges()
{
//variables
double calcTotalCharges,
regPanelCharges,
regPanelKeyNote,
workshopPanelCharges;
regPanelCharges = gui.regPanel.getRegistrationCost();
regPanelKeyNote = gui.regPanel.getKeyNoteCost();
workshopPanelCharges = gui.workshopPanel.getWorkshopCost();
if(gui.regPanel.keyNoteCheckBox.isSelected())
{
//total charges with keynote
calcTotalCharges = regPanelCharges + regPanelKeyNote + workshopPanelCharges;
}
else
{
//total charges without keynote
calcTotalCharges = regPanelCharges + workshopPanelCharges;
}
return calcTotalCharges;
}
public void actionPerformed(ActionEvent one)
{
if(one.getSource() == gui.calcButton)
{
String name = gui.regPanel.regNameTextField.getText();
DecimalFormat money = new DecimalFormat("$#,##0.00");
//Displays error
if(gui.regPanel.getRegType()== "Please select a type")
{
JOptionPane.showMessageDialog(null, "Please select a registraion type",
"Type Error",JOptionPane.ERROR_MESSAGE);
}
//prints to text area about keynote attendance of registrant
if(gui.regPanel.keyNoteCheckBox.isSelected())
{
gui.textArea.setText("Keynote address will be attended");
}
else
{
gui.textArea.setText("Keynote addresss will not be attended");
}
//prints to text area which workshop registrant will attend
gui.textArea.setText(name + " is registered in these workshops:" +
gui.workshopPanel.getWorkshopList());
//prints total charges for registration
gui.textArea.setText("Total charges for " + name + " are " + money.format(getCalcTotalCharges()));
}
else if(one.getSource() == gui.clearButton)
{
//clears text area, resets list and reset combo box
gui.textArea.setText("");
gui.workshopPanel.listOptions.setSelectedIndex(0);
gui.regPanel.typeRegComboBox.setSelectedIndex(0);
}
}
public void itemStateChanged(ItemEvent one)
{
String name = gui.regPanel.regNameTextField.getText();
String type = gui.regPanel.getRegType();
if(one.getSource() == gui.regPanel.typeRegComboBox)
{
gui.textArea.setText(name + " is a " + type + "registration");
}
}
public void focusLost(FocusEvent one)
{
if(one.getSource() == gui.regPanel.regNameTextField)
{
//user must enter name
if(gui.regPanel.regNameTextField.getText().length() == 0)
{
gui.regPanel.regNameTextField.setText("Please enter a name");
gui.regPanel.regNameTextField.requestFocus();
}
else
{
gui.regPanel.regNameTextField.setText("");
}
}
}
public void focusGained(FocusEvent one)
{
//selects text of the appropriate textfield
gui.regPanel.regNameTextField.selectAll();
}
}
Conference Client
import javax.swing.*;
import java.awt.*;
public class ConferenceClient extends JApplet
{
public void init()
{
setLayout(new BorderLayout());
add(new ConferenceGUI(), BorderLayout.CENTER);
}
}
Explanation / Answer
RegPanel.java
import java.awt.*;
import javax.swing.*;
public class RegPanel extends JPanel
{
//final variables containing the cost of each registration type
private final double BUSINESS_REG = 895.00;
private final double STUDENT_REG = 495.00;
private final double COMPLEMENT_REG = 0.00;
//final variable containing the cost of the dinner keynote speech
private final double DINNER_KEYNOTE_SPEECH = 30.00;
//registrant costs assembled as an array.
//On the class diagram, this was called WORKSHOP_COSTS but I felt that wasn't an accurate name
private final double[] REGISTRANT_COSTS = {BUSINESS_REG,
STUDENT_REG,
COMPLEMENT_REG};
//string array used in the creation of the JComboBox
//on the class diagram this was called WORKSHOPS, but I felt that wasn't an accurate name
private final String[] REGISTRANT_TYPES = {"Please select a type",
"Business",
"Student",
"Complimentary"};
//storage variables for objects used to create the registrant panel object
protected JPanel registrantPanel;
protected JPanel dinnerKeynotePanel;
protected JTextField txtName;
protected JComboBox<String> typesBox;
protected JCheckBox chkKeynote;
//string literals for text used in the GUI elements
private final String DINNER_CHECK = "Dinner and Keynote Speech";
private final String BORDER = "Registrant's Name & Type";
public RegPanel()
{
//sets the layout to a 2x1 grid layout
this.setLayout(new GridLayout(2,1));
//builds the registrantPanel and then adds it to the RegPanel
registrantPanel = buildRegistrantPanel();
add(registrantPanel);
//builds the dinnerKeynotePanel and then adds it to the Regpanel
dinnerKeynotePanel = buildDinnerKeynotePanel();
add(dinnerKeynotePanel);
}
private JPanel buildRegistrantPanel()
{
//variable for temporary registrantJPanel that will be returned
JPanel registrantPanel;
//instantiates the registrantPanel
registrantPanel = new JPanel();
//sets its layout to a flow layout
registrantPanel.setLayout(new FlowLayout());
//adds a border around it
registrantPanel.setBorder(BorderFactory.createTitledBorder(BORDER));
//creates the JTextField used for the registrants name
txtName = new JTextField(20);
//creates the JComboBox for registrant types
typesBox = new JComboBox<String>(REGISTRANT_TYPES);
//adds the name box and registrant types to the registrant panel
registrantPanel.add(txtName);
registrantPanel.add(typesBox);
//returns the registrantPanel
return registrantPanel;
}
private JPanel buildDinnerKeynotePanel()
{
//variable for temporary dinnerKeynotePanel that will be returned
JPanel dinnerKeynotePanel;
//instantiates the dinnerKeynotePanel
dinnerKeynotePanel = new JPanel();
//sets its layout to a flow layout
dinnerKeynotePanel.setLayout(new FlowLayout());
//creates the chkKeynote JCheckBox
chkKeynote = new JCheckBox(DINNER_CHECK);
//adds the chkKeynote JCheckBox to the dinnerKeynotePanel
dinnerKeynotePanel.add(chkKeynote);
//returns the dinnerKeynotePanel
return dinnerKeynotePanel;
}
public double getRegistrantCost()
{
//storage variable for the registrantCost
double registrantCost = 0;
//storage variable for the index, had to be defaulted to 0
int index = 0;
switch (typesBox.getSelectedIndex())
{
case 1: index = 0;
break;
case 2: index = 1;
break;
case 3: index = 2;
break;
}
//Finds the value of the selected index from the REGISTRANT_COSTS array and places it into
//the registrantCost variable
registrantCost = REGISTRANT_COSTS[index];
//returns the registrantCost
return registrantCost;
}
public double getKeynoteCost()
{
//storage variable for the keynoteCost. Defaulted to zero and is only modified when the if
//statment is entered.
double keynoteCost = 0;
if (chkKeynote.isSelected())
{
//sets the keynoteCost to the value in DINNER_KEYNOTE_SPEECH if the chkKeynote
//JCheckBox is selected
keynoteCost = DINNER_KEYNOTE_SPEECH;
}
//returns the keynoteCost
return keynoteCost;
}
public String getRegType()
{
//storage variable for the string returned by the .toString call
String selection;
//sets selection to the value returned by calling the typesBox .toString method
selection = typesBox.getSelectedItem().toString();
//returns selection
return selection;
}
}
ConfClient.java
import javax.swing.*;
import java.awt.*;
public class ConfClient extends JFrame
{
private Container c;
private ConferenceGUI gui;
public ConfClient()
{
gui = new ConferenceGUI();
c = getContentPane();
setSize(700,300);
add(gui);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args)
{
ConfClient client = new ConfClient();
}
}
ConferenceClient.java
import java.awt.*;
import javax.swing.*;
public class ConferenceClient extends JApplet
{
//variables containing the static height and width for the applet
private final int WIDTH = 700;
private final int HEIGHT = 250;
//variable to store the ConferenceGUI object.
private ConferenceGUI gui;
//variable for the container
private Container c;
public void init()
{
//gets the container window information from the operating system
getContentPane();
//sets the size of the container
setSize(WIDTH, HEIGHT);
//instantiates the ConferenceGUI object
gui = new ConferenceGUI();
//adds the ConferenceGUI object to the container
add(gui);
//makes the container visible
setVisible(true);
}
}
ConferenceGUI.java
import java.awt.*;
import javax.swing.*;
public class ConferenceGUI extends JPanel
{
//storage variables for objects used to create the GUI
protected JPanel titlePanel;
protected RegPanel regPanel;
protected WorkshopPanel workshopPanel;
protected JPanel buttonPanel;
protected JButton calculateButton;
protected JButton clearButton;
protected JTextArea textArea;
protected JScrollPane scrollArea;
protected JLabel title;
protected Font titleFont;
protected ConferenceHandler handler;
protected JPanel conferencePanel;
//string literals for text used in the GUI elements
private final String TITLE = "Select Registration Options";
private final String CALC_BTTN = "Calculate Charges";
private final String CLR = "Clear";
public ConferenceGUI()
{
//instantiates required objects from other classes
conferencePanel = new JPanel();
regPanel = new RegPanel();
workshopPanel = new WorkshopPanel();
handler = new ConferenceHandler(this);
//sets the gui layout to a border layout
conferencePanel.setLayout(new BorderLayout());
//builds the title panel and adds it to the north part of the GUI
titlePanel = buildTitlePanel();
conferencePanel.add(titlePanel, BorderLayout.NORTH);
//adds the reg panel to the west part of the GUI
conferencePanel.add(regPanel, BorderLayout.WEST);
//adds the required listeners to the GUI using the handler
regPanel.txtName.addFocusListener(handler);
regPanel.typesBox.addItemListener(handler);
regPanel.chkKeynote.addItemListener(handler);
//adds the workshop panel to the east part of the GUI
conferencePanel.add(workshopPanel, BorderLayout.EAST);
//builds a button panel and then adds to to the southern part of the GUI
buttonPanel = buildButtonPanel();
conferencePanel.add(buttonPanel, BorderLayout.SOUTH);
add(conferencePanel);
}
private JPanel buildTitlePanel()
{
//variable for holding a temporary JPanel that will be returned
JPanel titlePanel;
//instantiate the titlePlanel
titlePanel = new JPanel();
//instantiate a JLabel for the title panel
title = new JLabel(TITLE);
//creates a font object
titleFont = new Font("SansSerif", Font.BOLD, 18);
//sets the font of the title text to the font that was created
title.setFont(titleFont);
//adds the title to the titlePanel
titlePanel.add(title);
//returns the title panel to the constructor
return titlePanel;
}
private JPanel buildButtonPanel()
{
//variable for a temporary JPanel that will be returned.
JPanel buttonPanel;
//instantiates the buttonPanel
buttonPanel = new JPanel();
//sets its layout to a flow layout
setLayout(new FlowLayout());
//creates the calculate button and adds its Action Listener
calculateButton = new JButton(CALC_BTTN);
calculateButton.addActionListener(handler);
//creates the clear button and adds its Action Listener
clearButton = new JButton(CLR);
clearButton.addActionListener(handler);
//creates a text area that is 5 lines high and 30 chars wide
textArea = new JTextArea(5, 30);
//turns on line wrap for the text area
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
//creates a scroll area and places the textArea object inside it
scrollArea = new JScrollPane(textArea);
//adds the calculate button to the button panel
buttonPanel.add(calculateButton);
//adds the clear button to the button panel
buttonPanel.add(clearButton);
//adds the scroll area to the button panel
buttonPanel.add(scrollArea);
//returns the buttonPanel to the constructor
return buttonPanel;
}
}
ConferenceHandler.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;
public class ConferenceHandler implements ActionListener, ItemListener, FocusListener
{
//storage variable containing the ConferenceGUI object
protected ConferenceGUI gui;
//string literals for text used in GUI elements
private final String NO_NAME = "Please enter a name";
private final String REG_ERROR1 = "Please select a registration type";
private final String REG_ERROR2 = "Type Error";
private final String NAME_ERROR1 = "Please enter a name";
private final String NAME_ERROR2 = "Name Error";
private final String REGTYPE1 = " is a ";
private final String REGTYPE2 = " registration ";
private final String KEY_ATTEND = "Keynote address will be attended ";
private final String KEY_NO_ATTEND = "Keynote address will not be attended ";
private final String REGISTERED = " is registered in these workshops: ";
private final String TOTAL_CHARGES1 = " Total charges for ";
private final String TOTAL_CHARGES2 = " are ";
//decimalFormat used to format the currency output in the JTextArea
protected DecimalFormat currency = new DecimalFormat("$#,##0.00");
public ConferenceHandler(ConferenceGUI gui)
{
//sets the class level variable value to the value passed in as a parameter.
this.gui = gui;
}
public void actionPerformed(ActionEvent e)
{
//storage variable for a string array that will contain the selected entries in the
//workshops panel
String[] selections;
//storage variable for the running total cost
double totalCost;
//when an action is performed, this block checks to see if it was the calculate button
if (e.getSource() == gui.calculateButton)
{
//this block checks to see if the selected index of the JComboBox is still 0
//and if it is, prompts the user to select a registration type
if (gui.regPanel.typesBox.getSelectedIndex() == 0)
{
JOptionPane.showMessageDialog(null,
REG_ERROR1,
REG_ERROR2,
JOptionPane.ERROR_MESSAGE);
}
else
{
if (gui.regPanel.chkKeynote.isSelected())
{
gui.textArea.append(KEY_ATTEND);
}
else
{
gui.textArea.append(KEY_NO_ATTEND);
}
gui.textArea.append(gui.regPanel.txtName.getText() + REGISTERED);
//gets the list of workshops selected and then stores them in the variable
selections = gui.workshopPanel.getWorkshopList();
//for every entry stored in the selections array
for (int i = 0; i < selections.length; i++)
{
//print the selection to the text area
gui.textArea.append(selections[i] + " ");
}
totalCost = gui.regPanel.getKeynoteCost() + gui.workshopPanel.getWorkshopCost()
+ gui.regPanel.getRegistrantCost();
//prints a line presenting the total cost for the user in the text area.
gui.textArea.append(TOTAL_CHARGES1 + gui.regPanel.txtName.getText()
+ TOTAL_CHARGES2 + currency.format(totalCost));
}
}
//when an action is performed, this block checks to see if it was the clear button
if (e.getSource() == gui.clearButton)
{
//sets the registrant types back to the default
gui.regPanel.typesBox.setSelectedIndex(0);
//cleasrs all the selections on the workshop panel
gui.workshopPanel.workshopListBox.clearSelection();
//unchecks the keynote checkbox
gui.regPanel.chkKeynote.setSelected(false);
//empties the name field
gui.regPanel.txtName.setText("");
//emptie the text area
gui.textArea.setText("");
//sets the focus to the name text field
gui.regPanel.txtName.requestFocus();
}
}
public void itemStateChanged(ItemEvent i)
{
//storage variable for the name taken from the name text field
String name;
//storage variable for the registration type string
String selection;
//if the user attemts to change the types box but hasn't entered a name yet
if ( (i.getSource() == gui.regPanel.typesBox) &&
(gui.regPanel.txtName.getText().length() == 0) )
{
//sets the registrant types back to the default
gui.regPanel.typesBox.setSelectedIndex(0);
//brings the name field back into focus
gui.regPanel.txtName.requestFocus();
}
//checks to see if the source of the event was the typesBox and that the name has not been
//left blank
if ( (i.getSource() == gui.regPanel.typesBox) &&
(gui.regPanel.txtName.getText().length() != 0) )
{
//gets the name from the text field
name = gui.regPanel.txtName.getText();
//gets the registration type selected
selection = gui.regPanel.getRegType();
//places the name and registration type into the text area
gui.textArea.setText(name + REGTYPE1 + selection + REGTYPE2);
}
}
public void focusLost(FocusEvent f)
{
//checks to make sure that the source of the event is txtName
if (f.getSource() == gui.regPanel.txtName)
{
//if the name feild is left blank, displays an error message using JOptionPane
if (gui.regPanel.txtName.getText().length() == 0)
{
JOptionPane.showMessageDialog(null,
NAME_ERROR1,
NAME_ERROR2,
JOptionPane.ERROR_MESSAGE);
//brings the name field back into focus after the user presses OK
gui.regPanel.txtName.requestFocus();
}
}
}
public void focusGained(FocusEvent f)
{
//checks to make sure that the source of the event is txtName
if (f.getSource() == gui.regPanel.txtName)
{
//if it is, then select all of the text.
gui.regPanel.txtName.selectAll();
}
}
}
RegPanel.java
import java.awt.*;
import javax.swing.*;
public class RegPanel extends JPanel
{
//final variables containing the cost of each registration type
private final double BUSINESS_REG = 895.00;
private final double STUDENT_REG = 495.00;
private final double COMPLEMENT_REG = 0.00;
//final variable containing the cost of the dinner keynote speech
private final double DINNER_KEYNOTE_SPEECH = 30.00;
//registrant costs assembled as an array.
//On the class diagram, this was called WORKSHOP_COSTS but I felt that wasn't an accurate name
private final double[] REGISTRANT_COSTS = {BUSINESS_REG,
STUDENT_REG,
COMPLEMENT_REG};
//string array used in the creation of the JComboBox
//on the class diagram this was called WORKSHOPS, but I felt that wasn't an accurate name
private final String[] REGISTRANT_TYPES = {"Please select a type",
"Business",
"Student",
"Complimentary"};
//storage variables for objects used to create the registrant panel object
protected JPanel registrantPanel;
protected JPanel dinnerKeynotePanel;
protected JTextField txtName;
protected JComboBox<String> typesBox;
protected JCheckBox chkKeynote;
//string literals for text used in the GUI elements
private final String DINNER_CHECK = "Dinner and Keynote Speech";
private final String BORDER = "Registrant's Name & Type";
public RegPanel()
{
//sets the layout to a 2x1 grid layout
this.setLayout(new GridLayout(2,1));
//builds the registrantPanel and then adds it to the RegPanel
registrantPanel = buildRegistrantPanel();
add(registrantPanel);
//builds the dinnerKeynotePanel and then adds it to the Regpanel
dinnerKeynotePanel = buildDinnerKeynotePanel();
add(dinnerKeynotePanel);
}
private JPanel buildRegistrantPanel()
{
//variable for temporary registrantJPanel that will be returned
JPanel registrantPanel;
//instantiates the registrantPanel
registrantPanel = new JPanel();
//sets its layout to a flow layout
registrantPanel.setLayout(new FlowLayout());
//adds a border around it
registrantPanel.setBorder(BorderFactory.createTitledBorder(BORDER));
//creates the JTextField used for the registrants name
txtName = new JTextField(20);
//creates the JComboBox for registrant types
typesBox = new JComboBox<String>(REGISTRANT_TYPES);
//adds the name box and registrant types to the registrant panel
registrantPanel.add(txtName);
registrantPanel.add(typesBox);
//returns the registrantPanel
return registrantPanel;
}
private JPanel buildDinnerKeynotePanel()
{
//variable for temporary dinnerKeynotePanel that will be returned
JPanel dinnerKeynotePanel;
//instantiates the dinnerKeynotePanel
dinnerKeynotePanel = new JPanel();
//sets its layout to a flow layout
dinnerKeynotePanel.setLayout(new FlowLayout());
//creates the chkKeynote JCheckBox
chkKeynote = new JCheckBox(DINNER_CHECK);
//adds the chkKeynote JCheckBox to the dinnerKeynotePanel
dinnerKeynotePanel.add(chkKeynote);
//returns the dinnerKeynotePanel
return dinnerKeynotePanel;
}
public double getRegistrantCost()
{
//storage variable for the registrantCost
double registrantCost = 0;
//storage variable for the index, had to be defaulted to 0
int index = 0;
switch (typesBox.getSelectedIndex())
{
case 1: index = 0;
break;
case 2: index = 1;
break;
case 3: index = 2;
break;
}
//Finds the value of the selected index from the REGISTRANT_COSTS array and places it into
//the registrantCost variable
registrantCost = REGISTRANT_COSTS[index];
//returns the registrantCost
return registrantCost;
}
public double getKeynoteCost()
{
//storage variable for the keynoteCost. Defaulted to zero and is only modified when the if
//statment is entered.
double keynoteCost = 0;
if (chkKeynote.isSelected())
{
//sets the keynoteCost to the value in DINNER_KEYNOTE_SPEECH if the chkKeynote
//JCheckBox is selected
keynoteCost = DINNER_KEYNOTE_SPEECH;
}
//returns the keynoteCost
return keynoteCost;
}
public String getRegType()
{
//storage variable for the string returned by the .toString call
String selection;
//sets selection to the value returned by calling the typesBox .toString method
selection = typesBox.getSelectedItem().toString();
//returns selection
return selection;
}
}
WorkshopPanel.java
import java.awt.*;
import javax.swing.*;
import java.util.*;
public class WorkshopPanel extends JPanel
{
//final variables containing the cost of each workshop
private final double COST_IT_TRENDS_WS = 295.00;
private final double COST_DREAM_CAREER_WS = 295.00;
private final double COST_JAVA_WS = 395.00;
private final double COST_ETHICS_WS = 395.00;
//workshop costs assembled as an array
private final double[] WORKSHOP_FEES = {COST_IT_TRENDS_WS,
COST_DREAM_CAREER_WS,
COST_JAVA_WS,
COST_ETHICS_WS};
//array containing strings for use in the JList
private final String[] WORKSHOP_NAMES = {"IT Trends in Manitoba",
"Creating a Dream Career",
"Advanced Java Programming",
"Ethics: The Challenge Continues"};
//storage variable for the JList
protected JList<String> workshopListBox;
//string literal used in the creation of the panel border
private final String BORDER = "Workshops";
public WorkshopPanel()
{
//sets the layout to a flow layout
setLayout(new FlowLayout());
//creates the workshopListBox JList using the WORKSHOP_NAMES array for content
workshopListBox = new JList<String>(WORKSHOP_NAMES);
//allows more than one item to be selected
workshopListBox.setSelectionMode(2);
//puts a border around the box
setBorder(BorderFactory.createTitledBorder(BORDER));
//adds the workshopListBox to wherever its called
add(workshopListBox);
}
public double getWorkshopCost()
{
//integer array containing all of the selected indexes from workshopListBox
int[] selectedIndices = workshopListBox.getSelectedIndices();
//storage variable for the totalWorkshopCost
double totalWorkshopCost = 0;
//for every value in the selectedIndicies array, use that index to retrieve a cost value from
//the WORKSHOP_FEES array, then add it to the current value of totalWorkshopCost.
for (int i = 0; i < selectedIndices.length; i++)
{
totalWorkshopCost += WORKSHOP_FEES[selectedIndices[i]];
}
//returns the totalWorkshopCost
return totalWorkshopCost;
}
public String[] getWorkshopList()
{
//integer array containing all of the selected indexes from workshopListBox
int[] selectedIndices = workshopListBox.getSelectedIndices();
String[] selectedNames = new String[selectedIndices.length];
for (int i = 0; i < selectedIndices.length; i++)
{
selectedNames[i] = WORKSHOP_NAMES[selectedIndices[i]];
}
//returns the selectedNames
return selectedNames;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.