Giter Club home page Giter Club logo

swing-fx-properties's Introduction

Java CI with Maven Latest Version javadoc

swing-fx-properties

Adaptation of JavaFX properties for Swing.

JavaFX introduced an improved approach to component properties handling. In JavaFX, component properties are type safe, referenced by method (and not via string name like in Swing) and, what is probably the most significant improvement, support binding.

The JavaFX properties implementation, in fact, is not JavaFX specific - it is a generic property mechanism which can be used for any JavaBean (see JavaBeanObjectPropertyBuilder).

This project took the relevant pieces of JavaFX properties code and added support for Swing - to allow usage of JavaFX-style properties and related functionality with Swing components.

Note: The default JavaFX handling of binding evaluation errors was changed from logging the error and returning some default value to throwing BindingEvaluationException. For some cases, this library also provides alternative methods which allow to specify a default value which is returned instead of throwing BindingEvaluationException. Example:

  • Bindings.valueAt(ObservableList<E> op, int index) - throws BindingEvaluationException in case of invalid index.
  • Bindings.valueAt(ObservableList<E> op, int index, E defaultValue) - returns the specified default value in case of invalid index.

The Swing properties are obtained via static methods of class SwingPropertySupport.

Example 1 (bind 'enabled' property of a label to 'selected' property of a checkbox, so the label is disabled when the checkbox is unselected and vice versa):

import static io.github.parubok.fxprop.SwingPropertySupport.enabledProperty;
import static io.github.parubok.fxprop.SwingPropertySupport.selectedProperty;

JCheckBox checkBox = new JCheckBox(); // unselected checkbox
JLabel label = new JLabel();
enabledProperty(label).bind(selectedProperty(checkBox));
// label is now disabled since the checkbox is not selected
checkBox.setSelected(true);
// label is now enabled

Example 2 (bind 'enabled' property of an action to 'selectedRowCount' property of a table, so the action is disabled when the table has no selected rows and vice versa):

import static io.github.parubok.fxprop.SwingPropertySupport.enabledProperty;
import static io.github.parubok.fxprop.SwingPropertySupport.selectedProperty;

Action action = ...; // for example, 'delete table rows' action
JTable table = ...;
enabledProperty(action).bind(selectedRowCountProperty(table).greaterThanOrEqualTo(1));

The following APIs were added to the original APIs of JavaFX:

  • Bindings.createObjectBinding(ObservableValue<K> value1, ObservableValue<T> value2, BiFunction<K, T, D> func)
  • Bindings.createObjectBinding(ObservableValue<D> value1, ObservableValue<F> value2, ObservableValue<G> value3, TriFunction<D, F, G, R> func)
  • Bindings.stringValueAt(ObservableMap<K, String> op, K key, String defaultValue)
  • Bindings.valueAt(ObservableMap<K, V> op, K key, V defaultValue)
  • Bindings.valueAt(ObservableList<E> op, int index, E defaultValue)
  • Bindings.valueAt(ObservableList<E> op, ObservableIntegerValue index, E defaultValue)
  • ObservableMap.valueAt(K key, V defaultValue)
  • ObservableList.valueAt(int index, E defaultValue)
  • ObservableValue.asObject(Function<T, K> func)
  • ObservableValue.asBoolean(Predicate<T> predicate)
  • ObservableValue.asStringExpression(String format)

Note: As specified in bind method JavaDoc, JavaFX has all the bind calls implemented through weak listeners. This means the bound property can be garbage collected and stopped from being updated unless there is a strong reference pointing to it.

Includes a stand-alone demo io.github.parubok.fxprop.demo.Demo under test source sub-root.

Since JavaFX is licensed under GPL v2 with the Classpath exception, the same license applies to this project.

This project has no dependencies (except JUnit 5, for testing).

Requires Java 8 or later.

Installation

Releases are available in Maven Central

Maven

Add this snippet to the pom.xml dependencies section:

<dependency>
    <groupId>io.github.parubok</groupId>
    <artifactId>swing-fx-properties</artifactId>
    <version>1.25</version>
</dependency>

swing-fx-properties's People

Contributors

parubok avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

lvyitian

swing-fx-properties's Issues

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

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.

20231122_060422.mp4

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.