Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFx model equals always return false

Tags:

java

model

javafx

I create an application, and I create model for ObservableList I want call the collection .contains() method which is requre equals() but this always return with false. I only put one field in the equals method. When I compare in if statement use getter method it returns true as I expect.

import java.util.Objects;
import javafx.beans.InvalidationListener;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.image.Image;

public class Client implements ObservableValue {

    private final SimpleStringProperty remoteAddress;
    private final SimpleStringProperty userName;
    private final boolean connected = true;
    private final ObservableList<String> logs
            = FXCollections.observableArrayList();
    private final ObservableList<Image> imageLogs
            = FXCollections.observableArrayList();

    public Client(String remoteAddress, String userName) {
        this.remoteAddress = new SimpleStringProperty(remoteAddress);
        this.userName = new SimpleStringProperty(userName);
    }

    public SimpleStringProperty remoteAddressProperty() {
        return remoteAddress;
    }

    public SimpleStringProperty userNameProperty() {
        return userName;
    }

    public final String getRemoteAddress() {
        return remoteAddress.get();
    }

    public final void setRemoteAddress(String name) {
        this.remoteAddress.set(name);
    }

    public final String getUserName() {
        return userName.get();
    }

    public final void setUserName(String name) {
        this.userName.set(name);
    }

    public ObservableList<String> getLogs() {
        return logs;
    }

    public ObservableList<Image> getImageLogs() {
        return imageLogs;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        return this.remoteAddress == ((Client) obj).remoteAddress;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 37 * hash + Objects.hashCode(this.remoteAddress);
        return hash;
    }
}
like image 912
eszik.k Avatar asked Feb 24 '26 21:02

eszik.k


1 Answers

You need to compare the actual string content of the properties:

@Override
public boolean equals(Object obj) {
    if (obj == null || getClass() != obj.getClass()) {
        return false;
    }
    return this.remoteAddress.get().equals(((Client) obj).remoteAddress.get());
}

@Override
public int hashCode() {
    int hash = 3;
    hash = 37 * hash + Objects.hashCode(this.remoteAddress.get());
    return hash;
}
like image 170
James_D Avatar answered Mar 04 '26 09:03

James_D



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!