logo
down
shadow

JavaFX observableArrayList is always overwritten


JavaFX observableArrayList is always overwritten

By : LazyChen
Date : November 18 2020, 03:49 PM
I wish did fix the issue. That's because every time you are instantiating controller class like this:
code :
FXMLChoiceCarController carController = new FXMLChoiceCarController();
public ObservableList<Car> carChoiceBoxData = FXCollections.observableArrayList();
public static ObservableList<Car> carChoiceBoxData = FXCollections.observableArrayList();


Share : facebook icon twitter icon
In JavaFX, is an observableArrayList thread safe?

In JavaFX, is an observableArrayList thread safe?


By : ไกด์ กิ๊กส์ เทวดา
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , For this particular use case - where the ObservableList is bound to a TableView, you must only access the list from the FX Application Thread. This is because changes to the list will result in changes to the TableView, and all changes to the scene graph must occur on the FX Application Thread.
So in this case, the question as to whether the ObservableList is thread safe is a moot point: since you can only access it from a single thread for other reasons, you don't need it to be.
Javafx TableView not loading data from observableArrayList

Javafx TableView not loading data from observableArrayList


By : Karan Darji
Date : March 29 2020, 07:55 AM
To fix the issue you can do You need a cell value factory. The columns need to know how to render the data in the cell. It's best if the table and columns are typed.
code :
public void createColums(TableView table,int size){
    for(int x = 1; x<=size; x++){ //careful with indexes
        TableColumn<ObservableList<String>,String> t = new TableColumn<>("Paramiter "+x);
        t.setPrefWidth(120);
        final int X = x;
        t.setCellValueFactory(p -> new SimpleStringProperty(p.getValue().get(X-1)));
        table.getColumns().add(t);
    }
    TableColumn t = new TableColumn("Target");
    t.setPrefWidth(120);
    table.getColumns().add(t);
}
Find changed object in JavaFX ObservableArrayList

Find changed object in JavaFX ObservableArrayList


By : Stéphanie Malherbe
Date : March 29 2020, 07:55 AM
I wish this helpful for you You can call getFrom() on the change to get the index of the changed item. I don't think there's a way to actually figure out which property changed (if you have more than one property listed in the extractor) or get the old value, but maybe this is enough.
If you need more, you could consider registering your own listeners with the list to track, which would be tricky but not impossible.
code :
import java.util.Random;
import java.util.stream.IntStream;

import javafx.beans.Observable;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener.Change;
import javafx.collections.ObservableList;

public class ListChangeListenerTest {

    public static void main(String[] args) {
        ObservableList<Item> itemList = FXCollections.observableArrayList(item -> new Observable[]{item.valueProperty()});
        itemList.addListener((Change<? extends Item> c) -> {
            while (c.next()) {
                if (c.wasUpdated()) {
                    int index = c.getFrom();
                    System.out.println("Updated item at "+index+" new value is "+itemList.get(index).getValue());
                }
            }
        });
        IntStream.rangeClosed(1, 1000).mapToObj(Item::new).forEach(itemList::add);

        Random rng = new Random();
        itemList.get(rng.nextInt(itemList.size())).setValue(rng.nextInt(10000));

    }

    public static class Item {
        private final IntegerProperty value = new SimpleIntegerProperty();
        public Item(int value) {
            setValue(value);
        }
        public final IntegerProperty valueProperty() {
            return this.value;
        }

        public final int getValue() {
            return this.valueProperty().get();
        }

        public final void setValue(final int value) {
            this.valueProperty().set(value);
        }



    }

}
import java.util.Random;
import java.util.stream.IntStream;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.value.ChangeListener;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener.Change;
import javafx.collections.ObservableList;

public class ListChangeListenerTest {

    public static void main(String[] args) {    

        ChangeListener<Number> valueListener = (obs, oldValue, newValue) -> {
            Item item = (Item) ((Property<?>) obs).getBean();
            System.out.println("Value for "+item+" changed from " + oldValue + " to "+newValue);
        };

        ObservableList<Item> itemList = FXCollections.observableArrayList();
        itemList.addListener((Change<? extends Item> change) -> {
            while (change.next()) {
                if (change.wasAdded()) {
                    for (Item item : change.getAddedSubList()) {
                        item.valueProperty().addListener(valueListener);
                    }
                }
                if (change.wasRemoved()) {
                    for (Item item : change.getRemoved()) {
                        item.valueProperty().removeListener(valueListener);
                    }
                }
            }
        });

        IntStream.rangeClosed(1, 1000).mapToObj(Item::new).forEach(itemList::add);

        Random rng = new Random();
        itemList.get(rng.nextInt(itemList.size())).setValue(rng.nextInt(10000));

    }

    public static class Item {
        private final IntegerProperty value = new SimpleIntegerProperty(this, "value");
        private final String id ;
        public Item(int value) {
            id = "Item "+value ;
            setValue(value);
        }
        public final IntegerProperty valueProperty() {
            return this.value;
        }

        public final int getValue() {
            return this.valueProperty().get();
        }

        public final void setValue(final int value) {
            this.valueProperty().set(value);
        }

        @Override
        public String toString() {
            return id ;
        }

    }

}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

import javafx.collections.ListChangeListener.Change;
import javafx.collections.ModifiableObservableListBase;

public class UpdatingObservableList<T> extends ModifiableObservableListBase<T> {

    private final List<T> list ;

    public UpdatingObservableList(List<T> list) {
        this.list = list ;
    }

    public UpdatingObservableList() {
        this(new ArrayList<>());
    }

    public void updateSublist(int start, int end, Consumer<T> updater) {
        if (start < 0) throw new ArrayIndexOutOfBoundsException("Start ("+start+") cannot be < 0");
        if (end < start) throw new IllegalArgumentException("End ("+end+") cannot be less than start ("+start+")");
        if (end > size()) throw new ArrayIndexOutOfBoundsException("End ("+end+") cannot be greater than list size ("+size()+")");

        for (T element : list.subList(start, end)) {
            updater.accept(element);
        }

        fireChange(createUpdate(start, end));
    }

    @Override
    public T get(int index) {
        return list.get(index);
    }

    @Override
    public int size() {
        return list.size();
    }

    @Override
    protected void doAdd(int index, T element) {
        list.add(index, element);
    }

    @Override
    protected T doSet(int index, T element) {
        return list.set(index, element);
    }

    @Override
    protected T doRemove(int index) {
        return list.remove(index);
    }

    private Change<T> createUpdate(int start, int end) {
        return new Change<T>(this) {

            private boolean initialState = true ;

            @Override
            public boolean next() {
                if (initialState) {
                    initialState = false ;
                    return true ;
                }
                return false ;
            }

            @Override
            public void reset() {
                initialState = true ;
            }

            @Override
            public int getFrom() {
                checkState();
                return start ;
            }

            @Override
            public int getTo() {
                checkState();
                return end ;
            }

            @Override
            public List<T> getAddedSubList() {
                checkState();
                return Collections.emptyList();
            }

            @Override
            public List<T> getRemoved() {
                checkState();
                return Collections.emptyList();
            }

            @Override
            protected int[] getPermutation() {
                checkState();
                return new int[0];
            }

            @Override
            public boolean wasAdded() {
                checkState();
                return false ;
            }

            @Override
            public boolean wasRemoved() {
                checkState();
                return false ;
            }

            @Override
            public boolean wasUpdated() {
                return true ;
            }

            @Override
            public boolean wasPermutated() {
                checkState();
                return false ;
            }

            @Override
            public int getRemovedSize() {
                checkState();
                return 0 ;
            }

            @Override
            public int getAddedSize() {
                checkState();
                return 0 ;
            }

            private void checkState() {
                if (initialState) {
                    throw new IllegalStateException("Must call Change.next()");
                }
            }

        };
    }
}
import java.util.Random;
import java.util.stream.IntStream;

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.ListChangeListener.Change;

public class ListChangeListenerTest {

    public static void main(String[] args) {    

        UpdatingObservableList<Item> itemList = new UpdatingObservableList<Item>();
        itemList.addListener((Change<? extends Item> change) -> {
            while (change.next()) {
                if (change.wasUpdated()) {
                    for (int i = change.getFrom() ; i < change.getTo() ; i++) {
                        System.out.println(itemList.get(i) + " updated - new value: "+itemList.get(i).getValue());
                    }
                }
            }
        });

        IntStream.rangeClosed(1, 1000).mapToObj(Item::new).forEach(itemList::add);

        Random rng = new Random();
        int start = rng.nextInt(itemList.size());
        int end = Math.min(itemList.size(), start + 1 + rng.nextInt(15));
        itemList.updateSublist(start, end, item -> item.setValue(rng.nextInt(10000)));
    }

    public static class Item {
        private final IntegerProperty value = new SimpleIntegerProperty(this, "value");
        private final String id ;
        public Item(int value) {
            id = "Item "+value ;
            setValue(value);
        }
        public final IntegerProperty valueProperty() {
            return this.value;
        }

        public final int getValue() {
            return this.valueProperty().get();
        }

        public final void setValue(final int value) {
            this.valueProperty().set(value);
        }

        @Override
        public String toString() {
            return id ;
        }

    }

}
How to get selected value in observableArrayList type from ChoiceBox in JavaFX

How to get selected value in observableArrayList type from ChoiceBox in JavaFX


By : Cönjeh Jb
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further May be you can try THIS LINK. Here they explain:
How to create ChoiceBox(); How to use one How to add listeners How to retrieve data
How to populate FXCollections.observableArrayList with loop? [javafx]

How to populate FXCollections.observableArrayList with loop? [javafx]


By : Geoff
Date : March 29 2020, 07:55 AM
may help you . Create the list once, and add an element to it each time you iterate through the loop:
Related Posts Related Posts :
  • Multiple Consumers Spring Kafka
  • Bitbucket pipeline: environmental variables in build.gradle file not recognized
  • Java static enum method to return a default enum value
  • What is the complexity of empty for loop?
  • Group randomly List of Lists as N subsets with K elements in each
  • How to set texture on a shape drawn by glDrawArrays()?
  • How to make a jlabel resizable according to the window
  • Java Mathematical Expression Syntax
  • assigning values to characters in java
  • Method overriding and Inheritance in java
  • given a set of lists/groups with a series of number, find matching numbers
  • Connection to HID USB device (keyboard and mouse) in android
  • Inject HttpServletRequest in CDI SessionScoped bean
  • Pool game in java - ball collision algorithm
  • Focus ScrollView to selected position programmatically - Android
  • Is it hibernate bug?
  • Glib memory allocation error
  • Android Proguard - step by step
  • Setting the pivot point of a JPanel to its center
  • java check time is greater time
  • how to save a screenshot (matlab)
  • Anti-aliasing filled shapes in libgdx
  • Gwt custom text box having baloon popup
  • How to change date dynamically for each element in an Array
  • JPanel won't add the JLabel text?
  • how to access the .properties file in ant if it is in different location
  • Reduce application memory footprint
  • java.lang.ClassCastException: javax.mail.Session cannot be cast to javax.mail.Session
  • What is the exact purpose of calling System.exit() in java
  • How to do you get output from Javascript into Java using ScriptEngine
  • Java replaceAll() method to escape special characters
  • Java (Removing a 'keyword' from the alphabet)
  • Format BigDecimal in Spring
  • iterating checkbox values stored in array using EL
  • Java - Jackcess API with .accde(MS Access) format
  • Alternative for some of Struts 1 methods in Struts 2
  • javax.crypto JDK source code, again
  • Spring Roo - Command 'service' not found
  • antlr4: ATN version 2 expected 3
  • Deploy GlassFish using Netbeans generate java.net.MalformedURLException: Bad URL path
  • why is wait() called for loop in Thread.join() of Java?
  • Java JAXB marshall into DOM Document
  • Why JDBI 3 @ColumnName annotation doesn't work?
  • Java SimpleDateformatter with 10 decimals after the seconds, cannot convert to Date
  • Getting array index out of bounds when splitting a string in android
  • Verify if csv file is updated successfully
  • Get the parameter names of an interface method
  • How to retrieve object state from serialized Java objects without class file(s)
  • How to move a programmed game for pc to android
  • Size of an object and a string in Java
  • PrimeFaces Datatable Right Click Select + Row Specific Context Menu
  • How to use OOP to branch from a point class into lines and triangles (Java)
  • What does 'String...' mean?
  • Add YouTube Data API to Android Studio
  • JComboBox in JTable not displaying options, only the correct number of blank options
  • JTable and removeAll behavior
  • Google App Engine: Can I programmatically find out if this is the default version?
  • How do you open a program by using ActionListener in Java?
  • Android SipManager: android.net.sip.SipException: SipService.createSession() returns null
  • Forcefully terminating a thread I didn't write in Java
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org