0

I'm just looking some another efficient way to pass an object parameter to method.

So I have some method like this:

private void dashboardMenu() {
    Dashboard dashboard = new Dashboard();
    body.removeAll();
    body.add(dashboard);
    dashboard.setSize(body.getWidth(), body.getHeight());
    dashboard.setVisible(true);
}

private void dataMenu() {
    Data data = new Data();
    body.removeAll();
    body.add(data);
    data.setSize(body.getWidth(), body.getHeight());
    data.setVisible(true);
}

And I want an efficient method to call between this two method with object parameter (dashboard = new Dashboard(), and data = new Data()).

What I think it should be like this for example:

private void dasboardMenu() {
    navigateMenu(Type object);
}

private void dataMenu() {
    navigateMenu(Type object);
}

private void navigateMenu(Type object) {
    object menu = new object();
    body.removeAll();
    body.add(menu);
    menu.setSize(body.getWidth(), body.getHeight());
    menu.setVisible(true);
} 

Is it possible to do that?

Please give me an example. I don't even know what keyword should I do.

2
  • Data and Dashboard are going to need to implement a common interface which has those methods. Commented Apr 19, 2022 at 21:46
  • Are the four methods you're calling declared in a common supertype of Data and Dashboard? Commented Apr 19, 2022 at 21:47

1 Answer 1

1

How about this (assuming your Dashboard and Data are Swing Components)?

private void dashboardMenu() {
    navigateMenu(new Dashboard());
}

private void dataMenu() {
    navigateMenu(new Data());
}

private void navigateMenu(JComponent c) {
    body.removeAll();
    body.add(c);
    c.setSize(body.getWidth(), body.getHeight());
    c.setVisible(true);
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.