0

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

  • 1
    `if (actionlistner.getActionCommand() == "Calculate") {`? No. Don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *object references* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. What surprises me is that you did it correctly elsewhere in your code -- why incorrectly here? – Hovercraft Full Of Eels Dec 17 '15 at 11:54
  • Actually it doesn't matter if i put.equals or == because i tried to remove the if statement and keep the courselist.add and click calculate and it works. I tried the exit button that also works. So it's not that. You can copy paste my program and remove project 3,4,5 from the main and it will run but you'll face the same issue as me – MoodyKhader Dec 17 '15 at 12:11
  • 2
    Please read [mcve] and then carefully read and lean about debugging small programs from the page that is linked at the bottom of the M.C.V.E. page. – AdrianHHH Dec 17 '15 at 12:43
  • Showing your ``JFrame`` in the constructor is bad design - constructors should initialize your raw object instance and **only** that. Move ``.setVisible(true)`` to your ``main(String... args)`` entry point. – Binkan Salaryman Dec 17 '15 at 13:22

2 Answers2

1

In your example, I would rather compare event source with the buttons directly, and not use action commands:

public void actionPerformed(ActionEvent e){
    Object source = e.getSource();
    if (source == ISLM1103) {
        /* do something... */
    }
    /* and so on and so on... */
}

This will be valid as long as you are not goint to invoke actions from some 3rd components that does not have references to the toggle buttons. But in this case, this would be design flaw (and would require rather using separate Actions)

Binkan Salaryman
  • 3,008
  • 1
  • 17
  • 29
Antoniossss
  • 31,590
  • 6
  • 57
  • 99
1

ToggleButton#ToggleButton(String) sets the text to display rather than the actionCommand, which you seem to expect. So the line

private JToggleButton ISLM1103 = new JToggleButton("ISLM1103");

is equivalent to:

private JToggleButton ISLM1103;

{
    ISLM1103 = new JToggleButton();
    ISLM1103.setText("ISLM1103"); // <--
}
Binkan Salaryman
  • 3,008
  • 1
  • 17
  • 29