- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个单元工厂包装器来启用 JavaFX 表格单元的自定义配置。请参阅下面的代码。
package idmas.controller.modules.tableview;
import idmas.model.Cml;
import idmas.model.CmlDAO;
import idmas.model.Damageloop;
import idmas.model.modules.general.dateMethods;
import java.util.Date;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.util.Callback;
/**
* Generic cell factory that can be used to override specific properties of cells, such as formatting (text size, color etc.) and even values.
* @author Joshua Adams
* @param <S> = Data type of table view to be modified. E.g. Damageloop, Equipment, CML, Inspection etc.
* @param <T> = Data type of specific cell being overridden. E.g. Date, Integer, Double etc.
*/
public class CellFactoryCustom<S, T> implements Callback<TableColumn<S, T>, TableCell<S, T>> {
//Represents the name of the column for the specific tableview, i.e. Last Internal Inspection Date etc.
String colname;
//Constructors for class wide objects
CmlDAO cmldao = new CmlDAO();
/**
* Class constructor requires a column name to be passed so the specific formatting for that column's cells can be applied.
* @param colname = Name of column to apply formatting to.
*/
public CellFactoryCustom (String colname) {
this.colname = colname;
}
/**
* Main method to override the properties of the cells for a table column.
* @param arg
* @return
*/
@Override
public TableCell<S, T> call(TableColumn<S, T> arg) {
TableCell<S, T> cell = new TableCell<S, T>() {
@Override
protected void updateItem(T item, boolean empty) {
//super allows reffering to methods in superclass
super.updateItem(item, empty);
//recommended syntax for overridding the updateItem method - Refer to update item javadoc.
if (empty || item == null) {
setText(null);
setGraphic(null);
getStyleClass().removeAll("highlightCellBlack");
getStyleClass().add(getTableRow().getStyleClass().toString());
} else {
setConditionalFormatting(this,item);
}
}
};
return cell;
}
/**
* Specific formatting rules which are applied to a columns cells based on its column name.
* @param cell
* @param item
*/
public void setConditionalFormatting (TableCell<S,T> cell, T item) {
//Constructors for reference classes
dateMethods datemethods = new dateMethods();
Date currentdateplus60 = new Date();
Date date;
Damageloop damageloop;
Cml cml;
ComboBox combobox;
//Current styles need to be removed to ensure all specific styles for each cell are applied correctly
cell.getStyleClass().removeAll("highlightCellBlack");
//Switch statement selected over if statement to improve performance of code and readibility
switch (colname) {
case "colNextExternalInspectionDate":
damageloop = (Damageloop) cell.getTableRow().getItem();
cell.setText(datemethods.getDateToString((Date) item,"dd/MM/yyyy"));
currentdateplus60 = datemethods.getDatePlusDays(currentdateplus60, 60);
date = (Date) item;
if(date.before(currentdateplus60) && damageloop.getErrorcode() != 2 & damageloop.getErrorcode() != 3) {
cell.getStyleClass().add("highlightCellBlack");
} else {
cell.getStyleClass().add(cell.getTableRow().getStyleClass().toString());
}
break;
case "colNextInternalInspectionDate":
damageloop = (Damageloop) cell.getTableRow().getItem();
cell.setText(datemethods.getDateToString((Date) item,"dd/MM/yyyy"));
currentdateplus60 = datemethods.getDatePlusDays(currentdateplus60, 60);
date = (Date) item;
if(date.before(currentdateplus60) && damageloop.getErrorcode() != 1 && damageloop.getErrorcode() != 3) {
cell.getStyleClass().add("highlightCellBlack");
} else {
cell.getStyleClass().add(cell.getTableRow().getStyleClass().toString());
}
break;
case "colCmlStatus":
cml = (Cml) cell.getTableRow().getItem();
String[] fieldsArray = new String[]{"C = Continue to Monitor", "S = Scoped", "X = Redundant"};
String[] disabledFieldsArray = new String[]{"S = Scoped"};
String SQLString = "UPDATE IDMAS.CML SET CMLSTATUS = '<NEW_STATUS>' WHERE EQUIPMENT_ID = '" + cml.getEquipmentId() + "' AND CML_NO = " + cml.getCmlNo();
String replacestring = "<NEW_STATUS>";
String defaultvalue = item.toString();
ComboBoxCustom comboboxcustom = new ComboBoxCustom (fieldsArray, disabledFieldsArray, SQLString, replacestring, defaultvalue);
comboboxcustom.setPrefWidth(10);
cell.setGraphic(comboboxcustom);
break;
case "colCmlStatusRemediation":
ObservableList<String> options = FXCollections.observableArrayList(
"A = Approved for Remediation",
"C = Continue to Monitor",
"F = Fit for Service",
"R = Recommend Remediation"
);
cml = (Cml) cell.getTableRow().getItem();
combobox = new ComboBox(options);
combobox.setValue(item.toString());
combobox.setPrefWidth(10);
combobox.valueProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue ov, String oldvalue, String newvalue) {
cml.setCmlstatusremediation(newvalue.charAt(0));
cmldao.updateCml(cml);
//Platform runlater required to update the combox with the new value
Platform.runLater(() -> {
combobox.setValue(String.valueOf(newvalue.charAt(0)));
});
}
});
cell.setGraphic(combobox);
break;
case "colTemporaryrepairinstalled":
cml = (Cml) cell.getTableRow().getItem();
CheckBox checkbox = new CheckBox();
checkbox.setSelected(Boolean.valueOf(item.toString()));
checkbox.selectedProperty().addListener(new ChangeListener<Boolean>() {
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
cml.setTemporaryrepairinstalled(newValue);
cmldao.updateCml(cml);
checkbox.setSelected(newValue);
}
});
cell.setGraphic(checkbox);
break;
default:
break;
}
}
}
这个 cellfactory 包装器通常可以在我的所有表格 View 中导航。但是,一旦我在系统中执行其他功能,例如添加新记录、编辑记录等,就会抛出以下错误。
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at idmas.controller.modules.tableview.CellFactoryCustom.setConditionalFormatting(CellFactoryCustom.java:110)
at idmas.controller.modules.tableview.CellFactoryCustom$1.updateItem(CellFactoryCustom.java:60)
at javafx.scene.control.TableCell.updateItem(TableCell.java:663)
at javafx.scene.control.TableCell.indexChanged(TableCell.java:468)
at javafx.scene.control.IndexedCell.updateIndex(IndexedCell.java:116)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.requestCellUpdate(TableRowSkinBase.java:659)
at com.sun.javafx.scene.control.skin.TableRowSkinBase.handleControlPropertyChanged(TableRowSkinBase.java:234)
at com.sun.javafx.scene.control.skin.TableRowSkin.handleControlPropertyChanged(TableRowSkin.java:70)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.lambda$registerChangeListener$61(BehaviorSkinBase.java:197)
at com.sun.javafx.scene.control.MultiplePropertyChangeListenerHandler$1.changed(MultiplePropertyChangeListenerHandler.java:55)
at javafx.beans.value.WeakChangeListener.changed(WeakChangeListener.java:89)
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(ExpressionHelper.java:182)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(ExpressionHelper.java:81)
at javafx.beans.property.ReadOnlyIntegerPropertyBase.fireValueChangedEvent(ReadOnlyIntegerPropertyBase.java:72)
at javafx.beans.property.ReadOnlyIntegerWrapper.fireValueChangedEvent(ReadOnlyIntegerWrapper.java:102)
at javafx.beans.property.IntegerPropertyBase.markInvalid(IntegerPropertyBase.java:113)
at javafx.beans.property.IntegerPropertyBase.set(IntegerPropertyBase.java:147)
at javafx.scene.control.IndexedCell.updateIndex(IndexedCell.java:115)
at com.sun.javafx.scene.control.skin.VirtualFlow.setCellIndex(VirtualFlow.java:1957)
at com.sun.javafx.scene.control.skin.VirtualFlow.addTrailingCells(VirtualFlow.java:1344)
at com.sun.javafx.scene.control.skin.VirtualFlow.layoutChildren(VirtualFlow.java:1197)
at javafx.scene.Parent.layout(Parent.java:1087)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Parent.layout(Parent.java:1093)
at javafx.scene.Scene.doLayoutPass(Scene.java:552)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2397)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$30(Toolkit.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:354)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:381)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:510)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:490)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$404(QuantumToolkit.java:319)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
我似乎无法弄清楚为什么这个包装器认为我的 CML 类中有空值。有人知道为什么会抛出这个空指针异常吗?
最佳答案
无法保证TableCell
已与 TableRow
关联或者 TableRow
当 updateItem
时已经填满方法第一次被调用。
在你的情况下可能 getTableRow()
返回null
(但这意味着 cml = (Cml) cell.getTableRow().getItem();
是引发异常的行,而不是 String SQLString = "UPDATE IDMAS.CML SET CMLSTATUS = '<NEW_STATUS>' WHERE EQUIPMENT_ID = '" + cml.getEquipmentId() + "' AND CML_NO = " + cml.getCmlNo();
);至少当我尝试重现该错误时发生了这种情况。
但是如果
String SQLString = "UPDATE IDMAS.CML SET CMLSTATUS = '<NEW_STATUS>' WHERE EQUIPMENT_ID = '" + cml.getEquipmentId() + "' AND CML_NO = " + cml.getCmlNo();
确实是导致错误的行,那么导致出现这样的堆栈跟踪的唯一方法是 if cml
是 null
.
解决此问题的一种方法是根据索引从表中获取项目:
cml = (Cml) cell.getTableView().getItems().get(getIndex());
关于JavaFX 自定义 CellFactory 包装器抛出空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40776652/
正在尝试创建一个 python 包。似乎有效,但我收到警告。我的 setup.py 是: #! /usr/bin/env python from distutils.core import setup
我导入了一个数据类型 X ,定义为 data X a = X a 在本地,我定义了一个通用量化的数据类型,Y type Y = forall a. X a 现在我需要定义两个函数, toY 和 fro
我似乎无法让编译器让我包装 Tokio AsyncRead: use std::io::Result; use core::pin::Pin; use core::task::{Context, Po
我有两个函数“a”和“b”。当用户上传文件时,“b”被调用。 “b”重命名文件并返回新文件名。之后应该编辑该文件。像这样: def a(): edits file def b(): r
我使用 Entity Framework 作为我的 ORM,我的每个类都实现了一个接口(interface),该接口(interface)基本上表示表结构(每个字段一个只读属性)。这些接口(inter
有没有办法打开一个程序,通常会打开一个新的jframe,进入一个现有的jframe? 这里是解释,我下载了一个java游戏,其中一个是反射游戏,它在一个jframe中打开,框架内有一堆子面板,我想要做
我想要下面的布局 | AA BBBBBBB | 除非没有足够的空间,在这种情况下 | AA | | BBBBBBB | 在这种情况下,A 是复选框,B 是复选框旁边的 Text
我正在尝试以不同的方式包装我的网站,以便将背景分为 2 部分。灰色部分是主要背景,还有白色部分,它较小并包装主要内容。 基本上我想要this看起来像this . 我不太确定如何添加图像来创建阴影效果,
我正在使用 : 读取整数文件 int len = (int)(new File(file).length()); FileInputStream fis = new FileInputStream(f
我使用 maven 和 OpenJDK 1.8 打包了一个 JavaFX 应用程序我的 pom.xml 中的相关部分: maven-assembly-plugin
我正在使用两个不同的 ItemsControl 来生成一个按钮列表。
我有一个情况,有一个变量会很方便,to , 可以是 TimerOutput或 nothing .我有兴趣提供一个采用与 @timeit 相同参数的宏来自 TimerOutputs(例如 @timeit
我正在尝试包装一个名为 content 的 div与另一个具有不同背景的 div。 但是,当将“margin-top”与 content 一起使用时div,似乎包装 DIV 获得了边距顶部而不是 co
文档不清楚,它似乎允许包装 dll 和 csproj 以在 Asp.Net Core 5 应用程序中使用。它是否允许您在 .Net Core 5 网站中使用针对 .Net Framework 4.6
我被要求开发一个层,该层将充当通用总线,而不直接引用 NServiceBus。到目前为止,由于支持不引人注目的消息,这并不太难。除了现在,我被要求为 IHandleMessages 提供我们自己的定义
我正在尝试包装 getServersideProps使用身份验证处理程序函数,但不断收到此错误:TypeError: getServerSideProps is not a function我的包装看
我有一个项目,它在特定位置(不是/src/resources)包含资源(模板文件)。我希望在运行 package-bin 时将这些资源打包。 我看到了 package-options 和 packag
我正在寻找打印从一系列对象中绘制的 div。我可以通过使用下面的管道语法来实现这一点。 each i, key in faq if (key == 0) |
我在 Meteor.js“main.js - Server”中有这个方法。 Meteor.methods({ messageSent: function (message) { var a
我注意到,如果我的自定义Polymer 1.x元素的宽度比纸张输入元素上的验证错误消息的宽度窄,那么错误将超出自定义元素的右边界。参见下图: 有没有一种机制可以防止溢出,例如在到达自定义元素的边界时自
我是一名优秀的程序员,十分优秀!