简体   繁体   中英

How can I get my JAVA Method-Chaining to work?

I am trying to do some simple method chaining but always have the error "cannot find symbol" part way through. eg:

public JButton[] getSignOnButtons() {
    return InitialFrame.getInitialPanel().getSignOnButtons();
}

I am implementing the MVC model, in the View Package I have 4 classes: View, InitialFrame, InitialPanel, NorthPanel. For my Controller to communicate with the View package, I always go through the View Class.

My Controller Class needs to access attributes of View's classes, what's the best way?

I "cheated" it previously by making all the View classes' attributes public so I could just create a 'get' method from view eg

return InitialFrame.InitialPanel.Buttons;

Thanks for any help.

The error just says "cannot not find symbol" is each case.

**EDITED from this point down......

This is the whole View Package:

public class View {

    InitialFrame initialFrame;

    public View(){

        initialFrame = new InitialFrame();

    }

    public JFrame getInitialFrame() {
        return initialFrame;
    }

    public InitialPanel getInitialPanel() {
       return InitialFrame.getInitialPanel();
    }

    public JButton[] getSignOnButtons() {
        return initialFrame.getInitialPanel().getSignOnButtons();
    }
}

This is the InitialFrame Class:

public final class InitialFrame extends JFrame {

    private final InitialPanel initialPanel;

    public InitialFrame() {

        super("Welcome to the Sign-on Screen");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        this.setSize(700, 700);
        this.setLayout(new GridLayout(1, 1));

        initialPanel = new InitialPanel();        
        this.add(initialPanel);

        //this.pack();
        this.setLocationRelativeTo(null);
        this.validate();
        this.repaint();
        this.setVisible(true);

        JButton[] test = initialPanel.getSignOnButtons();
        String newStr = initialPanel.getNorthPanel().getTest();    //Call to getTest
    }

    public InitialPanel getInitialPanel() {
        return initialPanel;
    }


}

//InitialPanel ___________

class InitialPanel extends JPanel{

    private BorderLayout InitialPanelLayout;
    private JButton[] signOnButtons;
    private NorthPanel northPanel;
    private JPanel southPanel;
    private JPanel leftPanel;
    private JPanel rightPanel;
    private JPanel centerPanel;
    private JLabel userNameLabel;
    private JTextField userNameTextField;

    public InitialPanel() {

        this.setSize(600, 600);
        InitialPanelLayout = new BorderLayout();
        this.setLayout(InitialPanelLayout);

        this.createPanels();
        this.formatCenterPanel();


        setVisible(true);
        this.validate();
        this.repaint();
    }

    /**
     * Method is to create panels for all the Border Layout of initial Panel
     * @param none
     */
    private void createPanels() {

        //Graphics comp2D = new Graphics();
        //comp2D.drawString("Free the bound periodicals", 22, 100);

        northPanel = new NorthPanel();
        northPanel.setSize(600, 200);
        this.add(northPanel, "North");

        southPanel = new JPanel();
        this.add(southPanel, "South");

        leftPanel = new JPanel();
        this.add(leftPanel, BEFORE_LINE_BEGINS);

        rightPanel = new JPanel();
        this.add(rightPanel, AFTER_LINE_ENDS);

        centerPanel = new JPanel();
        this.add(centerPanel, "Center");

    }

    /**
     * Method is to format the center panel on the opening window.
     * It uses 4 row grid layout, top row is a container with Label and TextField.
     * @param none 
     */
    private void formatCenterPanel() {

        centerPanel.setLayout(new GridLayout(5, 1));
        Container userName = new Container();
        userName.setLayout(new GridLayout(1, 2));
        userNameLabel = new JLabel("UserName: ");
        userName.add(userNameLabel);
        userNameTextField = new JTextField(30);
        userName.add(userNameTextField);

        centerPanel.add(userName);

        signOnButtons = new JButton[3];
        signOnButtons[0] = new JButton("Sign-On");
        signOnButtons[1] = new JButton("Register");
        signOnButtons[2] = new JButton("Exit");

        for (JButton butt: signOnButtons) {
            centerPanel.add(butt);
        }    
        centerPanel.validate();
        centerPanel.repaint();
    }

    public JButton[] getSignOnButtons() {
        return signOnButtons;
    }

    public JTextField getUserNameTextField() {
        return userNameTextField;
    }

    public NorthPanel getNorthPanel() {
        return northPanel;
    }
}

ALL updated now...

Only one error remains: "non-static method getInitialPanel() cannot be referenced from a static context"

in View Class

public InitialPanel getInitialPanel() {
   return InitialFrame.getInitialPanel();
}

Final Edit: The main solution was to use the 'this' keyword. After that, I could use Controller to chain 3 or more methods to retrieve attributes buried in the View Package.

for example, in the View Class:

public JButton[] getSignOnButtons() {
    return this.initialFrame.getInitialPanel().getSignOnButtons();
}

**EDIT 12/25/2018 the this.keyword does not solve this every time. It is still a tricky operation. Sometimes I just allowed NetBeans to create the method itself because it says method not found even though it is named exactly the same.

Method chaining works from left to right in way that value returned by left method must have right method implemented in the class;

for example;

" Hello ".substring(1).trim()

Notice that here substring(1) is returning String value, which has also trim() method implemented in its ( String ) class.

In your case; return InitialFrame.getInitialPanel().getSignOnButtons();

you are returning Initialframe from getInitialPlane() method, but there is not implementation of getSignOnButtons() in InitialFrame class. That's why JVM is complaining.

There is no method getSignOnButtons() in JPanel . Perhaps it's in InitialPanel ? if that is the case, the return type of InitialFrame.getInitialPanel() should be InitialPanel instead of JPanel .

This is happening because you are trying to execute a method which is not present in JFrame class. When you are declaring this JFrame InitialFrame; , at compile time Java will check whether the method exists or not in JFrame class. Since it is not there in JFrame, you looks to be getting "cannot not find symbol" error.

Also, try to follow code standards - field names should be in lower camel case ie intialFrame.

Try below code (I have not compiled it though) -

public class View {

    private InitialFrame initialFrame;

    public View(){

        initialFrame = new InitialFrame();

    }

    public JFrame getInitialFrame() {
        return this.initialFrame;
    }

    public JPanel getInitialPanel() {
        return this.initialFrame.getInitialPanel();
    }

    public JButton[] getSignOnButtons() {
        return this.initialFrame.getInitialPanel().getSignOnButtons(); 
    }
}

public final class InitialFrame extends JFrame {

    private InitialPanel initialPanel;

    public InitialFrame() {

        super("Welcome to the Sign-on Screen");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);

        this.setSize(700, 700);
        this.setLayout(new GridLayout(1, 1));

        initialPanel = new InitialPanel();        
        this.add(initialPanel);

        //this.pack();
        this.setLocationRelativeTo(null);
        this.validate();
        this.repaint();
        this.setVisible(true);

        JButton[] test = initialPanel.getSignOnButtons();
        String new = initialPanel.getNorthPanel().getTest(); 
    } 

    public JPanel getInitialPanel() {
        return this.initialPanel;
    }


}

I assume you have another InitialPanel class which is extending JPanel and having getSignOnButtons() method defined in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM