简体   繁体   中英

How to bind the resort child of vBox in javafx and jfxml

I have the folloing fxml code which includes grid pane and vbox which includes buttons with its ids

is it possible to bind vbox so that the order of the buttons are resorted based on the fx:id?

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>

<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="472.0" prefWidth="698.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/18">
  <columnConstraints>
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
  </columnConstraints>
  <rowConstraints>
    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
  </rowConstraints>
   <children>
      <VBox prefHeight="200.0" prefWidth="100.0" GridPane.columnIndex="1">
         <children>
            <Button fx:id="1" mnemonicParsing="false" prefHeight="72.0" prefWidth="350.0" text="1" />
            <Button fx:id="2" mnemonicParsing="false" prefHeight="72.0" prefWidth="350.0" text="2" />
            <Button fx:id="3" mnemonicParsing="false" prefHeight="72.0" prefWidth="350.0" text="3" />
            <Button fx:id="4" mnemonicParsing="false" prefHeight="72.0" prefWidth="350.0" text="4" />
            <Button fx:id="5" mnemonicParsing="false" prefHeight="72.0" prefWidth="350.0" text="5" />
            <Button fx:id="6" mnemonicParsing="false" prefHeight="72.0" prefWidth="350.0" text="6" />
            <Button fx:id="7" mnemonicParsing="false" prefHeight="72.0" prefWidth="350.0" text="7" />
         </children>
      </VBox>
   </children>
</GridPane>

The Controller the controller has

        @FXML
            public void initialize() {
List<Integer> ids = new ArrayList<>();        // resort the childs of vbox based on the list "ids"
    
    }

i want to bind the list of buttons to my "ids" variable which includes the new fx-id`s and if the order of list-elemens changes then the order of list of buttons inside vbox should also be changed

0 Better use TreeMap from SortedMap interface.

SortedMap<Integer, CheckBox> sm = TreeMap<Integer, CheckBox>();

sm.put(new Integer(1),new CheckBox("first checkbox"));
sm.put(new Integer(2),new CheckBox("second checkbox"));
sm.put(new Integer(3),new CheckBox("third checkbox"));
sm.put(new Integer(4),new CheckBox("fourth checkbox"));
sm.put(new Integer(5),new CheckBox("fifth checkbox"));
sm.put(new Integer(6),new CheckBox("sixth checkbox"));
sm.put(new Integer(7),new CheckBox("seventh checkbox"));
....

getting them after traversing in the order of the id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM