I'm a long-time Python developer, and relied on Enthought's Traits/TraitsUI libraries for quick GUI development. I'm trying to understand if Swing has a similar implementation of Traits that would allow for easy delegation.
Imagine I have a JFrame that references to another class, Foo
, and has a textfield, fooText
. When the user changes fooText
, this triggers an event, and the change is propagated down to Foo
public class View extends javax.swing.JFrame {
private Foo foo;
private javax.swing.JTextField fooText;
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt){
foo.fooText = this.fooText.getText();
...
}
Where the Foo
class also shares the fooText
value:
public class Foo{
public String fooText;
}
Is it possible to synchronize the value of fooText
between the classes Foo
and View
? In the example above, I had to manually pass the new value in through an event listener. In the Python traits library, I can simply tell Foo to use delegation. Something like:
class Foo(HasTraits):
view = Instance(View);
String fooText = DelegatesTo(view)
In this way, rather than passing shared variables, I just pass a reference to the View
object to other objects in my program, and then any changes to View.fooText
are immediately propagated down to the delegating classes. This helps tremendously when UI fields need shared across many classes.
Is anything like this available in java/swing?
Thanks