parubok / swing-fx-properties Goto Github PK
View Code? Open in Web Editor NEWAdaptation of JavaFX properties for Swing.
Adaptation of JavaFX properties for Swing.
Recently I have been using your library, it has simplified many things to activate or deactivate buttons in my application, recently I noticed that I wanted to deactivate a button when an item in a list is selected under a certain condition, but there was nothing to do it, I checked how to do it. you did with the JComboBox since it is slightly similar, and well it works for me, although I don't know if it is that effective or if you follow any rule to do it, but I did it like this
import io.github.parubok.swingfx.beans.property.ObjectProperty;
import io.github.parubok.swingfx.beans.property.SimpleObjectProperty;
import io.github.parubok.swingfx.beans.value.ChangeListener;
import javax.swing.*;
import javax.swing.event.ListSelectionListener;
import java.util.Objects;
import static io.github.parubok.fxprop.ClientProps.PROP_SELECTED_ITEM;
public abstract class SelectedListItemPropertyImpl {
private static final ListSelectionListener ITEM_LISTENER = e -> {
JList<?> list = (JList<?>) e.getSource();
ObjectProperty p = (ObjectProperty) list.getClientProperty(PROP_SELECTED_ITEM);
if (!Objects.equals(list.getSelectedValue(), p.get())) {
p.set(list.getSelectedValue());
}
};
private static final ChangeListener FX_PROP_LISTENER = (observable, oldValue, newValue) -> {
ObjectProperty<?> p = (ObjectProperty) observable;
JList<?> list = (JList) p.getBean();
if (!Objects.equals(newValue, list.getSelectedValue())) {
list.setSelectedValue(newValue,false);
}
};
static <E> ObjectProperty<E> getProperty(JList<E> list) {
Objects.requireNonNull(list, "list");
ObjectProperty<E> p = (ObjectProperty) list.getClientProperty(PROP_SELECTED_ITEM);
if (p == null) {
p = new SimpleObjectProperty<>(list, "selectedItem", list.getSelectedValue());
list.putClientProperty(PROP_SELECTED_ITEM, p);
list.addListSelectionListener(ITEM_LISTENER);
p.addListener(FX_PROP_LISTENER);
}
return p;
}
}
public abstract class ExtraSwingPropertySupport {
public static <E> ObjectProperty<E> selectedItemProperty(JList<E> list) {
return SelectedListItemPropertyImpl.getProperty(list);
}
}
And a video.
Maybe it is not the right approach or for some reason you have not added it, recently I am testing the benefits of JavaFX property binding, it would be good if most of the components could be simplified to support the property but from what I see it is difficult because which depends on the Listener of each Swing component
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.