Home > Enterprise >  Efficient Way to Pass an Object Parameter in Java Method
Efficient Way to Pass an Object Parameter in Java Method

Time:04-20

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.

CodePudding user response:

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);
} 
  •  Tags:  
  • java
  • Related