0

I have multiple tabs serve1, server2, server3 which contain the same UI fields. Once a submit is pressed I wanted to validate the fields of all 3 tabs. What is the best way to do this, I can iterate over all the tabs but how to retrieve values of fields inside the tab.

//snippet - tabs creation

//IP address field

javafx.scene.control.TextField ipField = new javafx.scene.control.TextField();

Tab server1 = new Tab("server1");
server1.setContent(field);

Tab server2 = new Tab("server2");
server2.setContent(field);

Tab server3 = new Tab("server3");
server3.setContent(field);

//onSubmit

submitButton.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
            //validate IP address of all three servers here
            //how can I access the field here by iterating all three tabs?

}
});

Note: Not using fxml way of building UI

Thanks in advance

Slaw
  • 37,820
  • 8
  • 53
  • 80
raman
  • 11
  • 3
  • 2
    Create a model representing the data for each server. Then create a view which references the model (possibly via a view-model, depending on the architecture of your application—see MVC, MVP, MVVM, etc.). Then you can create as many instances of your view/model pair as you need. When you update a view it updates its associated model. With this setup you only need access to the model instances, which could be aggregated in a larger-scoped model class. Keep in mind that the model should have no knowledge of the view. – Slaw May 06 '20 at 21:51
  • https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx – SedJ601 May 06 '20 at 23:09
  • Generally it would be better to [validate input](https://stackoverflow.com/q/49918079/3992939). Can you delegate the validation ? `server1.validate()` or better `model1.validate()` ? – c0der May 07 '20 at 07:53
  • Thanks for the responses, I am able to address it. As the application's server tabs are dynamically added/removed. I assigned ID's for each field in the tab and accessing it by node.getChildren(). @Slaw Sure this is the way, I would refactor it. I guess it's bidirectional model binding. – raman May 07 '20 at 11:54

1 Answers1

0

If I understand your question correctly, you can use an EditField for each Tab

For example:

javafx.scene.control.TextField ipField1 = new javafx.scene.control.TextField();

javafx.scene.control.TextField ipField2 = new javafx.scene.control.TextField();

javafx.scene.control.TextField ipField3 = new javafx.scene.control.TextField();

Tab server1 = new Tab("server1"); server1.setContent(ipField1);

Tab server2 = new Tab("server2"); server2.setContent(ipField2);

Tab server3 = new Tab("server3"); server3.setContent(ipField3);

//onSubmit

submitButton.setOnAction(new EventHandler() {

        @Override
        public void handle(ActionEvent event) {
        //validate IP address of all three servers here
        String ipaddr_server1 = ipField1.getText();
        String ipaddr_server2 = ipField2.getText();
        String ipaddr_server3 = ipField3.getText();

} });
Maddy
  • 36
  • 2
  • Thanks Maddy, I was able to address it by using node.getChildren(). I tried to simplify the code so I had put only one field but each tab has bunch of same form fields and each tab is dynamically added/removed. – raman May 07 '20 at 11:56