user6162281
user6162281

Reputation:

How to initialize a TabPane (JavaFX)

In my GUI, I try to display a TabPane with other elements in it.

When calling the initialize-method of the specific scene, I can add the needed data to the first Tab.

How can I access the second one and add data there?`

Is there a method that gets called when u switch between the single tabs or is it possible to to it from the initialize method?

//all the fields

@FXML
private void initialize() {
    smsHandynumberColumn.setCellValueFactory(cellData -> cellData.getValue().numberProperty());
    smsHandyproviderColumn.setCellValueFactory(cellData -> cellData.getValue().providerProperty());
    displayHandyDetails(null);
    smsHandyList.getSelectionModel().selectedItemProperty()
            .addListener((observable, oldValue, newValue) -> displayHandyDetails(newValue));

//  providernameColumn.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
//  provideramountColumn.setCellValueFactory(cellData -> cellData.getValue().sizeProperty());
}

@FXML
private void displayHandyDetails(SmsHandy handy) {
    if (handy != null) {
        handyNumberLabel.setText(handy.getNumber());
        providerLabel.setText(handy.getProvider().getName());
        registerLabel.setText(handy.getDate().toString());
        receivedLabel.setText(Integer.toString(handy.getReceivedList().size()));
        sentLabel.setText(Integer.toString(handy.getSentList().size()));
        if (handy instanceof PrepaidSmsHandy) {
            balanceLabel.setText(Integer.toString(handy.getProvider().getCreditForSmsHandy(handy.getNumber()))
                    + " Cent on account");
            typeLabel.setText("Prepaid");
        } else {
            balanceLabel.setText(
                    Integer.toString(((TariffPlanSmsHandy) handy).getRemainingFreeSms()) + " free SMS left");
            typeLabel.setText("Tariff-Plan");
        }
    } else {
        handyNumberLabel.setText(" ");
        providerLabel.setText(" ");
        registerLabel.setText(" ");
        typeLabel.setText(" ");
        balanceLabel.setText(" ");
        receivedLabel.setText(" ");
        sentLabel.setText(" ");
    }
}

public void setMainApp(MainApp mainApp) {
    this.mainApp = mainApp;
    smsHandyList.setItems(mainApp.getHandyList());
}

}

So, this is the controller class. With the way it is, I can work obv. with the first tab pane (that is shown by default when u look at it).

FXML file: http://pastebin.com/MQ0C8EWD

Upvotes: 0

Views: 1224

Answers (2)

DVarga
DVarga

Reputation: 21799

Okay, I am not sure I understand the question properly.

If you are asking how to access GUI elements that are stored on the second Tab of your TabPane, the answer is:

TabPane is actually just a simple container, so if you insert two controls two the same Tab is (in terms of FXML injection) the same of you insert them to two separate Tabs.

I can see you have created a TableView in your FXML on the second tab like:

<TableView fx:id="providerList" layoutX="-11.0" layoutY="14.0" prefHeight="264.0" prefWidth="250.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">

So, if you say:

@FXML TableView providerList;

in the class body of your controller, then in the initialize method of the controller you will have a reference to that TableView (absolutely the same as you have done with smsHandyList on the first Tab).

This is what is called "FXML injection". You can check this post from Oracle how to connect FXML with Java code.

Upvotes: 0

JC97
JC97

Reputation: 1620

First you initialize your tabpane, and then you initialize all the tabs you want. You can set the title and content, but don't forget to add all the tabs to your tabPane.

 TabPane tabPane = new TabPane();
 Tab tab1 = new Tab();
 tab.setText("tab 1");
 tab.setContent(new Rectangle(200,200, Color.LIGHTSTEELBLUE));
 Tab tab2 = new Tab();
 tab.setText("tab 2");
 tab.setContent(new Rectangle(200,200, Color.RED));
 tabPane.getTabs().addAll(tab1, tab2);

more information: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TabPane.html

Upvotes: 1

Related Questions