java - 用于 TableView 行的 JavaFx 淡入淡出
问题描述
这是我关于stackoverflow的第一个问题,所以请善待:)
我正在制作一个汇率桌面应用程序。使用 api,每 10 秒我刷新一次表格视图。
我想为每个 tableview 行淡入淡出。当平均值大于前一个值时,该行将变为绿色。如果更少,它将是红色的。例子在这里;
https://examples.ext.net/#/SignalR/Basic/StockTicker/
我看到了这一点,但无法解决如何实施我的项目;通过 javafx 中的自定义单元工厂淡化过渡
代码;
货币.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();
}
};
}
}
我的控制器
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();
}
}
解决方案
推荐阅读
- sql - 如何连接具有相同主键名称但不同值的两个表
- java - Java 是否保证放置在 thenApply 中的代码始终可以访问方法参数的正确示例?
- spring-boot - 在控制器初始化问题期间使用@Value Spring 注释
- java - AOSP 构建:hiddenapi:未指定 DEX 文件
- ruby-on-rails - 在 Docker 容器中使用特定版本的 Ruby on rails
- javascript - NodeJS:实现 OAuth 客户端逻辑的最佳方式是什么?
- pandas - 数据框到字典指导
- javascript - 更改音量时禁用 Windows 弹出窗口(来自网页上的 Javascript)
- javascript - 无法使用 React Router 创建嵌套选项卡路由
- swagger - 如何隐藏特定路径中的 swagger ref 属性?