Home how to count up variables in java
Reply: 4

how to count up variables in java

java projekt
java projekt Published in 2017-11-13 15:13:19Z

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 :)

Synch Reply to 2017-11-13 15:30:21Z

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"));
Joop Eggen
Joop Eggen Reply to 2017-11-13 15:32:19Z

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) {

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) ...
ACV Reply to 2017-11-13 15:34:09Z
formatFields(String style, Pane... panes) {
 for (Pane pane : panes) {
 if (pane!=null) {

and then call:

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

fxrbfg Reply to 2017-11-13 15:35:31Z

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;
              .forEach(pane -> pane.setStyle("-fxbackground-color: white"));
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.323379 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO