Problems with Applet
Hello,
I'm having problems with my Applet. Basically the issue is that once I'm in the roomInfoScreen and the if condition does not apply anymore I get an exception and I don't know how to fix this problem .
I will appreciate any help.
Thank you very much
CODE
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class InputApplet extends JApplet implements ActionListener
{
int NUM_ROOMS; //rooms in the house
int clickcount=0;
Container con = getContentPane();
JPanel roomNumScreen = new JPanel(new FlowLayout());
JPanel roomInfoScreen = new JPanel(new FlowLayout());
JPanel sortingScreen = new JPanel(new FlowLayout());
JPanel outputScreen = new JPanel(new FlowLayout());
JPanel errorScreen = new JPanel (new FlowLayout());
JLabel labelRoomNum = new JLabel("Please enter the number of rooms in the your house");
JTextField textRoomNum = new JTextField("", 3);
JButton buttonNumSubmit = new JButton("Submit");
JLabel errorLabel = new JLabel("Input must be a number, please try again");
JButton errorButton = new JButton("Retry");
JLabel roomNumber = new JLabel("Room#1 information: ");
JLabel roomName = new JLabel("Room Name: ");
JLabel roomWidth = new JLabel("Room Width: ");
JLabel roomLength = new JLabel("Room Length: ");
JLabel roomFloor = new JLabel("Floor Room can be found on: ");
JTextField textRoomName = new JTextField("", 40);
JTextField textRoomWidth = new JTextField("", 40);
JTextField textRoomLength = new JTextField("", 40);
JTextField textRoomFloor = new JTextField("", 40);
JButton buttonRoomInfo = new JButton("Submit");
JLabel sortingLabel = new JLabel("Please make your reporting selection: ");
JButton sortArea = new JButton("Sort by Area");
JButton sortRoomName = new JButton("Sort by Room Name");
JButton sortFloorNum = new JButton("Sort by Floor Number");
RoomClass[] room; // instanciating the RoomClass
public void init()
{
roomNumScreen.add(labelRoomNum);
roomNumScreen.add(textRoomNum);
roomNumScreen.add(buttonNumSubmit);
textRoomNum.requestFocus();
buttonNumSubmit.addActionListener(this);
con.add(roomNumScreen);
errorScreen.add(errorLabel);
errorScreen.add(errorButton);
errorButton.addActionListener(this);
roomInfoScreen.add(roomNumber);
roomInfoScreen.add(roomName);
roomInfoScreen.add(textRoomName);
roomInfoScreen.add(roomWidth);
roomInfoScreen.add(textRoomWidth);
roomInfoScreen.add(roomLength);
roomInfoScreen.add(textRoomLength);
roomInfoScreen.add(roomFloor);
roomInfoScreen.add(textRoomFloor);
roomInfoScreen.add(buttonRoomInfo);
buttonRoomInfo.addActionListener(this);
sortingScreen.add(sortingLabel);
sortingScreen.add(sortArea);
sortingScreen.add(sortRoomName);
sortingScreen.add(sortFloorNum);
sortArea.addActionListener(this);
sortRoomName.addActionListener(this);
sortFloorNum.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
String stgRoomNum, roomName, stgRoomWidth, stgRoomLength, stgRoomFloor; //name of the room
intfloorNum, numRetries =3, x; //variables for floor number and number of retries.
double roomLength, roomWidth; //variables for lengh,width,
Object source = event.getSource();
if(source == buttonNumSubmit)
{
stgRoomNum = textRoomNum.getText();
System.out.println("Inside the roomNumScreen Panel - where textRoomNum input has meaning, and stgRoomNum="+stgRoomNum);
if(stgRoomNum.length() == 0)
{
con.remove(roomNumScreen);
con.add(errorScreen);
super.validate();
repaint();
if(source == errorButton)
{
con.remove(errorScreen);
con.add(roomNumScreen);
textRoomNum.setText("");
}
}
else
{
NUM_ROOMS = Integer.parseInt(stgRoomNum);
room = new RoomClass[NUM_ROOMS];
con.remove(roomNumScreen);
con.add(roomInfoScreen);
super.validate();
repaint();
}
}
if(source == buttonRoomInfo)
{
if(clickcount <= NUM_ROOMS)
{
clickcount++;
System.out.println("Inside the roomInfoScreen Panel " + clickcount + " :");
String roomNum = "Room #" + (clickcount+1) + " information: ";
roomNumber.setText(roomNum);
roomName = textRoomName.getText();
stgRoomWidth = textRoomWidth.getText();
roomWidth = Double.parseDouble(stgRoomWidth);
stgRoomLength = textRoomLength.getText();
roomLength = Double.parseDouble(stgRoomLength);
stgRoomFloor = textRoomFloor.getText();
floorNum = Integer.parseInt(stgRoomFloor);
// this passes the values to the constructor in the RoomClass class
room[clickcount] = new RoomClass(roomName, roomLength, roomWidth, floorNum);
textRoomName.setText("");
textRoomWidth.setText("");
textRoomLength.setText("");
textRoomFloor.setText("");
}
else
{
System.out.println("Inside the else statement");
con.remove(roomInfoScreen);
con.add(sortingScreen);
super.validate();
repaint();
}
}
if (source == sortRoomName)
{
SortClass.sorting(room, "Name");
}
else if (source == sortFloorNum)
{
SortClass.sorting(room, "Level");
}
else if (source == sortArea)
{
SortClass.sorting(room, "Square Feet");
}
}
}

