用javafx中相同位置的文本字段替换标签

时间:2017-01-14 08:13:44

标签: javafx scenebuilder

我的VBox中有许多标签,点击按钮我想用文本字段替换所有这些标签。

2 个答案:

答案 0 :(得分:3)

我不建议使用VBox,因为更换孩子可能会修改布局,从而导致影响用户的效果。
相反,我建议使用GridPane,它允许您将多个孩子放在一个单元格中。这样您就可以将所有LabelVBox es放在网格中,但将visible的{​​{1}}属性设置为TextField。这意味着falseLabel都将用于布局计算,您只需在"编辑模式"之间切换。和#34;正常模式"通过反转所有孩子的TextField属性:

<强> FXML

visible
<HBox xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxml.LabelReplaceController">
    <children>
        <GridPane fx:id="grid">
            <children>
                <TextField fx:id="t1" text="Hello World!" visible="false" GridPane.rowIndex="0"/>
                <Label text="${t1.text}" GridPane.rowIndex="0" />
                <TextField fx:id="t2" text="foo" visible="false" GridPane.rowIndex="1"/>
                <Label text="${t2.text}" GridPane.rowIndex="1" />
                <TextField fx:id="t3" text="bar" visible="false" GridPane.rowIndex="2"/>
                <Label text="${t3.text}" GridPane.rowIndex="2" />
            </children>
        </GridPane>
        <ToggleButton selected="false" onAction="#selectionChanged" text="edit"/>
    </children>
</HBox>

答案 1 :(得分:0)

您可以迭代VBox vBox = ... int currentPos = 0; Map<Integer, TextField> toInsert = new HashMap<>(); // map with TextFields that need to be inserted at position for (Iterator<Node> iterator = vBox.getChildren().iterator(); iterator.hasNext(); ) { Node child = iterator.next(); if (child instanceof Label) { Label lbl = (Label)child; TextField text = new TextField(lbl.getText()); iterator.remove(); // remove the label that is at index currentPos toInsert.put(currentPos, text); } currentPos++; } for (Integer pos : toInsert.keySet()) { TextField field = toInsert.get(pos); vBox.getChildren().add(pos, field); // Add the Text field at the old position of the Label } 并替换:

TextField

在第一个循环中,删除标签并创建替换TextField并记住稍后需要在哪个位置插入标签。这不可能在同一个循环中发生。然后在第二个循环中,将VBox添加到您指定位置的VBox

修改 基于@ fabian的有效评论,上述建议的解决方案可能会导致问题。这是另一种方法,可以创建第二个Label,其中复制所有非Label个节点,并将TextField替换为VBox。我假设原始Parent是某些Parent parent = ... // Parent of VBox VBox vbox = ... // Find the index of vBox in Parent int position = 0; for (int i = 0; i<parent.getChildren().size(); i++) { if (parent.getChildren().equals(vBox)) { position = i; break; // found our position, no need to look further } } VBox copyVBox = new VBox(); for (Node child : vBox.getChildren()) { if (child instanceof Label) { Label lbl = (Label)child; TextField text = new TextField(lbl.getText()); copyVBox.getChildren().add(text); } else { copyVBox.getChildren().add(child); } } parent.getChildren().remove(vBox); parent.getChildren().add(position, copyVBox); 的子元素。

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/htab_maincontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/htab_appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/htab_collapse_toolbar"
        android:layout_width="match_parent"
        android:layout_height="256dp"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/htab_header"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/header"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/htab_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:gravity="top"
            android:minHeight="?attr/actionBarSize"
            app:layout_collapseMode="parallax"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:titleMarginTop="13dp" />

        <android.support.design.widget.TabLayout
            android:id="@+id/htab_tabs"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:layout_marginTop="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            android:theme="@style/MyCustomTabLayout"
            app:tabIndicatorColor="@android:color/white" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/main_title"
            android:layout_gravity="bottom"
            android:layout_marginBottom="10dp"
            android:layout_marginStart="20dp"
            android:gravity="start"
            android:textStyle="bold"
            android:textSize="20dp"
            android:textColor="@color/colorAccent"
            app:layout_collapseMode="parallax"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
            android:maxLines="1"
            android:text="test articles"
            android:layout_marginLeft="20dp" />
    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/htab_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>