The Class file which is named Project2
public class Project2 extends JPanel implements ActionListener
{
private JToggleButton ISLM1103 = new JToggleButton("ISLM1103");
private JToggleButton GENG215 = new JToggleButton("GENG215");
private JToggleButton ESPU107 = new JToggleButton("ESPU107");
private JToggleButton ESPU1452 = new JToggleButton("ESPU1452");
private JToggleButton HSS110 = new JToggleButton("HSS110");
private JToggleButton Calculate = new JToggleButton("Calculate");
private JToggleButton Exit = new JToggleButton("Exit");
public Project2()
{
//Adding the action even to the buttons
ISLM1103.addActionListener(this);
GENG215.addActionListener(this);
ESPU107.addActionListener(this);
ESPU1452.addActionListener(this);
HSS110.addActionListener(this);
Calculate.addActionListener(this);
Exit.addActionListener(this);
//Set the Layout
setLayout(new GridLayout(12,12));
//Adding the buttons
add(ISLM1103);
add(GENG215);
add(ESPU107);
add(ESPU1452);
add(HSS110);
add(Calculate);
add(Exit);
}
public static void main(String[] args)
{
new Project2();
}
public void actionPerformed(ActionEvent actionlistner)
{
// initializing the arraylist
ArrayList<String> courseList = new ArrayList<String>();
if (actionlistner.getActionCommand().equals("ISLM1103"))
courseList.add("ISLM1103");
if (actionlistner.getActionCommand().equals("GENG215"))
courseList.add("GENG215");
if (actionlistner.getActionCommand().equals("ESPU107"))
courseList.add("ESPU107");
if (actionlistner.getActionCommand().equals("ESPU1452"))
courseList.add("ESPU1452");
if (actionlistner.getActionCommand().equals("HSS110"))
courseList.add("HSS110");
if (actionlistner.getActionCommand() == "Calculate")
{
try
{
FileWriter writer = new FileWriter("Course.txt");
BufferedWriter course = new BufferedWriter(writer);
PrintWriter out = new PrintWriter(course);
for(int i = 0; i < courseList.size(); i++)
{
if(courseList.get(i) != null)
out.println(courseList.get(i));
}
out.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
if (actionlistner.getActionCommand() == ("Exit"))
System.exit(0);
}
}
This one is called project one it's my constructor it's perfectly fine
public class Project1 extends JFrame
{
private Project2 topleft; // Buttons
private Project3 topright; // UAEU - Picture
private Project4 bottomleft; // Schedule
private Project5 bottomright; // Help - Pad
// Constructor
public Project1() throws IOException
{
// Display a title.
setTitle(" UAE University Interactive Course Calculator");
// Specify an action for the close button.
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create a GridLayout manager.
setLayout(new GridLayout(2, 2));
// Create the custom panels.
topleft = new Project2();
topright = new Project3();
bottomleft = new Project4();
bottomright = new Project5();
// Create the button panel.
add(topleft);
add(topright);
add(bottomleft);
add(bottomright);
// setting formatting options
pack();
setResizable(true);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setVisible(true);
}
// Main method
public static void main(String[] args) throws IOException
{
new Project1();
}
}
Just focus on the Project 2 and when the If functions appear, see if I remove the action listners the array prints out, so it's not an array issue. Now the program just won't add the values I told for it to add when the button is pressed. Anyone can help?
Edit!!! = it doesn't matter if i put .equals or == the program works nevertheless regardless of what I wrote. Because the calculate and the exit button work. It's not that. So before you assume it is i suggest you try out the program before assuming things from your mind