judiMc / oopExamples

repository to contain the solutions for the practice problems for the scioer e-text on oo programming in java.
GNU Affero General Public License v3.0
1 stars 0 forks source link

eventDrivenProgramming/eventhandlers #22

Closed judiMc closed 1 year ago

judiMc commented 2 years ago

eventDrivenProgramming/eventhandlers

judiMc commented 2 years ago

Start with the ExampleGui and ExampleController classes shown below. Add listeners to the buttons in the gui so that when the set string value button is clicked, the value shown in the text field is 'dinosaur'. Use lambda notation for the listeners and event handler methods. Be sure to let the ExampleController class manage the value of the string variable.

judiMc commented 2 years ago
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class ExampleGui extends JFrame
{

    private ExampleController myController;

    public static final int WIDTH = 600;
    public static final int HEIGHT = 500;
    Container contentPane;
    /*if you need a component to be accessible to all 
    methods make it a member variable*/
    TextField globalTextField; 

    public ExampleGui(ExampleController theController)
    {
        super();
        myController = theController;
        setUpSize();
        setMainContainer();

    }

    private void setUpSize(){

        setSize(WIDTH, HEIGHT);
        setTitle("A Gui Example");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    }

//the next few methods set up the three panels used for the demo GUI
    private void setMainContainer(){
        contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());
        //note the nested function call
        contentPane.add(interactWithProgramPanel(), BorderLayout.CENTER);
    }

/* the  panel returned by this function is set up with buttons and additional panels 
which, in turn, have listeners that interact with the controller.  
In a more complex program the controller would get its data from a 
third class known as the 'model'   */
    private JPanel interactWithProgramPanel(){
        JPanel examplePanel = new JPanel();
        examplePanel.setLayout(new GridLayout(0,1));
        globalTextField = new TextField("here there will be text");
        examplePanel.add(globalTextField);
        //more nested function calls
        examplePanel.add(showStringValueButton());
        examplePanel.add(setStringValueButton());
        return examplePanel;
    }

        private JButton showStringValueButton(){
        JButton exampleButton = new JButton("display current value of string value in controller/model");
        // add a listener to the button here. use methods if you can.
        return exampleButton;
    }

    private JButton setStringValueButton(){
        JButton exampleButton = new JButton("set string value of controller/model");
        // add a listener to the button here. use methods if you can.
        return exampleButton;
    }

/* event handler methods that might be useful  */

 private void changeString(String text){
    myController.setString(text);

 }
 private void showString(){
    globalTextField.setText(myController.toString());

 }
}
judiMc commented 2 years ago
package oointro;

public class ExampleController
{
    private String myString;

    public ExampleController(){
        myString = "this is a string";
    }

    public void setString(String ss){
        myString = ss;
    }
    @Override
    public String toString()
    {
        return myString;
    }

}