正如我在您的previous question中所建议的那样,只需在创建控件的位置注册控件的侦听器即可。
实际上,对于两个文本字段和复选框,实际上可以只使用一个侦听器(在每行中),并在任何更改时更新第三个文本框。例如:
public static GridPane table(int rows){
GridPane table = new GridPane();
for(int i=0; i<rows; i++){
TextField textField = new TextField();
textField.setAlignment(Pos.CENTER);
TextField textField2 = new TextField();
textField2.setAlignment(Pos.CENTER);
CheckBox checkBox = new CheckBox("Check Box");
checkBox.setTextFill(Color.WHITE);
checkBox.setAlignment(Pos.CENTER);
TextField textField3 = new TextField();
textField3.setAlignment(Pos.CENTER);
table.add(textField, 0, i);
table.add(textField2, 1, i);
table.add(checkBox , 2, i);
table.add(textField3,3, i);
ChangeListener<Object> listener = (obs, oldValue, newValue) ->
updateTotalField(textField.getText(), textField2.getText(), checkBox.isSelected(), textField3);
textField.textProperty().addListener(listener);
textField2.textProperty().addListener(listener);
checkBox.selectedProperty().addListener(listener);
GridPane.setMargin(textField, new Insets(5));
GridPane.setMargin(textField2, new Insets(5));
GridPane.setMargin(checkBox, new Insets(5));
GridPane.setMargin(textField3, new Insets(5));
}
table.setAlignment(Pos.CENTER);
return table;
}
private static void updateTotalField(String text1, String text2, boolean addPause, TextField output) {
int value1 = parseText(text1);
int value2 = parseText(text2);
int total = value1 + value2 ;
if (addPause) {
total += 10 ;
}
output.setText(Integer.toString(total));
}
private static int parseText(String text) {
// if text is a valid integer:
if (text.matches("\\d+")) {
return Integer.parseInt(text);
} else {
return 0 ;
}
}
现在你可以摆脱add
和addPause
方法和代码块你张贴在您遍历网格窗格的子节点,并添加监听器。除非您在其他地方需要,否则您可能无法使用getComponent()
方法。
下面是代码为SSCCE
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class TableOfControlsInGridPane extends Application {
@Override
public void start(Stage primaryStage) {
Scene scene = new Scene(table(10));
primaryStage.setScene(scene);
primaryStage.show();
}
public GridPane table(int rows){
GridPane table = new GridPane();
for(int i=0; i<rows; i++){
TextField textField = new TextField();
textField.setAlignment(Pos.CENTER);
TextField textField2 = new TextField();
textField2.setAlignment(Pos.CENTER);
CheckBox checkBox = new CheckBox("Check Box");
checkBox.setTextFill(Color.WHITE);
checkBox.setAlignment(Pos.CENTER);
TextField textField3 = new TextField();
textField3.setAlignment(Pos.CENTER);
table.add(textField, 0, i);
table.add(textField2, 1, i);
table.add(checkBox , 2, i);
table.add(textField3,3, i);
ChangeListener<Object> listener = (obs, oldValue, newValue) ->
updateTotalField(textField.getText(), textField2.getText(), checkBox.isSelected(), textField3);
textField.textProperty().addListener(listener);
textField2.textProperty().addListener(listener);
checkBox.selectedProperty().addListener(listener);
GridPane.setMargin(textField, new Insets(5));
GridPane.setMargin(textField2, new Insets(5));
GridPane.setMargin(checkBox, new Insets(5));
GridPane.setMargin(textField3, new Insets(5));
}
table.setAlignment(Pos.CENTER);
return table;
}
private void updateTotalField(String text1, String text2, boolean addPause, TextField output) {
int value1 = parseText(text1);
int value2 = parseText(text2);
int total = value1 + value2 ;
if (addPause) {
total += 10 ;
}
output.setText(Integer.toString(total));
}
private int parseText(String text) {
// if text is a valid integer:
if (text.matches("\\d+")) {
return Integer.parseInt(text);
} else {
return 0 ;
}
}
public static void main(String[] args) {
launch(args);
}
}
和截图:
由于Java是一种面向对象的语言,它很可能似乎更适合做这一种对象 - 首先是面向导向的方式。我不知道什么样的数据,你在这个观点代表,但你应该定义封装在一个行的数据一类:
import javafx.beans.binding.Bindings;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ReadOnlyIntegerProperty;
import javafx.beans.property.ReadOnlyIntegerWrapper;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
// Obviously use a more sensible name for the class:
public class RowData {
private final IntegerProperty firstValue = new SimpleIntegerProperty();
private final IntegerProperty secondValue = new SimpleIntegerProperty();
private final BooleanProperty includePause = new SimpleBooleanProperty();
private final ReadOnlyIntegerWrapper total = new ReadOnlyIntegerWrapper();
public RowData() {
total.bind(Bindings.createIntegerBinding(() -> {
int total = getFirstValue() + getSecondValue() ;
if (isIncludePause()) {
total += 10 ;
}
return total ;
}, firstValue, secondValue, includePause));
}
public final IntegerProperty firstValueProperty() {
return this.firstValue;
}
public final int getFirstValue() {
return this.firstValueProperty().get();
}
public final void setFirstValue(final int firstValue) {
this.firstValueProperty().set(firstValue);
}
public final IntegerProperty secondValueProperty() {
return this.secondValue;
}
public final int getSecondValue() {
return this.secondValueProperty().get();
}
public final void setSecondValue(final int secondValue) {
this.secondValueProperty().set(secondValue);
}
public final BooleanProperty includePauseProperty() {
return this.includePause;
}
public final boolean isIncludePause() {
return this.includePauseProperty().get();
}
public final void setIncludePause(final boolean includePause) {
this.includePauseProperty().set(includePause);
}
public final ReadOnlyIntegerProperty totalProperty() {
return this.total.getReadOnlyProperty();
}
public final int getTotal() {
return this.totalProperty().get();
}
}
然后一个类来显示这些数据:
import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.control.CheckBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
public class RowDataView {
private final TextField firstValueField ;
private final TextField secondValueField ;
private final CheckBox includePauseBox ;
private final TextField totalField ;
public RowDataView(RowData data) {
firstValueField = new TextField();
bindToStringProperty(data.firstValueProperty(), firstValueField.textProperty());
secondValueField = new TextField();
bindToStringProperty(data.secondValueProperty(), secondValueField.textProperty());
includePauseBox = new CheckBox();
data.includePauseProperty().bind(includePauseBox.selectedProperty());
totalField = new TextField();
totalField.setEditable(false);
totalField.textProperty().bind(data.totalProperty().asString());
}
public void addToGridPane(GridPane pane, int row, int firstValueColumn, int secondValueColumn, int checkboxColumn, int totalColumn) {
pane.add(firstValueField, firstValueColumn, row);
pane.add(secondValueField, secondValueColumn, row);
pane.add(includePauseBox, checkboxColumn, row);
pane.add(totalField, totalColumn, row);
}
public void addToGridPane(GridPane pane, int row, int column) {
addToGridPane(pane, row, column, column+1, column+2, column+3);
}
public void addToGridPane(GridPane pane, int row) {
addToGridPane(pane, row, 0);
}
private void bindToStringProperty(IntegerProperty p, StringProperty s) {
p.bind(Bindings.createIntegerBinding(
() -> {
if (s.get().matches("\\d+")) {
return Integer.parseInt(s.get());
}
return 0 ;
}, s));
}
}
这里是一些测试代码:
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class TableOfControlsInGridPane extends Application {
private final List<RowData> data = new ArrayList<>();
@Override
public void start(Stage primaryStage) {
Scene scene = new Scene(table(10), 800, 800);
scene.getStylesheets().add("style.css");
primaryStage.setScene(scene);
primaryStage.show();
}
public GridPane table(int rows){
GridPane table = new GridPane();
table.getStyleClass().add("data-grid");
data.clear();
for(int i=0; i<rows; i++){
RowData rowData = new RowData();
data.add(rowData);
RowDataView rowDataView = new RowDataView(rowData);
rowDataView.addToGridPane(table, i);
}
table.setAlignment(Pos.CENTER);
return table;
}
public static void main(String[] args) {
launch(args);
}
}
我感动的样式样式表(style.css
):
为实现这一
.data-grid {
-fx-background-color: purple ;
-fx-hgap: 10 ;
-fx-vgap: 10 ;
}
.data-grid .text-field, .data-grid .check-box {
-fx-alignment: center ;
}
.data-grid .check-box {
-fx-text-fill: white ;
}
你为什么不只是监听器添加到复选框,当你创建了吗?我给了你[上一个问题]一个完美可行的解决方案(https://stackoverflow.com/questions/44312354/javafx-how-to-compare-values-of-dynamically-created-textfields-inside-gridpane):完全相同的方法也可以在这里工作。 –
@Jamed_D你的例子很好,但我需要重构整组代码。 – Junaid
为什么你需要重构它,这与这个问题(或你的前一个问题)有什么关系? –