- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
基本上问题标题说明了一切。我在表格 View 中有一列字符串(称为 type
)和相应的一列数字(称为 size
),每行代表一个对象 CargoItem
>,它有两个属性type
和size
。两列都是可编辑的。 TableView 与相应的 CargoItem
可观察列表关联,称为 cargoList
。
ChoiceBox 最初设置为可观察列表。我有一个附加到 ChoiceBox 的监听器,因此它可以使用相应的大小属性更新 TextField。这可以正常工作:如果您在大小列中的单元格中键入新值,则 TextField 中的相应值将更新(选择 ChoiceBox 项后)。
目前,如果我编辑 type
列中的单元格,ChoiceBox 中的项目将不会更新。我有一个诊断按钮,它将验证可观察列表是否正确更新。我想我需要将监听器放入类型列的单元格中,但不确定如何操作。
以下是一个最小的工作示例,有 4 个文件:Table_test02.fxml、CargoItem.java、ControllerTest.java 和 TestApp.java
Table_test02.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<AnchorPane fx:id="root" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="252.0" prefWidth="232.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="so_question01.ControllerTest">
<children>
<TableView fx:id="cargoTable" editable="true" layoutX="9.0" layoutY="30.0" prefHeight="107.0" prefWidth="213.0">
<columnResizePolicy>
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
</columnResizePolicy>
<columns>
<TableColumn fx:id="cargoTableTypeCol" maxWidth="130.0" minWidth="60.0" prefWidth="75.0" sortable="false" text="Type" />
<TableColumn fx:id="cargoTableSizeCol" maxWidth="118.0" minWidth="60.0" prefWidth="67.0" sortable="false" text="Size" />
</columns>
</TableView>
<Label layoutX="10.0" layoutY="11.0" text="Cargo">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Label>
<ChoiceBox fx:id="externalChoiceBox" layoutX="9.0" layoutY="162.0" prefHeight="25.0" prefWidth="99.0" />
<TextField fx:id="externalTextField" layoutX="123.0" layoutY="162.0" prefHeight="25.0" prefWidth="99.0" />
<Button fx:id="diagnosticButton" layoutX="83.0" layoutY="210.0" mnemonicParsing="false" text="Diagnostic" textFill="#862828">
<font>
<Font name="Arial Black" size="11.0" />
</font>
</Button>
<Label layoutX="10.0" layoutY="143.0" text="Type">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Label>
<Label layoutX="123.0" layoutY="144.0" text="Size">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Label>
</children>
</AnchorPane>
CargoItem.java:
package so_question01;
import javafx.beans.property.*;
public class CargoItem {
private final StringProperty type;
private final FloatProperty size;
public CargoItem() {
this.type = new SimpleStringProperty("");
this.size = new SimpleFloatProperty(0f);
}
public CargoItem(String type, float size) {
this.type = new SimpleStringProperty(type);
this.size = new SimpleFloatProperty(size);
}
public void setType(String type) {
this.type.set(type);
}
public void setSize(float size) {
this.size.set(size);
}
public String getType() {
if (!(type.get().equals("") || type.get() == null )) {
return type.get();
}
return "";
}
public float getSize() {
return size.get();
}
public StringProperty typeProperty() {
return type;
}
public FloatProperty sizeProperty() {
return size;
}
public void clear() {
this.type.set("");
this.size.set(0);
}
@Override
public String toString() {
return type.get();
}
public String display() {
return "\nCARGO ITEM:"
+ "\n\ttype: " + type.get()
+ "\n\tsize: " + size.get();
}
}
ControllerTest.java:
package so_question01;
import java.text.DecimalFormat;
import javafx.collections.ListChangeListener.*;
import javafx.collections.ObservableList;
import javafx.collections.FXCollections;
import javafx.collections.*;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.beans.value.ObservableValue;
import javafx.util.StringConverter;
import javafx.beans.value.*;
public class ControllerTest {
ObservableList<CargoItem> cargoList = FXCollections.observableArrayList();
private TestApp mainApp; // Reference to the main application.
@FXML private ChoiceBox externalChoiceBox;
@FXML private TextField externalTextField;
@FXML private Button diagnosticButton;
@FXML private TableView<CargoItem> cargoTable;
@FXML private TableColumn<CargoItem, Float> cargoTableSizeCol;
@FXML private TableColumn<CargoItem, String> cargoTableTypeCol;
public ControllerTest() { }
@FXML
private void initialize() {
//*****DIAGNOSTIC ONLY*******************//
diagnosticButton.setOnAction(e->{
for(CargoItem c: cargoList){
System.out.print(c.display());
}
});
//does not work when changes made via table editing, but may be useful when new list loaded from file?
cargoList.addListener(new ListChangeListener< CargoItem>(){
public void onChanged(Change<? extends CargoItem> c){
// Do your changes here
System.out.println(c.getList());
}});
//**** test choicebox***//
StringConverter cargoItemConverter= new CargoItemStringConverter();
externalChoiceBox.setConverter(cargoItemConverter);
//***************** CARGO TABLE **************//
cargoTable.setEditable(true);
// Initialize the cargo table with the 2 columns.
cargoTableTypeCol.setCellFactory(TextFieldTableCell.forTableColumn());
cargoTableTypeCol.setCellValueFactory(new PropertyValueFactory<>("type"));
cargoTableSizeCol.setCellFactory(TextFieldTableCell.forTableColumn(new FloatIntegerStringConverter()));
cargoTableSizeCol.setCellValueFactory(new PropertyValueFactory<>("size"));
}
/**
* Is called by the main application to give a reference back to itself.
*
* @param mainApp
*/
public void setMainApp(TestApp mainApp) {
this.mainApp = mainApp;
cargoList.setAll(mainApp.getCargoData());
// Add observable list data to the table
cargoTable.setItems(cargoList);
cargoTable.getSelectionModel().setCellSelectionEnabled(true);
externalChoiceBox.setItems(cargoList);
//adds listener to choicebox selection, so size is rendered to text field
externalChoiceBox.getSelectionModel().selectedItemProperty().addListener(
new ChangeListener<CargoItem>() {
@Override public void changed(ObservableValue<? extends CargoItem> observableValue, CargoItem oldChoice, CargoItem newChoice) {
externalTextField.setText(new FloatIntegerStringConverter().toString(newChoice.getSize()));
}
});
}
}
//*******string converter classes, nothing to see here************//
class CargoItemStringConverter extends StringConverter<CargoItem>{
@Override
public String toString(CargoItem c){
return c.toString();
}
@Override
public CargoItem fromString(String type){
return null;
}
}
class FloatIntegerStringConverter extends StringConverter<Float>
{
DecimalFormat decimalFormat = new DecimalFormat("##,###,##0");
private Float returnVal;
@Override
public Float fromString(String value) {
if (value == null) {
return null;
}
value = value.trim();
String digits = value.replaceAll("[^0-9.]", ""); //gets rid of non-numerics
if (digits.length() < 1) {
return null;
}
try{ //avoids exception by converting invalid strings to zero (which should just be redundant due to regexp above)
returnVal= Float.valueOf(Math.round(Float.valueOf(digits)));//have to do this so the float value coresponds to the rounded value in table
} catch(NumberFormatException e) { returnVal= Float.valueOf(0); }
return returnVal;
}
@Override
public String toString(Float value) {
if (value == null) { // If the specified value is null, return a zero-length String
return "";
}
return decimalFormat.format(value)+" MT"; //truncated integer representation, unit metric tonnes
}
}
TestApp.java:
package so_question01;
import java.io.*;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.*;
import javafx.collections.ObservableList;
import javafx.collections.FXCollections;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class TestApp extends Application {
private Stage primaryStage;
private ObservableList<CargoItem> cargoData = FXCollections.observableArrayList();
public TestApp() {
// create some sample data
CargoItem cargoInstance0 = new CargoItem("Toxic waste", 5000f);
CargoItem cargoInstance1 = new CargoItem("Cement", 10000f);
CargoItem cargoInstance2 = new CargoItem("Wheat", 20000f);
cargoData.add(cargoInstance0);
cargoData.add(cargoInstance1);
cargoData.add(cargoInstance2);
}
/**
* Returns the data as observable lists of objects.
* @return
*/
public ObservableList<CargoItem> getCargoData() {return cargoData; } //called in the controller
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
this.primaryStage.setTitle("**** TEST ****");
show();
}
/**
* Initializes the root layout.
*/
public void show() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(TestApp.class.getResource("Table_test02.fxml"));
AnchorPane root = (AnchorPane) loader.load();
// Give the controller access to the main app.
ControllerTest controller = loader.getController();
controller.setMainApp(this);
// Show the scene containing the root layout.
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) { System.out.println(e);}
}
/**
* Returns the main stage.
* @return
*/
public Stage getPrimaryStage() {
return primaryStage;
}
public static void main(String[] args) {
Application.launch(TestApp.class, (java.lang.String[])null);
}
}
最佳答案
假设我正确理解了这个问题,您需要初始化您的 cargoList
与 extractor
:
ObservableList<CargoItem> cargoList = FXCollections.observableArrayList(cargo ->
new Observable[]{cargo.typeProperty(), cargo.sizeProperty()});
ChoiceBox
历史上无法很好地处理其基础列表的更新通知;我不确定最新版本是否已修复所有问题。您可能需要考虑切换到 ComboBox
反而。
更新
如果您需要使用ChoiceBox
,解决方法可能是不将项目直接设置为 cargoList
,但要在 cargoList
时更新项目变化。您可以通过在 setMainApp(...)
中进行以下更改来实现此目的:
// externalChoiceBox.setItems(cargoList);
externalChoiceBox.getItems().setAll(cargoList);
并在列表更改监听器中添加以下内容:
cargoList.addListener(new ListChangeListener< CargoItem>(){
public void onChanged(Change<? extends CargoItem> c){
// Do your changes here
System.out.println(c.getList());
externalChoiceBox.getItems().setAll(cargoList);
}});
关于java - 从 TableView 中的可编辑列动态填充 ChoiceBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34561453/
我想做的是让 JTextPane 在 JPanel 中占用尽可能多的空间。对于我使用的 UpdateInfoPanel: public class UpdateInfoPanel extends JP
我在 JPanel 中有一个 JTextArea,我想将其与 JScrollPane 一起使用。我正在使用 GridBagLayout。当我运行它时,框架似乎为 JScrollPane 腾出了空间,但
我想在 xcode 中实现以下功能。 我有一个 View Controller 。在这个 UIViewController 中,我有一个 UITabBar。它们下面是一个 UIView。将 UITab
有谁知道Firebird 2.5有没有类似于SQL中“STUFF”函数的功能? 我有一个包含父用户记录的表,另一个表包含与父相关的子用户记录。我希望能够提取用户拥有的“ROLES”的逗号分隔字符串,而
我想使用 JSON 作为 mirth channel 的输入和输出,例如详细信息保存在数据库中或创建 HL7 消息。 简而言之,输入为 JSON 解析它并输出为任何格式。 最佳答案 var objec
通常我会使用 R 并执行 merge.by,但这个文件似乎太大了,部门中的任何一台计算机都无法处理它! (任何从事遗传学工作的人的附加信息)本质上,插补似乎删除了 snp ID 的 rs 数字,我只剩
我有一个以前可能被问过的问题,但我很难找到正确的描述。我希望有人能帮助我。 在下面的代码中,我设置了varprice,我想添加javascript变量accu_id以通过rails在我的数据库中查找记
我有一个简单的 SVG 文件,在 Firefox 中可以正常查看 - 它的一些包装文本使用 foreignObject 包含一些 HTML - 文本包装在 div 中:
所以我正在为学校编写一个 Ruby 程序,如果某个值是 1 或 3,则将 bool 值更改为 true,如果是 0 或 2,则更改为 false。由于我有 Java 背景,所以我认为这段代码应该有效:
我做了什么: 我在这些账户之间创建了 VPC 对等连接 互联网网关也连接到每个 VPC 还配置了路由表(以允许来自双方的流量) 情况1: 当这两个 VPC 在同一个账户中时,我成功测试了从另一个 La
我有一个名为 contacts 的表: user_id contact_id 10294 10295 10294 10293 10293 10294 102
我正在使用 Magento 中的新模板。为避免重复代码,我想为每个产品预览使用相同的子模板。 特别是我做了这样一个展示: $products = Mage::getModel('catalog/pro
“for”是否总是检查协议(protocol)中定义的每个函数中第一个参数的类型? 编辑(改写): 当协议(protocol)方法只有一个参数时,根据该单个参数的类型(直接或任意)找到实现。当协议(p
我想从我的 PHP 代码中调用 JavaScript 函数。我通过使用以下方法实现了这一点: echo ' drawChart($id); '; 这工作正常,但我想从我的 PHP 代码中获取数据,我使
这个问题已经有答案了: Event binding on dynamically created elements? (23 个回答) 已关闭 5 年前。 我有一个动态表单,我想在其中附加一些其他 h
我正在尝试找到一种解决方案,以在 componentDidMount 中的映射项上使用 setState。 我正在使用 GraphQL连同 Gatsby返回许多 data 项目,但要求在特定的 pat
我在 ScrollView 中有一个 View 。只要用户按住该 View ,我想每 80 毫秒调用一次方法。这是我已经实现的: final Runnable vibrate = new Runnab
我用 jni 开发了一个 android 应用程序。我在 GetStringUTFChars 的 dvmDecodeIndirectRef 中得到了一个 dvmabort。我只中止了一次。 为什么会这
当我到达我的 Activity 时,我调用 FragmentPagerAdapter 来处理我的不同选项卡。在我的一个选项卡中,我想显示一个 RecyclerView,但他从未出现过,有了断点,我看到
当我按下 Activity 中的按钮时,会弹出一个 DialogFragment。在对话框 fragment 中,有一个看起来像普通 ListView 的 RecyclerView。 我想要的行为是当
我是一名优秀的程序员,十分优秀!