用于TableView行的JavaFx淡入淡出

问题描述

这是我关于stackoverflow的第一个问题,请客气:)

我正在制作一个货币汇率桌面应用程序。每隔10秒,我就会使用API​​刷新表格视图。

我想对每个tableview行进行淡入淡出。当平均值大于前一个值时,该行将变为绿色。如果更少,它将为红色。示例在这里

https://examples.ext.net/#/SignalR/Basic/StockTicker/

我看到了这一点,但无法解决如何实施我的项目; Fade Transition via custom cell factory in javafx

代码

Currency.java

package model;

import javafx.beans.property.SimpleStringProperty;

public class Currency {
    private SimpleStringProperty dovizKuru;
    private SimpleStringProperty alis;
    private SimpleStringProperty satis;
    private SimpleStringProperty ortalama;
    private SimpleStringProperty gunlukDegisim;

public Currency(SimpleStringProperty dovizKuru,SimpleStringProperty alis,SimpleStringProperty satis,SimpleStringProperty ortalama,SimpleStringProperty gunlukDegisim) {
    super();
    this.dovizKuru = dovizKuru;
    this.alis = alis;
    this.satis = satis;
    this.ortalama = ortalama;
    this.gunlukDegisim = gunlukDegisim;
}


public String getDovizKuru() {
    return this.dovizKuru.get();
}


public void setDovizKuru(SimpleStringProperty dovizKuru) {
    this.dovizKuru = dovizKuru;
}


public String getAlis() {
    return this.alis.get();
}


public void setAlis(SimpleStringProperty alis) {
    this.alis = alis;
}


public String getSatis() {
    return this.satis.get();
}


public void setSatis(SimpleStringProperty satis) {
    this.satis = satis;
}


public String getortalama() {
    return this.ortalama.get();
}


public void setortalama(SimpleStringProperty ortalama) {
    this.ortalama = ortalama;
}


public String getGunlukDegisim() {
    return this.gunlukDegisim.get();
}


public void setGunlukDegisim(SimpleStringProperty gunlukDegisim) {
    this.gunlukDegisim = gunlukDegisim;
}

}

Api服务

package services;

import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONArray;
import org.json.JSONObject;

import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import model.Currency;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class CurrencyRateServices extends Service<ObservableList<Currency>> {
    public ObservableList<Currency> apiResponse() throws IOException{

    
    
    StringBuilder urlBuilder = new StringBuilder("https://api.yapikredi.com.tr/api/investmentrates/v1/currencyRates");
    URL url = new URL(urlBuilder.toString());
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    //Bearer 9e6f33ac-ddfc-4a5d-b55d-155a0f3f0e69
    conn.setRequestProperty("Authorization","TOKEN");
    System.out.println("Response code: " + conn.getResponseCode());
    BufferedReader rd;
    if(conn.getResponseCode() >= 200 && conn.getResponseCode() <= 300) {
        rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    } else {
        rd = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
    }
    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = rd.readLine()) != null) {
        sb.append(line);
    }
    rd.close();
    conn.disconnect();
    System.out.println(sb);
    
    ObservableList<Currency> currList= FXCollections.observableArrayList();
    JSONObject obj = new JSONObject(sb.toString());
    JSONObject obj2=obj.getJSONObject("response");
    JSONArray jsonArray=obj2.getJSONArray("exchangeRateList");
    for (int i = 0; i < jsonArray.length(); i++) {
        if (jsonArray.getJSONObject(i).getString("minorCurrency").equals("TL")) {
            Currency currency = new Currency(
                    new SimpleStringProperty(jsonArray.getJSONObject(i).getString("majorCurrency") + "/"+ jsonArray.getJSONObject(i).getString("minorCurrency")),new SimpleStringProperty(jsonArray.getJSONObject(i).getString("buyRate")),new SimpleStringProperty(jsonArray.getJSONObject(i).getString("sellRate")),new SimpleStringProperty(jsonArray.getJSONObject(i).getString("averageRate")),new SimpleStringProperty(jsonArray.getJSONObject(i).getString("changeRatioDaily"))

            );
            currList.add(currency);
        }
        
    }
    
    return currList;
}

@Override
protected Task<ObservableList<Currency>> createTask() {
    // Todo Auto-generated method stub
    return new Task<ObservableList<Currency>>() {
        protected ObservableList<Currency> call() throws Exception{
            return apiResponse();
        }
    };
}

}

MyController

package controller;

import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import java.util.ResourceBundle;
import application.DovizManager;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.util.Duration;
import model.Currency;
import services.CurrencyRateServices;
import view.ViewFactory;

public class DovizMainController extends BaseController implements Initializable{
    @FXML
    private TableView<Currency> dovizTableView;

@FXML
private TableColumn<Currency,String> col_DovizKuru;

@FXML
private TableColumn<Currency,String> col_Alis;

@FXML
private TableColumn<Currency,String> col_Satis;

@FXML
private TableColumn<Currency,String> col_Ortalama;

@FXML
private TableColumn<Currency,String> col_GunlukDegisim;

private ObservableList<Currency> crList;

public DovizMainController(DovizManager dovizManager,ViewFactory viewFactory,String fxmlName) {
    super(dovizManager,viewFactory,fxmlName);
}

@Override
public void initialize(URL arg0,ResourceBundle arg1) {
    setUpFirstTableView();
    setUpTableView();

    // Todo Auto-generated method stub
    
}
public void setUpFirstTableView() {
    //ObservableList<Currency> crList = null;

    CurrencyRateServices crService=new CurrencyRateServices();
    try {
        crList=crService.apiResponse();
    } catch (IOException e) {
        // Todo Auto-generated catch block
        e.printstacktrace();
    }
    dovizTableView.setItems(crList);
    col_DovizKuru.setCellValueFactory(new PropertyValueFactory<Currency,String>("dovizKuru"));
    col_Alis.setCellValueFactory(new PropertyValueFactory<Currency,String>("alis"));
    col_Satis.setCellValueFactory(new PropertyValueFactory<Currency,String>("satis"));
    col_Ortalama.setCellValueFactory(new PropertyValueFactory<Currency,String>("ortalama"));
    col_GunlukDegisim.setCellValueFactory(new PropertyValueFactory<Currency,String>("gunlukDegisim"));
    
    
}

public void setUpTableView() {
    Timeline timeline = new Timeline(new KeyFrame(Duration.millis(10000),new EventHandler() {
        

        @Override
        public void handle(Event arg0) {
            // Todo Auto-generated method stub


            CurrencyRateServices crService=new CurrencyRateServices();
            crService.start();
            crService.setonSucceeded(e->{
                ObservableList<Currency> crListNew=crService.getValue();
                for (int i = 0; i < crList.size(); i++) {
                    for (int j = 0; j < crListNew.size(); j++) {
                        if(crList.get(i).getDovizKuru().equals(crListNew.get(j).getDovizKuru())) {
                            System.out.println("başlıklar aynı");
                            
                            if(crList.get(i).getortalama()!=crListNew.get(j).getortalama()) {
                                System.out.println("ortalamalar aynı");
                                if(Double.parseDouble(crList.get(i).getortalama())>Double.parseDouble(crListNew.get(j).getortalama())) {
                                    
                                    crList.get(i).setAlis(new SimpleStringProperty(crListNew.get(j).getAlis()));
                                    crList.get(i).setGunlukDegisim(new SimpleStringProperty(crListNew.get(j).getGunlukDegisim()));
                                    crList.get(i).setortalama(new SimpleStringProperty(crListNew.get(j).getortalama()));
                                    crList.get(i).setSatis(new SimpleStringProperty(crListNew.get(j).getSatis()));
                                    
                                    dovizTableView.getItems().set(i,crList.get(i));
                                } else {
                                    crList.get(i).setAlis(new SimpleStringProperty(crListNew.get(j).getAlis()));
                                    crList.get(i).setGunlukDegisim(new SimpleStringProperty(crListNew.get(j).getGunlukDegisim()));
                                    crList.get(i).setortalama(new SimpleStringProperty(crListNew.get(j).getortalama()));
                                    crList.get(i).setSatis(new SimpleStringProperty(crListNew.get(j).getSatis()));
                                    
                                    dovizTableView.getItems().set(i,crList.get(i));
                                    
                                }
                                
                            }
                            
                        }else {continue;}
                    }
                    
                    
                }
                
            });



        }
        
    }));
    timeline.setCycleCount(Animation.INDEFINITE);
    timeline.play();

}

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)