ListSelectionView不显示ObservableList

时间:2017-11-06 13:35:12

标签: javafx controlsfx

我在JavaFX中有一个ListSelectionView(来自ControlsFX库),我需要它在单击特定按钮时显示一组值。它在初始化方法中将内容添加到其中时显示值。但是当我将相同的代码放在按钮的事件操作方法中时,它不会显示。我有另一种方法从ListSelectionView获取Source项和Target项并显示它们。即使ListSelectionView没有显示任何内容,getSourceItems()方法也会返回一组值,然后显示这些值。

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

enter image description here

1 个答案:

答案 0 :(得分:0)

应该

 abc.setSourceItems(users);

abc.getSourceItems().addAll(users);