JavaFX如何处理titledpane expandproperty?(JavaFX how to handle titledpane expandproperty?)
有人给我一个暗示这个问题吗? 我正在测试一些应用程序,可以在我打开TitlePane时立即将文本写入TextArea。 当我点击titlepane2的标题时,我编写了我的应用程序,一些文本应该写在textArea2中,并且它工作正常。 但问题是当我点击titlepane1中的完成按钮时,如何写一些文字? 如您所见,如果单击完成按钮,将扩展titlepane2,并且应在textArea2中写入一些文本。 我不知道如何解决这个问题。请帮助我
import java.net.URL; import java.util.ResourceBundle; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.TextArea; import javafx.scene.control.TitledPane; /** * FXML Controller class * * @author James */ public class TitlePaneTestController implements Initializable { @FXML private TitledPane titlePane1; @FXML private TitledPane titlePane2; @FXML private TextArea textArea1; @FXML private TextArea textArea2; /** * Initializes the controller class. */ @Override public void initialize(URL url, ResourceBundle rb) { // TODO } @FXML private void handleTitlePane1Clicked() { textArea1.appendText("This is TitlePane 1"); } @FXML private void handleTitle1Finished() { titlePane1.setExpanded(false); titlePane2.setExpanded(true); } @FXML private void handleTitlePane2Clicked() { textArea2.appendText("This is TitlePane 1"); } }
以下是FXML文件
<?xml version="1.0" encoding="UTF-8"?> <?import java.lang.*?> <?import java.util.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="smallframework.TitlePaneTestController"> <children> <Accordion layoutX="107.0" layoutY="101.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> <panes> <TitledPane fx:id="titlePane1" animated="false" text="Step 1"> <content> <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"> <children> <VBox prefHeight="353.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> <children> <TextArea fx:id="textArea1" prefHeight="200.0" prefWidth="200.0" /> <Button mnemonicParsing="false" onAction="#handleTitle1Finished" text="Finish" /> </children> </VBox> </children> </AnchorPane> </content> </TitledPane> <TitledPane fx:id="titlePane2" animated="false" text="Step 2"> <content> <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"> <children> <VBox prefHeight="353.0" prefWidth="598.0"> <children> <TextArea fx:id="textArea2" prefHeight="200.0" prefWidth="200.0" /> <Button mnemonicParsing="false" text="Finish" /> </children> </VBox> </children> </AnchorPane> </content> </TitledPane> </panes> </Accordion> </children> </AnchorPane>
Is someone give me a hint for this question? I'm testing some application which can be write texts to a TextArea as immediate as I open a TitlePane. I coded my app when I click a title of titlepane2, some text should be written in the textArea2 and it's working fine. But the problem is how can some text be written when I click the finish button located in titlepane1? As you can see, if finish button clicked, titlepane2 is going to be expanded and some text should be written in textArea2. I have no idea how solve this..please help me
import java.net.URL; import java.util.ResourceBundle; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.TextArea; import javafx.scene.control.TitledPane; /** * FXML Controller class * * @author James */ public class TitlePaneTestController implements Initializable { @FXML private TitledPane titlePane1; @FXML private TitledPane titlePane2; @FXML private TextArea textArea1; @FXML private TextArea textArea2; /** * Initializes the controller class. */ @Override public void initialize(URL url, ResourceBundle rb) { // TODO } @FXML private void handleTitlePane1Clicked() { textArea1.appendText("This is TitlePane 1"); } @FXML private void handleTitle1Finished() { titlePane1.setExpanded(false); titlePane2.setExpanded(true); } @FXML private void handleTitlePane2Clicked() { textArea2.appendText("This is TitlePane 1"); } }
the following is FXML file
<?xml version="1.0" encoding="UTF-8"?> <?import java.lang.*?> <?import java.util.*?> <?import javafx.scene.*?> <?import javafx.scene.control.*?> <?import javafx.scene.layout.*?> <AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="smallframework.TitlePaneTestController"> <children> <Accordion layoutX="107.0" layoutY="101.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> <panes> <TitledPane fx:id="titlePane1" animated="false" text="Step 1"> <content> <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"> <children> <VBox prefHeight="353.0" prefWidth="598.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"> <children> <TextArea fx:id="textArea1" prefHeight="200.0" prefWidth="200.0" /> <Button mnemonicParsing="false" onAction="#handleTitle1Finished" text="Finish" /> </children> </VBox> </children> </AnchorPane> </content> </TitledPane> <TitledPane fx:id="titlePane2" animated="false" text="Step 2"> <content> <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"> <children> <VBox prefHeight="353.0" prefWidth="598.0"> <children> <TextArea fx:id="textArea2" prefHeight="200.0" prefWidth="200.0" /> <Button mnemonicParsing="false" text="Finish" /> </children> </VBox> </children> </AnchorPane> </content> </TitledPane> </panes> </Accordion> </children> </AnchorPane>
原文:https://stackoverflow.com/questions/34337001
最满意答案
我认为你所缺少的是JLS 17.4.4 :
监视器m上的解锁动作与m上的所有后续锁定动作同步(其中“后续”根据同步顺序定义)。
这与关于volatile变量的内容非常相似:
对易失性变量v(第8.3.1.4节)的写入与任何线程对v的所有后续读取同步(其中“后续”根据同步顺序定义)。
然后在17.4.5:
如果动作x与后续动作y同步,那么我们也有hb(x,y)。
......其中hb是“发生在之前”的关系。
然后:
如果一个动作发生在另一个动作之前,那么第一个动作在第二个动作之前是可见的并且在第
内存模型非常复杂,我并不认为是专家,但我的理解是引用部分的含义是你所展示的第二种模式是安全的,而变量不是易变的 - 实际上任何变量都是仅在同步块内修改和读取同一个监视器是安全的,不会出现波动。 更有趣的方面(对我而言)是变量值引用的对象内的变量会发生什么。 如果
Singleton
不是一成不变的,那么你仍然可能遇到问题 - 但这是一步之遥。更具体地说,如果两个线程在
INSTANCE
为空时调用getInstance()
,则其中一个线程将首先锁定监视器。 在解锁操作之前发生对INSTANCE
的非空引用的写入操作,并且在另一个线程的锁定操作之前发生解锁操作。 锁定操作发生在读取INSTANCE
变量之前,因此写入发生在读取之前...此时,我们保证写入对读取线程可见。I think what you're missing is this from JLS 17.4.4:
An unlock action on monitor m synchronizes-with all subsequent lock actions on m (where "subsequent" is defined according to the synchronization order).
Which is very similar to the bullet about volatile variables:
A write to a volatile variable v (§8.3.1.4) synchronizes-with all subsequent reads of v by any thread (where "subsequent" is defined according to the synchronization order).
Then in 17.4.5:
If an action x synchronizes-with a following action y, then we also have hb(x, y).
... where hb is the "happens-before" relation.
Then:
If one action happens-before another, then the first is visible to and ordered before the second.
The memory model is incredibly complicated and I don't claim to be an expert, but my understanding is that the implication of the quoted parts is that the second pattern you've shown is safe without the variable being volatile - and indeed any variable which is only modified and read within synchronization blocks for the same monitor is safe without being volatile. The more interesting aspect (to me) is what happens to the variables within the object that the variable's value refers to. If
Singleton
isn't immutable, you've still potentially got problems there - but that's one step removed.To put it more concretely, if two threads call
getInstance()
whenINSTANCE
is null, one of those threads will lock the monitor first. The write action of a non-null reference toINSTANCE
happens-before the unlock operation, and that unlock operation happens-before the lock operation of the other thread. The lock operation happens-before the read of theINSTANCE
variable, therefore the write happens-before the read... at which point, we are guaranteed that the write is visible to the reading thread.
相关问答
更多-
Java线程:是否所有共享变量都是挥发性的?(Java Threads: Should all shared variables be Volatile ? [duplicate])[2022-05-14]
为了简化一点: volatile只提供了可见性:当你读取一个volatile变量时,你会得到两个保证:(1)即使它是在另一个线程中执行的,你也会看到最新的写入变量,(2)在volatile写入之前的所有写操作也是可见的。 synchronized给你的可见性和原子性 - 线程观察synchronized块中使用同一个监视器在synchronized块中执行的操作将会看到所有这些操作,或者看不到它们。 所以要回答你的问题,不,如果一个变量被写入synchronized块中,你不需要将它标记为volatile ... -
在这种情况下,如果第一个检查位于同步块内(但是如果变量不是易失性的),则不会有一个线程可能看不到另一个线程执行的更改。 单独的易失性是不够的,因为您需要以原子方式执行多个操作。 但要小心! 你在这里所谓的双重锁定 - 一个常见的成语,不幸的是不能可靠地工作 。 我认为这是自Java 1.6以来发生了变化,但是这种代码可能还是有风险的。 编辑 :当变量是volatile时,该代码从JDK 5(不如我之前写的6)正常工作,但在JDK 1.4或更早版本中将无法正常工作。 Synchronization by it ...
-
是的,保证线程2将打印“完成”。 当然,如果在线程1中写入b实际上是在线程2中从b读取之前发生的,而不是在同一时间或更早的时间发生! 这里推理的核心是发生之前的关系 。 多线程程序执行被视为由事件组成。 事件可以通过发生之前的关系相关联,这表示一个事件发生在另一个事件之前。 即使两个事件不是直接相关的,如果你可以跟踪从一个事件到另一个事件之间的一连串发生关系,那么你可以说一个发生在另一个事件之前。 在您的情况下,您有以下事件: 线程1写入s 线程1写入b 线程2从b读取 线程2从s读取 以下规则发挥作用: ...
-
要了解线程安全有两个方面:(1)执行控制和(2)内存可见性。 第一个与执行代码(包括执行指令的顺序)以及是否可以同时执行的控制有关,第二个与当其他线程可见的内存中的效果有关时。 由于每个CPU在主存储器之间具有多级高速缓存,所以运行在不同CPU或内核上的线程可以在任何给定时刻看到“内存”,因为允许线程获得并处理主内存的私有副本。 使用synchronized可防止任何其他线程获取同一对象的监视器(或锁定),从而防止同时对象上的同步对象的任何和所有代码受到同时执行。 重要的是,同步还会创建一个“先发制人”的内 ...
-
原子/ volatile / synchronized之间有什么区别?(What is the difference between atomic / volatile / synchronized?)[2022-11-14]
你具体询问他们在内部工作 ,所以在这里你是: 无同步 private int counter; public int getNextUniqueIndex() { return counter++; } 它基本上从内存中读取值,将其递增并放回内存。 这可以单线程工作,但如今,在多核,多CPU,多级缓存的时代,它将无法正常工作。 首先它引入了竞争条件(几个线程可以同时读取值),还有可见性问题。 该值只能存储在“ 本地 ”CPU内存(某些缓存)中,而对于其他CPU /内核(以及线程)不可见。 这就是为 ... -
安全性由传递发生在以前的关系定义如下: 17.4.5。 发生在订单之前 可以通过先发生关系来排序两个动作。 如果一个动作发生在另一个动作之前,那么第一个动作在第二个动作之前是可见的并且在第 如果我们有两个动作x和y ,我们写hb(x,y)来表示x发生在y之前 。 如果x和y是同一个线程的动作,并且x在程序顺序中出现在y之前,那么hb(x,y) 。 从对象的构造函数的末尾到该对象的终结器(第12.6节)的开始有一个发生前的边缘。 如果动作x 与后续动作y 同步 ,那么我们也有hb(x,y) 。 如果是hb(x ...
-
我认为你所缺少的是JLS 17.4.4 : 监视器m上的解锁动作与m上的所有后续锁定动作同步(其中“后续”根据同步顺序定义)。 这与关于volatile变量的内容非常相似: 对易失性变量v(第8.3.1.4节)的写入与任何线程对v的所有后续读取同步(其中“后续”根据同步顺序定义)。 然后在17.4.5: 如果动作x与后续动作y同步,那么我们也有hb(x,y)。 ......其中hb是“发生在之前”的关系。 然后: 如果一个动作发生在另一个动作之前,那么第一个动作在第二个动作之前是可见的并且在第 内存模型非常 ...
-
如果helper不为null,那么什么可以确保代码能够看到帮助程序构造的所有效果? 没有volatile ,没有什么会这样做。 考虑: synchronized(this) { // 4 if (helper == null) { // 5 helper = new Helper(); // 6 } // 7 假设在内部将其作为第一个设置helper实现为非空值,然后调用构造函数以创建有效的Helper对象。 没有规则阻止这一点。 另一个线程可能看到helper为非null ...
-
a)bwteen声明一个(在线程之间共享)变量volatile和不变量有什么区别? 如果它不是易失性的,那么任何其他线程都不会看到一个线程对它的写入。 b)我们是否应该在从不同线程访问它时始终声明它是不稳定的? 除非始终从同步块访问它。 c)同步volatile变量是否有意义? 它确实如此,但只有在任何同步块之外也访问volatile变量。 a)声明AtomicReference volatile是否有意义? 如果您计划在运行时更改实例,那肯定会这样。 但是,通常最好有一个final AtomicRefer ...
-
它可能/将会破裂。 执行示例: T1: if(!cont.isLocked())返回true T2: set(someObject) T1: return cont.getObject(); 返回旧版本 更糟糕的是,由于T2和T1之间没有正确的先发生关系, cont.getObject()可能会返回对T2创建的新对象的引用,但指向不一致/未完全构造的对象。 你有一个典型的检查然后行动问题。 为什么不直接使用对象的易失性引用? public class Controller { private vo ...