0

my problem is the following: I want to set all panes white, just like this:

    paneVfeld1.setStyle("-fx-background-color: white");
    paneVfeld2.setStyle("-fx-background-color: white");
    paneVfeld3.setStyle("-fx-background-color: white");
    paneVfeld4.setStyle("-fx-background-color: white");
    paneVfeld5.setStyle("-fx-background-color: white");
    paneVfeld6.setStyle("-fx-background-color: white");
    paneVfeld7.setStyle("-fx-background-color: white");
    paneVfeld8.setStyle("-fx-background-color: white");
    paneVfeld9.setStyle("-fx-background-color: white");
    paneVfeld10.setStyle("-fx-background-color: white");

But i seriously can´t do this for all my 42 panes, how can i shorten that, with an Array?, anything else?, thx for the help :)

3
  • 1
    An array or probably even better a List sounds like a good idea. Commented Nov 13, 2017 at 15:14
  • Possible duplicate of Adding to an ArrayList Java Commented Nov 13, 2017 at 15:14
  • 1
    get rid of pane variables and use arraylist or array to store references then use index Commented Nov 13, 2017 at 15:14

4 Answers 4

0

You can put all your paneVfelds into a List and then iterate over it. For example:

List<PaneVfeld> paneVfelds = new ArrayList<>();
paneVfelds.forEach(paneVfeld -> paneVfeld.setStyle("-fx-background-color: white"));
Sign up to request clarification or add additional context in comments.

Comments

0

There is no separate CSS class for a Pane, but you might create one. You still have to add the style to every class.

This allows adding more styles and for instance offering a "dark" theme.

.paperpane {
    -fx-background-color: white;
}

So the hard way:

Pane[] panes = new Pane[] { paneVfeld1, paneVfeld2, ... };
for (Pane pane : panes) {
   pane.getStyleClass().add("paperpane");
}

or make an artifical base class derived from Pane that in the constructor sets it to white (ugly though, as restrictive and abuse of inheritance).

To do a bulk change, if all those panes are children of one container, you could iterate over its children.

However the regular numbered names seem to indicate, you could better create an array or list of panes:

Pane[] vFelder = new Pane[10];
for (int i = 0; i < vFelder.length; ++i) ...

Comments

0
formatFields(String style, Pane... panes) {
 for (Pane pane : panes) {
 if (pane!=null) {
  pane.setStyle(style);
}
}
}

and then call:

formatFields("-fx-background-color: white", field1, field2, field3, field4);

Comments

0

You don't need manually put fields to array or list or especially use reflection(it's a bad practice almost in all cases) Simple find Parent where all your panes reside and apply action to all its children.

  Parent parent = //parent where all your panes resides;
  parent.getChildrenUnmodifiable()
              .forEach(pane -> pane.setStyle("-fxbackground-color: white"));

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.