如何在JavaFX中隐藏/显示动态创建的节点?

huangapple go评论83阅读模式
英文:

how hide/show dynamically created nodes in javafx?

问题

我已动态创建了多个节点。现在,当单击特定节点时,必须查找该节点,它必须隐藏窗格中的所有其他节点。此外,当我再次单击它时,它应显示被隐藏的节点。

例如,在附加的图像中,单击根节点上的向下箭头图标,它必须隐藏所有其他节点,再次单击时,它必须显示隐藏的节点。
这是我创建按钮的方式。

  Node_Basic_Event_Node anode = new Node_Basic_Event_Node(calcNode_Id("Basic_Event_Node"), "Basic_Event_Node", editorName, editorType); anode.relocate(50, 50); elementsContent.getChildren().add(anode);

现在,我如何在单击图像中的根节点的向下箭头图标时隐藏节点?我尝试了这段代码示例,但它隐藏了包括 "root node" 在内的所有节点。

  Pane elementsContent = (Pane) getParent();
  for (Node node : elementsContent.getChildren()) {
        node.managedProperty().set(true);
        node.setVisible(false);
  }
英文:

I have created multiple nodes dynamically. Now, I have to find specific node when it is clicked, it must hide all other node in the pane. Also, When I click it again it should display the hidden node.

如何在JavaFX中隐藏/显示动态创建的节点?
For example,In the attached image, the down-arrow icon on root node is clicked, it must hide all other nodes and when it is clicked second time, it must show the hidden nodes again.
This how I am creating the buttons.

  Node_Basic_Event_Node anode = new Node_Basic_Event_Node(calcNode_Id("Basic_Event_Node"), "Basic_Event_Node", editorName, editorType); anode.relocate(50, 50); elementsContent.getChildren().add(anode); 

Now, How I can hide nodes when clicking on root node's down-arrow icon in the image? I tried this sample of code but it is hiding all nodes including "root node"

	Pane elementsContent = (Pane) getParent();
	for (Node node : elementsContent.getChildren()) {
			node.managedProperty().set(true);
			node.setVisible(false);
	}

答案1

得分: 1

我并不完全确定您的问题出在哪里但我创建了一个小示例希望能对您有所帮助不应更改可见性的按钮此处为特定文本在每次被过滤时都会被排除

package sample;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.Optional;
import java.util.Random;


public class Main extends Application {

    @Override
    public void start(Stage primaryStage) {

        VBox vBox = new VBox();

        Random random = new Random();
        int randomIndex = random.nextInt(10);

        String chosen = "The Chosen One", simple = "Simple Button";

        for (int i = 0; i < 10; i++) {

            Button button = new Button(simple);

            if (i == randomIndex)
                button.setText(chosen);

            vBox.getChildren().add(button);

            button.setOnAction(event -> {

                Button clickedButton = (Button) event.getSource();

                if (clickedButton.getText().equals(chosen)) {

                    // 保存普通按钮的可见性状态:
                    Optional<Boolean> visible = vBox.getChildren().stream()
                            .filter(node -> node instanceof Button)
                            .map(node -> (Button) node)
                            .filter(btn -> !btn.getText().equals(chosen))
                            .findFirst().map(Button::isVisible);

                    // 更改除被过滤的选定按钮外的其他按钮的可见性:
                    vBox.getChildren().stream()
                            .filter(node -> node instanceof Button)
                            .map(node -> (Button) node)
                            .filter(btn -> !btn.getText().equals(chosen))
                            .forEach(btn -> btn.setVisible(!btn.isVisible()));

                    // 更改(并不是那么)选中的按钮:
                    if (visible.isPresent() && !visible.get()) {
                        int nextIndex = random.nextInt(10);
                        for (int j = 0; j < vBox.getChildren().size(); j++) {
                            if (vBox.getChildren().get(j) instanceof Button) {
                                Button btn = (Button) vBox.getChildren().get(j);
                                if (j == nextIndex)
                                    btn.setText(chosen);
                                else
                                    btn.setText(simple);
                            }
                        }
                        vBox.requestFocus();
                    }
                }
            });
        }

        primaryStage.setScene(new Scene(vBox));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
英文:

I am not absolutly sure where your problem exactly is, but I created a small example which will hopefully help you. The button which should not change visibility (here: certain text) is simply filtered out every time.

package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.util.Optional;
import java.util.Random;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
VBox vBox = new VBox();
Random random = new Random();
int randomIndex = random.nextInt(10);
String chosen = &quot;The Chosen One&quot;, simple = &quot;Simple Button&quot;;
for (int i = 0; i &lt; 10; i++) {
Button button = new Button(simple);
if (i == randomIndex)
button.setText(chosen);
vBox.getChildren().add(button);
button.setOnAction(event -&gt; {
Button clickedButton = (Button) event.getSource();
if (clickedButton.getText().equals(chosen)) {
// Save visibility status of the simple buttons:
Optional&lt;Boolean&gt; visible = vBox.getChildren().stream()
.filter(node -&gt; node instanceof Button)
.map(node -&gt; (Button) node)
.filter(btn -&gt; !btn.getText().equals(chosen))
.findFirst().map(Button::isVisible);
// Change visibility of the buttons except the filtered chosen one:
vBox.getChildren().stream()
.filter(node -&gt; node instanceof Button)
.map(node -&gt; (Button) node)
.filter(btn -&gt; !btn.getText().equals(chosen))
.forEach(btn -&gt; btn.setVisible(!btn.isVisible()));
// Change the (not so) chosen one:
if (visible.isPresent() &amp;&amp; !visible.get()) {
int nextIndex = random.nextInt(10);
for (int j = 0; j &lt; vBox.getChildren().size(); j++) {
if (vBox.getChildren().get(j) instanceof Button) {
Button btn = (Button) vBox.getChildren().get(j);
if (j == nextIndex)
btn.setText(chosen);
else
btn.setText(simple);
}
}
vBox.requestFocus();
}
}
});
}
primaryStage.setScene(new Scene(vBox));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

huangapple
  • 本文由 发表于 2020年5月29日 09:49:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/62077380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定