ListSelectionView 不显示 ObservableList 的内容
ListSelectionView not displaying contents of ObservableList
我在 JavaFX 中有一个 ListSelectionView(来自 ControlsFX 库),我需要它在单击特定按钮时显示一组值。当我在初始化方法中将内容添加到其中时,它会显示值。但是当我在按钮的事件操作方法中放置相同的代码时,它不会显示。我有另一种方法可以从 ListSelectionView 获取源项目和目标项目并显示它们。尽管 ListSelectionView 没有显示任何内容,但 getSourceItems() 方法 returns 显示了一组值。
public class FXMLDocumentController implements Initializable {
@FXML
private Label label;
@FXML
private Button button;
@FXML
private JFXListView<Label> listv;
@FXML
private ListSelectionView<Label> abc;
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
@FXML
private void handleToggle(MouseEvent event) {
ObservableList<Label> users = FXCollections.observableArrayList();
abc.setSourceHeader(new Label("Present"));
abc.setTargetHeader(new Label("Absent"));
for (int i=0; i<4;i++)
{
users.add(new Label("Name "+i));
}
abc.setSourceItems(users);
abc.setDisable(false);
for (int i=0; i<4;i++)
{
Label k = new Label("Hello"+i);
listv.getItems().add(k);
}
}
@FXML
private void handlenewNOde(MouseEvent event) {
ObservableList<Label> users1 =abc.getSourceItems();
System.out.println("Source items");
for(int i =0; i<users1.size(); i++)
{
Label k=users1.get(i);
System.out.println(k.getText());
}
System.out.println("Target items");
ObservableList<Label> users12 =abc.getTargetItems();
for(int i =0; i<users12.size(); i++)
{
Label k=users12.get(i);
System.out.println(k.getText());
}
}
}
.fxml :
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXListView?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import org.controlsfx.control.ListSelectionView?>
<AnchorPane id="AnchorPane" prefHeight="680.0" prefWidth="914.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.141" fx:controller="listselectionviewtryout.FXMLDocumentController">
<children>
<Button fx:id="button" layoutX="14.0" layoutY="21.0" onAction="#handleButtonAction" text="Click Me!" />
<Label fx:id="label" layoutX="17.0" layoutY="54.0" minHeight="16" minWidth="69" />
<JFXListView fx:id="listv" layoutX="131.0" layoutY="35.0" prefHeight="237.0" prefWidth="75.0" />
<JFXButton layoutX="413.0" layoutY="58.0" onMouseClicked="#handleToggle" text="Toggle Extension" />
<JFXButton layoutX="445.0" layoutY="134.0" onMouseClicked="#handlenewNOde" text="new Node" />
<ListSelectionView fx:id="abc" layoutX="226.0" layoutY="262.0" />
</children>
</AnchorPane>
忽略 JFXListView。
输出:
Source items
Name 0
Name 1
Name 2
Name 3
Target items
应该
abc.setSourceItems(users);
成为
abc.getSourceItems().addAll(users);
我在 JavaFX 中有一个 ListSelectionView(来自 ControlsFX 库),我需要它在单击特定按钮时显示一组值。当我在初始化方法中将内容添加到其中时,它会显示值。但是当我在按钮的事件操作方法中放置相同的代码时,它不会显示。我有另一种方法可以从 ListSelectionView 获取源项目和目标项目并显示它们。尽管 ListSelectionView 没有显示任何内容,但 getSourceItems() 方法 returns 显示了一组值。
public class FXMLDocumentController implements Initializable {
@FXML
private Label label;
@FXML
private Button button;
@FXML
private JFXListView<Label> listv;
@FXML
private ListSelectionView<Label> abc;
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
}
@FXML
private void handleToggle(MouseEvent event) {
ObservableList<Label> users = FXCollections.observableArrayList();
abc.setSourceHeader(new Label("Present"));
abc.setTargetHeader(new Label("Absent"));
for (int i=0; i<4;i++)
{
users.add(new Label("Name "+i));
}
abc.setSourceItems(users);
abc.setDisable(false);
for (int i=0; i<4;i++)
{
Label k = new Label("Hello"+i);
listv.getItems().add(k);
}
}
@FXML
private void handlenewNOde(MouseEvent event) {
ObservableList<Label> users1 =abc.getSourceItems();
System.out.println("Source items");
for(int i =0; i<users1.size(); i++)
{
Label k=users1.get(i);
System.out.println(k.getText());
}
System.out.println("Target items");
ObservableList<Label> users12 =abc.getTargetItems();
for(int i =0; i<users12.size(); i++)
{
Label k=users12.get(i);
System.out.println(k.getText());
}
}
}
.fxml :
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXListView?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import org.controlsfx.control.ListSelectionView?>
<AnchorPane id="AnchorPane" prefHeight="680.0" prefWidth="914.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.141" fx:controller="listselectionviewtryout.FXMLDocumentController">
<children>
<Button fx:id="button" layoutX="14.0" layoutY="21.0" onAction="#handleButtonAction" text="Click Me!" />
<Label fx:id="label" layoutX="17.0" layoutY="54.0" minHeight="16" minWidth="69" />
<JFXListView fx:id="listv" layoutX="131.0" layoutY="35.0" prefHeight="237.0" prefWidth="75.0" />
<JFXButton layoutX="413.0" layoutY="58.0" onMouseClicked="#handleToggle" text="Toggle Extension" />
<JFXButton layoutX="445.0" layoutY="134.0" onMouseClicked="#handlenewNOde" text="new Node" />
<ListSelectionView fx:id="abc" layoutX="226.0" layoutY="262.0" />
</children>
</AnchorPane>
忽略 JFXListView。 输出:
Source items
Name 0
Name 1
Name 2
Name 3
Target items
应该
abc.setSourceItems(users);
成为
abc.getSourceItems().addAll(users);