Search code examples
javafxfxmlscenebuildergluon-mobilejavafxports

Javafx more reusable to click and change text of 30 labels on one page


So I am building an app where i have Arabic text all over the page added as an image through a pdf document in the fxml file, and i am adding labels so that once i swipe to the right i see the English translation and when i swipe to the left i see Arabic language. I know there will be more than 800 pages of Arabic text and double the amount of labels, would you recommend a more reusable way of achieving this?

The code i have is for 10 labels that i added and i want the text to change in :

The main thing is - i want the style and font to stay consistent and the only thing that will change is the text.

Even with the code below when i run it i see that once you click once all labels display in English so even for this one i need to add separate methods. .. but i am hoping someone can help me and recommend a solution of using one method that is totally reusable for all 10 labels in the one page. some how if we have to change anything we could just change at that one localized location.

I am new to development i am an Automation Selenium engineer so i understand resusability from a testing perspective but am new to development.

please help.

thanks

      @FXML
void changeTexttoEnglish(String setstyle, String setText, String setFont) {
    invisiblelabelpg2header.setStyle("-fx-background-color: linear-gradient(#86C1B9, #7CAFC2); -fx-background-: 40;");
    invisiblelabelpg2header.setText("In the name of ALLAH, the Lord of Mercy, the Giver of Mercy!");
    invisiblelabelpg2header.setFont(Font.font("Comic Sans", FontWeight.BOLD, 10));


    invisiblelabelpg2label1.setStyle("-fx-background-color: linear-gradient(#86C1B9, #7CAFC2); -fx-background-: 40;");
    invisiblelabelpg2label1.setText("All praise is for Allah, Lord of all worlds");
    invisiblelabelpg2label1.setFont(Font.font("Comic Sans", FontWeight.BOLD, 10));

    invisiblelabelpg2label2.setStyle("-fx-background-color: linear-gradient(#86C1B9, #7CAFC2); -fx-background-: 40;");
    invisiblelabelpg2label2.setText("The Most Compassionate");
    invisiblelabelpg2label2.setFont(Font.font("Comic Sans", FontWeight.BOLD, 10));

    invisiblelabelpg2label3.setStyle("-fx-background-color: linear-gradient(#86C1B9, #7CAFC2); -fx-background-: 40;");
    invisiblelabelpg2label3.setText("The Most Merciful");
    invisiblelabelpg2label3.setFont(Font.font("Comic Sans", FontWeight.BOLD, 10));

    invisiblelabelpg2label4.setStyle("-fx-background-color: linear-gradient(#86C1B9, #7CAFC2); -fx-background-: 40;");
    invisiblelabelpg2label4.setText("Master of the Day of Judgement");
    invisiblelabelpg2label4.setFont(Font.font("Comic Sans", FontWeight.BOLD, 10));

    invisiblelabelpg2label5.setStyle("-fx-background-color: linear-gradient(#86C1B9, #7CAFC2); -fx-background-: 40;");
    invisiblelabelpg2label5.setText("We only worship You and only ask You for help");
    invisiblelabelpg2label5.setFont(Font.font("Comic Sans", FontWeight.BOLD, 10));

    invisiblelabelpg2label6.setStyle("-fx-background-color: linear-gradient(#86C1B9, #7CAFC2); -fx-background-: 40;");
    invisiblelabelpg2label6.setText("Guide as along the Straight Path");
    invisiblelabelpg2label6.setFont(Font.font("Comic Sans", FontWeight.BOLD, 10));

    invisiblelabelpg2label7.setStyle("-fx-background-color: linear-gradient(#86C1B9, #7CAFC2); -fx-background-: 40;");
    invisiblelabelpg2label7.setText("The Path");
    invisiblelabelpg2label7.setFont(Font.font("Comic Sans", FontWeight.BOLD, 10));

    invisiblelabelpg2label8.setStyle("-fx-background-color: linear-gradient(#86C1B9, #7CAFC2); -fx-background-: 40;");
    invisiblelabelpg2label8.setText("of those you have blessed");
    invisiblelabelpg2label8.setFont(Font.font("Comic Sans", FontWeight.BOLD, 10));

    invisiblelabelpg2label9.setStyle("-fx-background-color: linear-gradient(#86C1B9, #7CAFC2); -fx-background-: 40;");
    invisiblelabelpg2label9.setText("Not those");
    invisiblelabelpg2label9.setFont(Font.font("Comic Sans", FontWeight.BOLD, 10));

    invisiblelabelpg2label10.setStyle("-fx-background-color: linear-gradient(#86C1B9, #7CAFC2); -fx-background-: 40;");
    invisiblelabelpg2label10.setText("You are displeased with or those who are astray");
    invisiblelabelpg2label10.setFont(Font.font("Comic Sans", FontWeight.BOLD, 10));

}

@FXML
private void changeTexttoArabic() {
    invisiblelabelpg2header.setText(null);
    invisiblelabelpg2header.setStyle(null);

    invisiblelabelpg2label1.setText(null);
    invisiblelabelpg2label1.setStyle(null);

    invisiblelabelpg2label2.setText(null);
    invisiblelabelpg2label2.setStyle(null);

    invisiblelabelpg2label3.setText(null);
    invisiblelabelpg2label3.setStyle(null);

    invisiblelabelpg2label4.setText(null);
    invisiblelabelpg2label4.setStyle(null);

    invisiblelabelpg2label5.setText(null);
    invisiblelabelpg2label5.setStyle(null);

    invisiblelabelpg2label6.setText(null);
    invisiblelabelpg2label6.setStyle(null);

    invisiblelabelpg2label7.setText(null);
    invisiblelabelpg2label7.setStyle(null);

    invisiblelabelpg2label8.setText(null);
    invisiblelabelpg2label8.setStyle(null);

    invisiblelabelpg2label9.setText(null);
    invisiblelabelpg2label9.setStyle(null);

    invisiblelabelpg2label10.setText(null);
    invisiblelabelpg2label10.setStyle(null);


}

}


Solution

  • There is too few code to actually get your problem. In my understanding of your problem, there are two ways to go:

    1) you can use a method, that constructs the labels with styling and the like. You than can use a loop to create the labels - or style them - and you were able to store them in an array or List.

    For this solution I'd recommend to save Font and Style in an class variable (field), as you say, they don't change. The method would then be something like this to create the Labels

    Label createLabel(String text) {
        Label lbl = new Label(text);
        lbl.setStyle...
        ....
        return lbl;
    }
    
    Label[] labels = new Label[10];
    for (int i = 0; i < labels.size; i++)
        labels[i] = createLabel(getTextFromSomewhere());
    

    You then can iterate over the array (or List) to change the text. You also can create simple methods to do what you need to do for all labels and call these within simple for or for-each loops.

    2) As you use a certain styling you may subclass Label and then have the possibility to style the labels using a css sheet. So you won't need all the styling code anymore. As you always have a group of labels that need to get the same actions, go for 1) to address the group (array, List,...)