0

I have an application which essentially updates many JFrames with the click off a button, when you click increment then all the values of all the attributes will be incremented. However i now need to pass a parameter from the controller class to the constructor in the main class which will give each Jframe a different location as they are currently bunched on top off each. How would i go about doing this?

The controller class

public class Controller extends JFrame
                         implements ActionListener {

    private Model model;
    private View3 view3;
    private View4 view4;
    private JButton clearViews;   // For direct message to views
    private JButton incB;
    private String title;

    // Constructor
    public Controller(Model model, String title) {

        // Record reference to the model
        this.model = model;
        this.title = getTitle();

        // Configure the window
        setTitle("Controller");
        setLocation(40,200);
        setSize(350,150);
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        Container window = getContentPane();
        window.setLayout(new FlowLayout());     // The default is that JFrame uses BorderLayout



        // Set up input GUI
        clearViews = new JButton("Clear views");
        window.add(clearViews);
        clearViews.addActionListener(this);

        incB = new JButton("Increment B");
        window.add(incB);
        incB.addActionListener(this);
        view3 = new View3(this, model);
        window.add(view3);
        view4 = new View4(this, model);
        window.add(view4);

        // Display the frame
        setVisible(true);

    } // constructor

    // Button click handling:
    public void actionPerformed(ActionEvent e) {

        if (e.getSource() == clearViews) {
            view3.clear();
            view4.clear();
        }
        else if (e.getSource() == incB) 
            model.modifyB();     // The model will trigger the views to update themselves

    } // actionPerformed

} 

Main Method

public class Main {

    public static void main(String args[]) {

        Model model = new Model(); 
        String title = new String();


        Controller1 c1 = new Controller(model, title); 
        Controller2 c2 = new Controller(model, title); 
        Controller2 c3 = new Controller(model, title ); 

    } // main

} // Main
Toby
  • 3
  • 4
  • See: [The use of multiple JFrames, Good / Bad practice?](http://stackoverflow.com/questions/9554636/the-use-of-multiple-jframes-good-or-bad-practice) The general consensus says it's a bad practice. But if you insist in using it, use [`JFrame#setLocation(...)`](https://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#setLocation(int,%20int)) or [`JFrame#setLocationByPlatform(...)`](https://docs.oracle.com/javase/7/docs/api/java/awt/Window.html#setLocationByPlatform(boolean)) also consider using a [Card layout](https://docs.oracle.com/javase/tutorial/uiswing/layout/card.html)... – Frakcool Feb 15 '17 at 22:52
  • 1
    Or using [Dialogs](https://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) – Frakcool Feb 15 '17 at 22:53
  • Way more complicated than it has to be – Toby Feb 15 '17 at 22:57

1 Answers1

1

There's not much to do it in a general manner, just accept two more parameters and use those for your location.

public Controller(Model model, String title, int xPos, int yPos) {

    // Record reference to the model
    this.model = model;
    this.title = getTitle();

    // Configure the window
    setTitle("Controller");
    setLocation(xPos, yPos);

    // Rest of the constructor...
Adowrath
  • 701
  • 11
  • 24