-2

I have a Pane that includes label1, label2, label3 and a icon(pane with image). I duplicated that Pane and now i have 10 Pane 5 at left 5 at right and i named every item different for Pane(like left1label1,left2label1,right2label3)but i don't want to use different method for every single Pane.

I tried to create an array of panes but couldn't figure it out. All i want is easy way to manage this Panes. How should i do it?

public void leftPane1Config()
{

    leftPane1Label1.setText("Object");

    leftPane1Label2.setText("HP:"+ hp);
    leftPane1Label3.setText("EXP:"+ exp);

    Image img= new Image("directory");
    leftPane1Icon.setEffect(new ImageInput(img));
}

public void leftPane2Config()
{

    leftPane2Label1.setText("Object");

    leftPane2Label2.setText("HP:"+ hp);
    leftPane2Label3.setText("EXP:"+ exp);

    Image img= new Image("directory");
    leftPane2Icon.setEffect(new ImageInput(img));
}

.
.And 8 other 
.


I have tried to use something like this (trying to create an hierarchy but i couldn't make it :') ) but i couldn't use setText() so it didn't work.

leftPane1.getChildren().add(leftPane1Label1);
leftPane1.getChildren().get(0).setText??

Then i tried to use something like this

Pane[] leftPane= new Pane[5];
leftPane[0]= new Pane();

and again i couldn't figure it out.

Dabs
  • 1

1 Answers1

3

It's not really clear what you're asking. Typically you would just write a method to create the panes and parameterize the parts the vary, in the usual way you do in any kind of programming.

public Pane createPane(String text1, String text2, String text3, URL image) {
    Image img = new Image(image.toExternalForm());
    // whatever layout you need here
    VBox pane = new VBox(
        new Label(text1),
        new Label(text2),
        new Label(text3),
        new ImageView(img)
    );
    // any other stuff you need to do
    return pane ;
}

And then of course you just call createPane() with the values you need and add the result to your scene graph however you need.

James_D
  • 201,275
  • 16
  • 291
  • 322
  • You can create custom objects with `new` as an alternative, like custom controls or widgets. Still, these kinds of parameterized factory methods are pretty common in JavaFX UI programming and compromise the most basic way and, in many cases, a perfectly reasonable way of customizing UI. – jewelsea Nov 11 '22 at 23:22