简体   繁体   English

如何动态更改 JavaFX TextArea 中文本的前景色?

[英]How do I change the foreground color of text in a JavaFX TextArea dynamically?

I am using JavaFX 8 and have a read-only TextArea that displays either a confirmation message or an error message, depending on the situation.我正在使用 JavaFX 8 并且有一个只读的 TextArea,它根据情况显示确认消息或错误消息。 I want confirmation messages to have black text but I want error messages to have red text.我希望确认消息具有黑色文本,但我希望错误消息具有红色文本。 How do I do that in JavaFX 8?我如何在 JavaFX 8 中做到这一点? For the moment, I'd prefer to do it programmatically but I would certainly be interested in how that could be done in FXML as well for the next draft of the program.目前,我更愿意以编程方式执行此操作,但我当然会对如何在 FXML 中以及程序的下一个草稿中完成此操作感兴趣。

Essentially, I am displaying a tableView and letting the user edit the information in the cells of the TableView.本质上,我正在显示一个 tableView 并让用户编辑 TableView 的单元格中的信息。 If the user puts an invalid value in the cell, I want to give him/her a clear error message about what's wrong and I'd like that message to be red (or highlighted in some way).如果用户在单元格中输入了无效值,我想给他/她一个明确的错误消息,告诉他出了什么问题,我希望该消息为红色(或以某种方式突出显示)。 If they change the value in an acceptable way, I want a confirmation message that tells them they've changed the value successfully in the table and the database;如果他们以可接受的方式更改值,我需要一条确认消息,告诉他们已成功更改表和数据库中的值; I'd prefer that message is black (or similar).我希望该消息是黑色的(或类似的)。

I'm fairly new to JavaFX and am surprised to find that there is no setForeground() any more.我是 JavaFX 的新手,很惊讶地发现不再有 setForeground() 了。 How are we supposed to do this now?我们现在应该怎么做? Or should I be using some kind of HTML field?或者我应该使用某种 HTML 字段吗? I haven't played with those yet....我还没有玩过那些......

======================================================================= Followup question ================================================ ===================== 后续问题

I may be misunderstanding the intent of James_D's answer but I created TWO pseudoclasses, one for error messages and one for informational messages:我可能误解了 James_D 回答的意图,但我创建了两个伪类,一个用于错误消息,一个用于信息性消息:

private static final PseudoClass ERROR =                               PseudoClass.getPseudoClass("error");

private static final PseudoClass INFO = PseudoClass.getPseudoClass("info");   

I had hoped to be able to have a simple method that could write EITHER kind of message, informational or error, simply by passing the pseudoclass to the method.我曾希望能够拥有一个简单的方法,只需将伪类传递给该方法,即可编写任何一种消息、信息性消息或错误消息。 Here's the method:这是方法:

private void writeMessage(String message, PseudoClass pseudoClass) {

    messageArea.pseudoClassStateChanged(pseudoClass, true);
    messageArea.setText(message);
}

I invoke the method as follows for an error message:我按如下方式调用该方法以获得错误消息:

writeMessage("This is an error message.", ERROR);

and this way for an information message:并通过这种方式获取信息消息:

writeMessage("This is an information message.", INFO);

And this is what I added to my CSS file:这是我添加到我的 CSS 文件中的内容:

.text-area:error {
-fx-text-fill: red ;
-fx-font-weight: 700;
}   

.text-area:info {
-fx-text-fill: black;
-fx-font-weight: normal;
 }

Why don't my error messages show up as bold?为什么我的错误消息不显示为粗体? (All messages appear in normal font.) Why do some error messages not show up as red? (所有消息均以普通字体显示。)为什么有些错误消息不显示为红色? I consistently invoke writeMessage() the same way throughout.我始终以相同的方式调用 writeMessage() 。

In JavaFX, you style controls using CSS.在 JavaFX 中,您可以使用 CSS 设置控件样式。 The recommended way is to use an external style sheet, which separates the style (in the CSS file) from the logic (in Java).推荐的方法是使用外部样式表,它将样式(在 CSS 文件中)与逻辑(在 Java 中)分开。 You can also use an "inline" style by directly calling setStyle(...) on a control.您还可以通过在控件上直接调用setStyle(...)来使用“内联”样式。

According to the CSS documentation , TextArea s have a CSS property -fx-text-fill which defines the color of the text that is displayed.根据CSS 文档TextArea有一个 CSS 属性-fx-text-fill ,它定义了所显示文本的颜色。

So the "quick and dirty" way could be:所以“快速而肮脏”的方式可能是:

private TextArea messageArea ;

// ...

private void showMessage(String message, boolean error) {
    if (error) {
        messageArea.setStyle("-fx-text-fill: red ;") ;
    } else {
        messageArea.setStyle("");
    }
    messageArea.setText(message);
}

A better approach is to define your own CSS PseudoClass , and then to reference it in an external style sheet:更好的方法是定义您自己的 CSS PseudoClass ,然后在外部样式表中引用它:

private static final PseudoClass ERROR = PseudoClass.getPsuedoClass("error");

private TextArea messageArea ;

// ...

private void showMessage(String message, boolean error) {
    messageArea.pseudoClassStateChanged(ERROR, error);
    messageArea.setText(message);
}

Your CSS file can now define how error messages are styled:您的 CSS 文件现在可以定义错误消息的样式:

.text-area:error {
    -fx-text-fill: red ;
}

and then you can attach the stylesheet to the scene with然后你可以将样式表附加到场景中

Scene scene = ... ;
scene.getStylesheets().add("path/to/css/file");

textArea.setTextFill(Color.WHITE); works for me.为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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