You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello,
I created a JFrame composed of one View and two JTextField:
The View displays five nodes: four are just landmarks and are not expected to be moved by the user ("fixed_*"), and one is there to be moved by the user ("unfixed"). The two JTextField show the coordinates of the "unfixed" node. Both the View and the JTextField have to be synchronized with each other. Indeed, when the user moves the "unfixed" node in the View, then the two JTextField have to be updated accordingly :
Reciprocally, when the user modifies the coordinate in one of the JTextField, then the View has to be updated accordingly too:
Here are four test cases:
Test case 1: the user runs the application and moves the "unfixed" node in the View.
Test case 2: the user runs the application and modifies the coordinate in one of the JTextField.
Test case 3: the user runs the application, modifies the coordinate in one of the JTextField, and then moves the "unfixed" node in the View.
Test case 4: the user runs the application, moves the "unfixed" node in the View, and then modifies the coordinate in one of the JTextField.
Test cases 1, 2 and 3 work fine, but test case 4 doesn't work. Actually, in test case 4, once the user moved the "unfixed" node in the View, then the modification of the coordinate in one of the JTextField doesn't update the View.
I tried to analyze what differs between the execution of test cases 3 and 4. To do so, I printed the name of the current thread at different places of the code. I saw that modifications through the JTextField are run on thread "AWT-EventQueue-0" (this is the Event Dispatch Thread of Swing, isn't it?), and that modifications through the View are run on thread "Thread-0". In my implementation, "Thread-0" is the thread on which I run the GraphStream's pumping loop, to wait for events occurred in the GraphStream's Viewer thread and to copy back them inside "Thread-0". From what I understood of GraphStream's documentation:
the GraphStream's Viewer always runs in the Event Dispatch Thread (EDT) of Swing,
the GraphStream's Graph associated to a GraphStream's Viewer can be accessed either from the EDT or from another thread, depending on the GraphStream's ThreadingModel used,
it is more powerful to access the GraphStream's Graph from another thread than the EDT, because it allows running algorithms on the graph in parallel with the GraphStream's Viewer.
Did I understand well the documentation?
In my implementation, I chose to access the GraphStream's Graph from another thread than the Swing one. Thus, I deduce from my previous runs of test cases 3 and 4 that:
the update of the GraphStream's View from the EDT doesn't prevent a future update of the GraphStream's View from "Thread-0" (test case 3),
but the update of the GraphStream's View from "Thread-0" blocks any future update of the GraphStream's View from the EDT (test case 4).
I have the impression that I'm doing something wrong with all these threads. Could you help me please?
I tried to make a minimum working example (MWE) to reproduce my problem. Here is the content of the Java source file NodeSyncTest.java:
and here is the one of the the Maven POM file pom.xml to build an executable JAR including all dependencies:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>mwe</groupId>
<artifactId>mwe</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>MWE</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
</dependency>
<dependency>
<groupId>org.graphstream</groupId>
<artifactId>gs-core</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.graphstream</groupId>
<artifactId>gs-ui-swing</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- Build an executable JAR -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!-- here we specify that we want to use the main
method within the App class -->
<mainClass>mwe.NodeSyncTest</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>mwe.NodeSyncTest</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
To use these two files, just create a folder mwe/, put NodeSyncTest.java into mwe/src/main/java/mwe/ and pom.xml into mwe/, and, in mwe/, run mvn compile assembly:single
and java -jar target/mwe-0.0.1-SNAPSHOT-jar-with-dependencies.jar
Here is the full project folder: MWE.zip
Hello,
I created a JFrame composed of one View and two JTextField:
The View displays five nodes: four are just landmarks and are not expected to be moved by the user ("fixed_*"), and one is there to be moved by the user ("unfixed"). The two JTextField show the coordinates of the "unfixed" node. Both the View and the JTextField have to be synchronized with each other. Indeed, when the user moves the "unfixed" node in the View, then the two JTextField have to be updated accordingly :
Reciprocally, when the user modifies the coordinate in one of the JTextField, then the View has to be updated accordingly too:
Here are four test cases:
Test cases 1, 2 and 3 work fine, but test case 4 doesn't work. Actually, in test case 4, once the user moved the "unfixed" node in the View, then the modification of the coordinate in one of the JTextField doesn't update the View.
I tried to analyze what differs between the execution of test cases 3 and 4. To do so, I printed the name of the current thread at different places of the code. I saw that modifications through the JTextField are run on thread "AWT-EventQueue-0" (this is the Event Dispatch Thread of Swing, isn't it?), and that modifications through the View are run on thread "Thread-0". In my implementation, "Thread-0" is the thread on which I run the GraphStream's pumping loop, to wait for events occurred in the GraphStream's Viewer thread and to copy back them inside "Thread-0". From what I understood of GraphStream's documentation:
Did I understand well the documentation?
In my implementation, I chose to access the GraphStream's Graph from another thread than the Swing one. Thus, I deduce from my previous runs of test cases 3 and 4 that:
I have the impression that I'm doing something wrong with all these threads. Could you help me please?
I tried to make a minimum working example (MWE) to reproduce my problem. Here is the content of the Java source file NodeSyncTest.java:
and here is the one of the the Maven POM file pom.xml to build an executable JAR including all dependencies:
To use these two files, just create a folder mwe/, put NodeSyncTest.java into mwe/src/main/java/mwe/ and pom.xml into mwe/, and, in mwe/, run
mvn compile assembly:single
and
java -jar target/mwe-0.0.1-SNAPSHOT-jar-with-dependencies.jar
Here is the full project folder:
MWE.zip
EDIT: I just posted my question on Stack Overflow (https://stackoverflow.com/q/77913500/3890496) to reach more readers. I will update this issue if I get answer from there.
The text was updated successfully, but these errors were encountered: