JavaFX TextArea如何使用自动换行符设置文本 [英] JavaFX TextArea how to set text with automatic new line breaks

查看:2072
本文介绍了JavaFX TextArea如何使用自动换行符设置文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用了两个Tabs。在第一个我放置HtmlEditor,在第二个我放置TextArea。 HTML选项卡是默认的,当用户创建HTML输入时,他可以切换到TextArea以直接查看/更改HTML源代码。我添加了一个监听器来从HtmlEditor获取rhe htmlText并将其设置为TextArea中的文本,因此用户可以轻松地在HTML和源模式之间切换。这是我的倾听者:

In my application, I'm using two Tabs. In the first I placed HtmlEditor and in the second one I placed TextArea. HTML tab is default and when user is creating HTML input, he can switch into TextArea to see / change the HTML source code directly. I've added a listener to get rhe htmlText from the HtmlEditor and set it as text in TextArea, so user can easily switch between HTML and source mode. Here's my listener:

@FXML
private Tab htmlTab;

@FXML
private Tab sourceTab;

@FXML
private HTMLEditor htmlEditor;

@FXML
private TextArea textEditor;

        htmlTab.selectedProperty().addListener(new ChangeListener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
            if (htmlTab.isSelected()) {
                htmlEditor.setHtmlText(textEditor.getText());
            }
        }
    });

    sourceTab.selectedProperty().addListener(new ChangeListener<Boolean>() {
        @Override
        public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
            if (sourceTab.isSelected()) {
                textEditor.setText(htmlEditor.getHtmlText());
            }
        }
    });

它工作正常,但HtmlEditor会自动将文本分成行。当我切换到TextArea时,它全部在一行中。

It works fine, but HtmlEditor is breaking text into lines automatically. When I switch to TextArea, it's all in one line.

我考虑制作一个辅助方法,它采用TextArea长度属性来计算字符数并每次添加换行符n字符,但也许有更好的解决方案?

I thought about making a helper method which takes TextArea length attribute to count number of chars and adds new line character every "n" character, but maybe there is a better solution?

推荐答案

如果你只想要文本换行,请使用

If you just want the text to wrap, use

textEditor.setWrapText(true);

这篇关于JavaFX TextArea如何使用自动换行符设置文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆