user3932611
user3932611

Reputation:

Apparent inconsistency in execution of code

I have an Editbutton which controls the actions of a Changebutton.

When the Editbutton is true, the Changebutton should go to Edit, and when it is not true it should go to view.

The problem is the Editbutton is not executing consistently.

The first time the code is loaded, the Editbutton will go between true and false, as it should. However, if I press the Changebutton, the Editbutton will no longer switch between true and false, but will go to true and then false in one iteration. The third time it will execute as intended, with this alternating cycle continuing.

I am using a return statement in the listener of the Editbutton, that is supposed to stop the code after an if statement. I have added in numbers to track what is being executed and it seems that the second time the code is executing past the return statement eg not being stopped. Either that or for some reason the code is reloading for second time.

Below is an MCVE version of my code:

public class Toolbar {

    static Color c;
    static JButton buttonEdit = new JButton("Edit");
    public static JFrame frame = new JFrame();
    static boolean Edit, Delete;


    public static void main(String[] args) {
        Toolbar.WorkDiaryAllGui();
        System.out.println("TOOLBAR");
    }

    public static JPanel Toolbar(String panelname){

        FlowLayout layout = new FlowLayout();

        JPanel Toolbar = new JPanel(new BorderLayout());
        Toolbar.setLayout(layout);

        JButton Changebutton = new JButton("Diary");    
        Changebutton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                WorkDiaryAllGui();

            }
        });




        buttonEdit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                if(Edit==false){
                Edit = true;
                buttonEdit.setBackground(Color.CYAN);
                System.out.println(1);
                return; 
                }


                if(Edit==true){
                Edit = false;
                buttonEdit.setBackground(c);
                System.out.println(2);
                return; 
                }


            }
        });

        Toolbar.add(Changebutton);
        Toolbar.add(buttonEdit);

        return Toolbar;

    }

    public static void WorkDiaryAllGui(){

        JPanel WorkDiaryAll = new JPanel();

        WorkDiaryAll.add(Toolbar.Toolbar("Whole Diary"));

        frameGui(WorkDiaryAll, "Whole Diary");
    }

    public static void frameGui(JPanel panel, String name){

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setContentPane(panel);

        frame.setSize(1200,500);

        frame.setVisible(true);
 }

}

Upvotes: 0

Views: 39

Answers (1)

Every time Toolbar is called you add another listener to the button.

After I call it once, it has a listener which toggles edit mode (or whatever the button is supposed to do).

If I call it again, it returns a new JPanel, containing the same button from before, which now has two listeners that toggle edit mode. When I click the button, it toggles edit mode twice.

If I call it again, it has three listeners. If I click the button, it toggles edit mode three times. And so on.

I suggest making Toolbar (the method) create entirely new GUI objects each time it's called. If you do that, Toolbar (the class) shouldn't need any fields (in your question's version of the code, at least).

Also, the usual Java convention is to begin method names with lowercase letters - however, this is only a style issue and doesn't stop your program from working, so change it if you want.

A more serious style issue (but still only affecting readability of your code, not whether it works) - method names should describe what they do. Does Toolbar toolbar? I don't think it toolbars, so it shouldn't be named Toolbar. (Similarly, WorkDiaryAllGui doesn't work the diary all GUI, and frameGui doesn't frame the GUI). If you want other style tips, consider posting your code (once it works) on https://codereview.stackexchange.com/.

Upvotes: 1

Related Questions