关键词:
【中文标题】在控制器中编辑文本区域【英文标题】:Editing Text Area within Controller 【发布时间】:2021-06-07 22:40:54 【问题描述】:我是 Java 和 JavaFX 的新手。我已经使用 SceneBuilder/FXML 创建了一个 JavaFX 项目,并且我试图在程序开始时添加一个实时时钟,该时钟在屏幕顶部的整个程序中运行。我创建了一个文本区域并尝试从给定的代码中添加时钟功能,但每次启动程序时,它总是显示为空白。即使尝试手动使用 .setText("string") 函数也不起作用,所以我认为我将代码放在错误的位置。如果可能的话,有人能告诉我这段代码应该去哪里或指向正确的方向吗?
这是我的主要内容:
package application;
import java.lang.ModuleLayer.Controller;
import java.util.Date;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
public class Main extends Application
public static Stage stage = null;
@Override
public void start(Stage stage) throws Exception
Parent root = FXMLLoader.load(getClass().getResource("/Ui.fxml"));
Scene scene = new Scene(root);
stage.initStyle(StageStyle.UNDECORATED);
stage.setScene(scene);
this.stage = stage;
stage.show();
public static void main(String[] args)
launch(args);
这是我的控制器代码:
package application;
import java.util.Date;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.TextArea;
public class UiController
@FXML
private TextArea clockTextArea;
@FXML
private TextArea transactionLog;
@FXML
private TextField recipentField;
@FXML
private Button payButton;
@FXML
private Button requestButton;
@FXML
private TextField commentField;
private void refreshClock()
Thread refreshClock = new Thread()
public void run()
while (true)
Date dte = new Date();
String topMenuStr = " " + dte.toString();
clockTextArea.setText(topMenuStr);
try
sleep(3000L);
catch (InterruptedException e)
// TODO Auto-generated catch block
e.printStackTrace();
// end while ( true )
// end run thread
;
refreshClock.start();
public void initialize()
TextArea clockTextArea = new TextArea();
refreshClock();
【问题讨论】:
您无法从后台线程更新 UI。 hmm ...这看起来与***.com/questions/66540928/… 非常相似(包括我评论过的错误)-您是否创建了一个新帐户来重新发布?如果是这样,请不要 - 而是编辑以前的内容以使其可回答。至少,修复已经发现的错误..顺便说一句:创建一个本地文本字段并且不以任何方式使用它(初始化)是..没用的。 【参考方案1】:需要注意的是,无论是在Swing还是JavaFX中,更新控件的时候,都需要在事件派发线程EDT中进行,否则将不起作用或抛出异常Not on FX application thread; currentThread = *
此时只需将更新控制代码的代码放在事件派发线程中即可完成。在 JavaFX 中使用以下代码
Platform.runLater(new Runnable()
@Override
public void run()
//Update the control code of JavaFX and put it here
);
所以改变你的代码:
Date dte = new Date();
String topMenuStr = " " + dte.toString();
Platform.runLater(() -> clockTextArea.setText(topMenuStr));
并检查它是否有效。
【讨论】:
【参考方案2】:一种方法是使用Timeline
来处理时钟。它将独立于您的其他代码。
主要
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
/**
* JavaFX App
*/
public class App extends Application
@Override
public void start(Stage stage)
try
FXMLLoader fxmlLoader = new FXMLLoader(App.class.getResource("primary.fxml"));
Scene scene = new Scene(fxmlLoader.load(), 1080, 720);
stage.setScene(scene);
stage.show();
catch (IOException ex)
System.out.println(ex.toString());
public static void main(String[] args)
launch();
主控制器
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.util.Duration;
public class PrimaryController
@FXML Label lblClock;
Timeline clockHandler;
@FXML
void initialize()
Locale locale_en_US = Locale.US ;
DateTimeFormatter formatterUS = DateTimeFormatter.ofLocalizedTime( FormatStyle.SHORT ).withLocale( locale_en_US ) ;
clockHandler = new Timeline(
new KeyFrame(Duration.seconds(1), (ActionEvent event) ->
LocalTime currentTime = LocalTime.now();
String outputUs = currentTime.format(formatterUS);
lblClock.setText(outputUs);
));
clockHandler.setCycleCount(Timeline.INDEFINITE);
clockHandler.play();
初级 FXML
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<VBox alignment="TOP_CENTER" prefHeight="720.0" prefWidth="1080.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sed.home.javafxrealtimeclock.PrimaryController">
<children>
<MenuBar>
<menus>
<Menu mnemonicParsing="false" text="File">
<items>
<MenuItem mnemonicParsing="false" text="Close" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Edit">
<items>
<MenuItem mnemonicParsing="false" text="Delete" />
</items>
</Menu>
<Menu mnemonicParsing="false" text="Help">
<items>
<MenuItem mnemonicParsing="false" text="About" />
</items>
</Menu>
</menus>
</MenuBar>
<HBox>
<children>
<Label maxWidth="1.7976931348623157E308" HBox.hgrow="ALWAYS" />
<Label fx:id="lblClock" text="00:00">
<HBox.margin>
<Insets />
</HBox.margin>
</Label>
</children>
<VBox.margin>
<Insets left="5.0" right="5.0" />
</VBox.margin>
</HBox>
<AnchorPane prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS">
<children>
<Label layoutX="451.0" layoutY="331.0" text="The other parts of the program here" />
</children>
</AnchorPane>
</children>
</VBox>
【讨论】:
文本区域编辑中的问题
】文本区域编辑中的问题【英文标题】:Issuesintextareasediting【发布时间】:2012-12-3107:21:19【问题描述】:我正在处理用户输入(包括选项卡)的文本区域中制作一种文本编辑器。每次用户输入内容时,我都会运行一个paginate()函... 查看详情
多个文本区域上的 flex 编辑菜单操作
】多个文本区域上的flex编辑菜单操作【英文标题】:flexeditmenuoperationsonmultipletextareas【发布时间】:2010-10-1713:21:39【问题描述】:我有一个网格,其中一列将itemrenderer作为textarea。我的应用程序是菜单控制的。现在我想使用菜单... 查看详情
TinyMCE 在动态生成的文本区域上不可点击和编辑
】TinyMCE在动态生成的文本区域上不可点击和编辑【英文标题】:TinyMCEnotclickableandeditableondynamicallygeneratedtextarea【发布时间】:2019-12-2914:38:45【问题描述】:我有一组div元素用于旅行行程。我使用添加新按钮重复输入组。在每组... 查看详情
summernote 富编辑器文本区域高度变为零
】summernote富编辑器文本区域高度变为零【英文标题】:summernotericheditortextareaheightbecomezero【发布时间】:2018-07-2002:26:34【问题描述】:我正在使用https://summernote.org/富文本编辑器。将Summernote编辑器放置在引导模式中。在模态显示... 查看详情
jQuery - 从文本区域中删除用户输入文本
...描述】:好的,我有一个设备列表,我可以在其中选择要编辑的设备。我有3个状态可供编辑。未选择任何设备时、选择了1个设备或选择了x个设备时。我遇到的问题是,当用户在textarea(commentField)中键入一些文本并取消编辑以... 查看详情
单击元素并编辑内容(如文本区域)
】单击元素并编辑内容(如文本区域)【英文标题】:Clickonelementandeditthecontent(likeatextarea)【发布时间】:2013-05-2308:45:32【问题描述】:我想要一个包含一些文本的元素(从数据库调用),单击该元素并能够编辑文本,然后当我... 查看详情
虚拟键盘隐藏字段/文本区域/内容可编辑(隐藏在键盘下方)
】虚拟键盘隐藏字段/文本区域/内容可编辑(隐藏在键盘下方)【英文标题】:VirtualKeyboardhidesfields/textareas/contenteditable(hiddenbelowkeyboard)【发布时间】:2015-07-0113:28:57【问题描述】:我知道已经有一些***线程关于手机虚拟键盘隐藏... 查看详情
触摸文本区域时 UISearchDisplayController 不显示键盘
...】:2012-02-0607:28:04【问题描述】:我在嵌套在UITabBar下的控制器中有一个UITableView。到目前为止,交互都是在InterfaceBuilder中完成的,在视图切换方面没有以编程 查看详情
单击文本区域时,iOS 上的 reactstrap 模态关闭(仅在编辑时)
】单击文本区域时,iOS上的reactstrap模态关闭(仅在编辑时)【英文标题】:reactstrapModalclosesoniOSwhenclickingatextarea(onlywhenediting)【发布时间】:2021-12-1613:35:27【问题描述】:我们的网站正在进行反应,我们正在使用reactstrap。当您发... 查看详情
不允许在 Flex 中编辑 textarea 中的特定区域
】不允许在Flex中编辑textarea中的特定区域【英文标题】:DonotalloweditingatsepecificareaintextareainFlex【发布时间】:2011-12-2523:26:08【问题描述】:我只想不允许在flex中的某些文本区域进行编辑。怎么做?假设文本区域中的文本长度为50... 查看详情
HTML5:使可编辑的文本区域可拖动而没有副作用?
】HTML5:使可编辑的文本区域可拖动而没有副作用?【英文标题】:HTML5:makeaneditabletextzonedraggablewithnoside-effect?【发布时间】:2012-07-3007:04:15【问题描述】:我目前正在开发一个Web应用程序,在该应用程序中,我将拥有一个包含可... 查看详情
两个文本区域并排,最大尺寸填充整个覆盖网站,就像在崇高的文本编辑器中一样,有两列布局? [关闭]
...域并排,最大尺寸填充整个覆盖网站,就像在崇高的文本编辑器中一样,有两列布局?[关闭]【英文标题】:Twotextareasidebysidewithmaxsizethatfillplacewholecoveringwebsitelikeinsublimetexteditor,withtwocolumnslayout?[closed]【发布时间】:2021-10-1317:45:54... 查看详情
计算文本区域字符
】计算文本区域字符【英文标题】:Counttextareacharacters【发布时间】:2012-12-1417:10:38【问题描述】:我正在这个website上为我的文本区域开发字符数。现在,它说NaN,因为它似乎找不到字段中有多少个字符的长度,开头是0,所以... 查看详情
对同一页面中的所有文本区域重复summernote编辑器
】对同一页面中的所有文本区域重复summernote编辑器【英文标题】:repeatthesummernoteeditorforallthetextareasinsamepage【发布时间】:2016-02-1309:14:27【问题描述】:我有一个如下所示的cshtml视图页<divclass="form-group">@foreach(varfieldsinModel.Li... 查看详情
如何在反应中从文本区域获取选定的文本?
...时间】:2017-07-2523:08:48【问题描述】:我试图做一个文本编辑器中react.Does任何人知道如何从textarea的选定的文本,以便样式可以在选定text.I适用知道我们能在JavaScript中使用window.getSelection但我想知道如果任何其他方法可用于该功 查看详情
CKeditor 5文本区域未更新
...示例,将其作为更改触发。但是问题是它在加载时会触发编辑器中的内容,而不是我所做的更改。我假设在收集变量之前我需要告诉文本编辑器刷新或其他什么?有人知道我如何解决这个问题吗,这是我的代 查看详情
有没有办法使文本区域部分可编辑? (仅使部分文本可编辑)
】有没有办法使文本区域部分可编辑?(仅使部分文本可编辑)【英文标题】:Isthereawaytomakeatextareapartiallyeditable?(makeonlyportionsofthetexteditable)【发布时间】:2011-07-2315:01:05【问题描述】:我刚刚遇到了一种情况,在这种情况下,... 查看详情
ckeditor 更新文本区域
...r正常工作。显然它没有使用textarea所以提交表单时不会在编辑器中提交文本。因为我使用了多态关联等。我无法使用onsubmit函数来获取textarea的值(提交表单时)。于是我发现了这个问题:UsingjQuerytograbthecontentfromC 查看详情