Question

How should i implement an MVC controller with multiple JButtons on the view?

for example: i have a start button, stop button and many others as well.

I tried to do this for the start button and it works fine but then how do i implement it for a stop button trigger?

Controller Code:

public MVCAuctionController(Auction a, MVCAuctionView v) {
        auction = a;
        view = v;
        view.addProcessBidsListener(new ProcessBidsController());
        view.addStopProcessListener(new StopBidsController());
    }


    class ProcessBidsController  implements ActionListener
    {
        public void actionPerformed(ActionEvent e) 
        { 
            view.disableProcessButton();
            Thread thread = new Thread (auction);
            thread.start();
        }

    }

addProcessBidsListener - is associated with the START/Process button,When i click on the button - the thread starts running and fills the JTextArea with data.

Now my Stop button should stop the thread. For this if i do something like this it doesnt actually stop the Thread:

class ProcessStartController  implements ActionListener
    {
        public void actionPerformed(ActionEvent e) 
        { 
            if(e.getSource() == view.start){            
            view.disableStartButton();
            new Thread (rest).start();
            //thread.start();
            System.out.println("inside action performed of start button");
            view.kitchen.append("Orders to kitchen");
            }
            else if (e.getSource() == view.stop)
            {
                new Thread (rest).interrupt();
            }
        }


    }
Was it helpful?

Solution

Use an Action for each button, they are self contained controllers

See How to use Actions for more details

You can then set up some kind of relationship between the Actions and the main controller should you need it, via some kind listener for example

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top