parubok / swing-fx-properties

Adaptation of JavaFX properties for Swing.
20 stars 1 forks source link

[Enhancement] or [Feature] idk, Support for JList #1

Closed Kokecena closed 9 months ago

Kokecena commented 10 months ago

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 i use there image

And a video.

https://github.com/parubok/swing-fx-properties/assets/75457600/5f24bc6b-e9e8-4366-ae72-9d38ef2c7d40

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

parubok commented 9 months ago

Hi! Yes, you got it right as far as I can see. Since overall Swing components have a huge number of properties, I mainly select to implement those which I need in my project (closed source). I'll add JList 'selected value' property in the next release of the library (v1.25).

Thanks!