我有一个TableView,其中包含始终显示可写文本字段的列。如果column1的“BigDecimal”值大于column2的值,我想让文本字段更改颜色。我可以在EditableTextCell类中设置文本字段的样式(例如,如果文本不是有效数字),但是似乎无法访问该模型进行其他比较。这是我的代码:
EditableTextCell.java
package tester;
import java.util.Objects;
import javafx.beans.value.ObservableValue;
import javafx.beans.value.WritableValue;
import javafx.geometry.Pos;
import javafx.scene.control.TableCell;
import javafx.scene.control.TextField;
public class EditableTextCell<E> extends TableCell<E, String>
{
private final TextField textField;
private boolean updating = false;
public EditableTextCell(boolean editable)
{
textField = new TextField();
textField.setAlignment(Pos.CENTER_RIGHT);
textField.setEditable(editable);
textField.textProperty().addListener((ObservableValue<? extends String> o, String oldValue, String newValue) ->
{
if (!updating)
{
((WritableValue<String>) getTableColumn().getCellObservableValue((E) getTableRow().getItem())).setValue(newValue);
getTableView().scrollTo(getTableRow().getIndex());
getTableView().scrollToColumn(getTableColumn());
}
// this is where I would like stylize the textfield based on the input
});
}
@Override
protected void updateItem(String item, boolean empty)
{
super.updateItem(item, empty);
if (empty)
{
setGraphic(null);
} else
{
setGraphic(textField);
if (!Objects.equals(textField.getText(), item))
{
// prevent own updates from moving the cursor
updating = true;
textField.setText(item);
updating = false;
}
}
}
}
LineItem.java
package tester;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class LineItem
{
private final StringProperty string1;
private final StringProperty string2;
public LineItem()
{
this.string1 = new SimpleStringProperty();
this.string2 = new SimpleStringProperty();
}
public final StringProperty getString1Property()
{
return this.string1;
}
public final StringProperty getString2Property()
{
return this.string2;
}
}
测试器
package tester;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableRow;
import javafx.scene.control.TableView;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Callback;
public class Tester extends Application
{
@Override
public void start(Stage primaryStage)
{
TableView<LineItem> table = new TableView<>();
table.setRowFactory(p ->
{
final TableRow<LineItem> row = new TableRow<>();
row.setOnMouseClicked(event ->
{
if (event.getClickCount() == 2 && (!row.isEmpty()))
{
LineItem rowData = row.getItem();
System.out.println(rowData.getString1Property().get() + " "+rowData.getString2Property().get());
}
});
return row;
});
Callback<TableColumn<LineItem, String>, TableCell<LineItem, String>> textFactoryEditable = (TableColumn<LineItem, String> p) -> new EditableTextCell(true);
TableColumn<LineItem, String> column1 = new TableColumn<>("Test1");
column1.setCellValueFactory(cellData -> cellData.getValue().getString1Property());
column1.setEditable(true);
column1.setCellFactory(textFactoryEditable);
table.getColumns().add(column1);
TableColumn<LineItem, String> column2 = new TableColumn<>("Test2");
column2.setCellValueFactory(cellData -> cellData.getValue().getString2Property());
column2.setEditable(true);
column2.setCellFactory(textFactoryEditable);
table.getColumns().add(column2);
table.getItems().add(new LineItem());
HBox root = new HBox();
root.getChildren().addAll(table);
Scene scene = new Scene(root, 500, 500);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
最佳答案
我想到了。这是可以访问模型类的行。LineItem lineItem = (LineItem) getTableRow().getItem();
但是,我的问题是我也在更改EditableTextCell上的类声明以匹配类型:public class EditableTextCell<E> extends TableCell<E, String>
至:public class EditableTextCell<LineItem> extends TableCell<LineItem, String>
这使我无法使用getTableRow().getItem()
下的属性
出现此错误:
cannot find symbol
symbol: method getString1Property()
location: variable lineItem of type LineItem
where LineItem is a type-variable:
LineItem extends Object declared in class EditableTextCell`