简体   繁体   中英

How to get the content of a ScrollPane object

I am making a text editor in JavaFX.

My program has a TabPane, which contains tabs, and each tab has a TextArea inside a ScrollPane.

I need to be able to get the text content of the TextArea in order to save it to a .txt file, however I am running into problems.

Here's the bit of code that is supposed to retrieve the text contained in the TextArea for the currently selected tab:

tabPane.getSelectionModel().getSelectedItem().getContent().getContent().getText();

getSelectionModel().getSelectedItem() gets the currently selected tab, then the .getContent() gets the ScrollPane.

The next .getContent() is supposed to return the TextArea contained within the ScrollPane. However, NetBeans tells me that the ScrollPane getContent() method does not exist.

However, Oracle's JavaFX documentation ( http://docs.oracle.com/javafx/2/api/javafx/scene/control/ScrollPane.html#getContent() ) tells me that the getContent() method exists and is supposed to return the ScrollPane's contained node.

Does anyone have any idea how I am supposed to get the node contained within the selected tab's ScrollPane?

Thanks!

You need to cast the result of the getContent() calls to the appropriate node type if you want to access type specific methods on that node; for example:

((TextArea)
    ((ScrollPane)
        tabPane
            .getSelectionModel()
            .getSelectedItem()
            .getContent()
    ).getContent()
).getText();        

Some advice:

  1. You don't really need to place TextAreas in ScrollPanes as the TextArea will wrap its text and also gain an in-built scrollbars as needed.
  2. If you don't need a ScrollPane, don't use one as getting ScrollPanes to layout properly (correctly sizing both themselves and their content) can be challenging.

Here is some alternate code.

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;

public class ScrollableTabs extends Application {
    @Override public void start(Stage stage) {
        final TabPane tabPane = new TabPane();
        tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);
        tabPane.getTabs().setAll(
                makeTab("doe", "A deer, a female deer."),
                makeTab("ray", "A flash a golden light."),
                makeTab("me",  "A name a call myself.")
        );

        Label selectedLabel = new Label();
        selectedLabel.textProperty().bind(
                Bindings.select(
                        tabPane,
                        "selectionModel",
                        "selectedItem",
                        "content",
                        "text"
                )
        );

// listener alternative to binding.
//        tabPane
//                .getSelectionModel()
//                .selectedItemProperty()
//                .addListener((observable, oldTab, newTab) -> {
//            TextArea selectedArea = (TextArea) newTab.getContent();
//
//            selectedLabel.setText(
//                selectedArea.getText()
//            );
//        });

        tabPane.getSelectionModel().select(1);
        VBox.setVgrow(tabPane, Priority.ALWAYS);

        stage.setScene(
                new Scene(
                        new VBox(10,
                                selectedLabel,
                                tabPane
                        )
                )
        );

        stage.show();
    }

    private Tab makeTab(String title, String text) {
        Tab tab = new Tab(title);

        tab.setContent(
                new TextArea(text)
        );

        return tab;
    }

    public static void main(String[] args) { launch(args); }
}  

Notice how there is no ScrollPane in my code, and yet a scroll bar is still appropriately displayed for the TextArea.

金光闪闪

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM