JavaFX Observable列表在绑定到listview后没有正确更新GUI

时间:2018-02-22 18:09:37

标签: java user-interface javafx

问题

收到来自 function CheckBuyingCondition(prefix){ var Res= ""; var Current_Price_Open_ref = firebase.database().ref("dailyT/Current_Price_Open/"+nextDayTrading).orderByChild("Prefix").equalTo(prefix) return Current_Price_Open_ref.once("value").then(function(snapshot) { if(snapshot.exists()){ snapshot.forEach(function(childSnapshot) { var val = childSnapshot.val(); res =""+ val.Current_Price_Open; }); return res; }else{ res = "NA"; } }); } 类的消息后,会向MessageListener添加一条消息,该消息会在更改时更新GUI。问题是这种情况不一致,并且由于某些未知原因,似乎有2个列表在将消息添加到其列表时进行交换。

这是此行为的日志:

private final ObservableList<String>

如果我模拟INFO: Message received: Text: SSN= 1223 Requested amount= 11€ time= 110 ID: ID:78-192.168.0.23(84:be:e3:92:5b:b3)-1-1519325025180 Reply to: null Feb 22, 2018 7:43:45 PM Bank.ABNAmbro addMessages INFO: Adding message to List:SSN= 1223 Requested amount= 11€ time= 110 Feb 22, 2018 7:43:45 PM Bank.ABNAmbro addMessages INFO: New size: 2 Feb 22, 2018 7:43:54 PM Bank.BankMessageListener onMessage INFO: Message received: Text: SSN= 1223 Requested amount= 11€ time= 110 ID: ID:82-192.168.0.23(84:be:e3:92:5b:b3)-1-1519325034484 Reply to: null Feb 22, 2018 7:43:54 PM Bank.ABNAmbro addMessages INFO: Adding message to List:SSN= 1223 Requested amount= 11€ time= 110 Feb 22, 2018 7:43:54 PM Bank.ABNAmbro addMessages INFO: New size: 9 Feb 22, 2018 7:43:55 PM Bank.BankMessageListener onMessage INFO: Message received: Text: SSN= 1223 Requested amount= 11€ time= 110 ID: ID:86-192.168.0.23(84:be:e3:92:5b:b3)-1-1519325035575 Reply to: null Feb 22, 2018 7:43:55 PM Bank.ABNAmbro addMessages INFO: Adding message to List:SSN= 1223 Requested amount= 11€ time= 110 Feb 22, 2018 7:43:55 PM Bank.ABNAmbro addMessages INFO: New size: 10 Feb 22, 2018 7:43:57 PM Bank.BankMessageListener onMessage INFO: Message received: Text: SSN= 1223 Requested amount= 11€ time= 110 ID: ID:90-192.168.0.23(84:be:e3:92:5b:b3)-1-1519325037074 Reply to: null Feb 22, 2018 7:43:57 PM Bank.ABNAmbro addMessages INFO: Adding message to List:SSN= 1223 Requested amount= 11€ time= 110 Feb 22, 2018 7:43:57 PM Bank.ABNAmbro addMessages INFO: New size: 11 Feb 22, 2018 7:43:58 PM Bank.BankMessageListener onMessage INFO: Message received: Text: SSN= 1223 Requested amount= 11€ time= 110 ID: ID:94-192.168.0.23(84:be:e3:92:5b:b3)-1-1519325038499 Reply to: null Feb 22, 2018 7:43:58 PM Bank.ABNAmbro addMessages INFO: Adding message to List:SSN= 1223 Requested amount= 11€ time= 110 Feb 22, 2018 7:43:58 PM Bank.ABNAmbro addMessages INFO: New size: 3 类只需每隔几秒添加MessageListener,则日志会显示两个大小相同的列表。

String

Controller类:

Feb 22, 2018 9:12:16 PM Bank.ABNAmbro addMessages
INFO: Adding message to List:Test 0
Feb 22, 2018 9:12:16 PM Bank.ABNAmbro addMessages
INFO: New sieze: 0

Feb 22, 2018 9:12:16 PM Bank.ABNAmbro addMessages
INFO: Adding message to List:Test 0
Feb 22, 2018 9:12:16 PM Bank.ABNAmbro addMessages
INFO: New sieze: 0

Feb 22, 2018 9:12:17 PM Bank.ABNAmbro addMessages
INFO: Adding message to List:Test 1
Feb 22, 2018 9:12:17 PM Bank.ABNAmbro addMessages
INFO: New sieze: 1

Feb 22, 2018 9:12:17 PM Bank.ABNAmbro addMessages
INFO: Adding message to List:Test 1
Feb 22, 2018 9:12:17 PM Bank.ABNAmbro addMessages
INFO: New sieze: 1

Feb 22, 2018 9:12:18 PM Bank.ABNAmbro addMessages
INFO: Adding message to List:Test 2
Feb 22, 2018 9:12:18 PM Bank.ABNAmbro addMessages
INFO: New sieze: 2

Feb 22, 2018 9:12:18 PM Bank.ABNAmbro addMessages
INFO: Adding message to List:Test 2
Feb 22, 2018 9:12:18 PM Bank.ABNAmbro addMessages
INFO: New sieze: 2

消息监听:

package Bank;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.stage.Stage;

import javax.jms.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ABNAmbro extends Application implements Bank {

    private String tile = "bank";

    private List<String> messages = new ArrayList<String>();

    private final ObservableList<String> olMessages = FXCollections.observableArrayList(messages);

    Logger logger = Logger.getLogger("ABNAmbro");

    @FXML
    ListView lsRequestView;

    @Override
    public void start(Stage primaryStage) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("/Bank/Bank.fxml"));

        Scene scene = new Scene(root);

        primaryStage.setTitle(tile);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    @FXML
    public void initialize() {
        lsRequestView.setItems(olMessages);
    }

    public ABNAmbro() throws NamingException {

        tile = "ABNAmbro Loan Department.";
        String queuename = "ABNBankqueue";
        MessageListener messageListener = new BankMessageListener(this);

        InitialContext initialContext = new InitialContext();

        //JMS 2.0 lookup
        ConnectionFactory connectionFactory = (ConnectionFactory) initialContext.lookup("java:comp/DefaultJMSConnectionFactory");

        //Creating context to reseive messages
        JMSContext context = connectionFactory.createContext();
        Queue queue = (Queue) initialContext.lookup(queuename);

        //create consumer and add a message listener to it so it can get its message async 
        JMSConsumer consumer = context.createConsumer(queue);
        consumer.setMessageListener(messageListener);

    }

    @FXML
    public void SendResponse(ActionEvent event) {

    }

    public void addMessages(final String messages) {
        logger.log(Level.INFO,"Adding message to List:" + messages);
        Platform.runLater(new Runnable() {
            @Override
            public void run() {

                //adding message to list
                olMessages.add(messages);
            }
        });
        logger.log(Level.INFO, "New sieze: " + olMessages.size());
    }

}

0 个答案:

没有答案