Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Combox ListView中显示动态变化的文本_Java_Text_Dynamic_Javafx_Combobox - Fatal编程技术网

Java 如何在Combox ListView中显示动态变化的文本

Java 如何在Combox ListView中显示动态变化的文本,java,text,dynamic,javafx,combobox,Java,Text,Dynamic,Javafx,Combobox,我想在加载列表内容时,在组合框列表中显示动态变化的加载文本。在获取内容时显示一些文本不是问题。问题是如何使文本动态更改。此代码将动态更改消息显示为标签: import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.application.Application; import javafx.event.Event; import javafx.event.EventHandler; impor

我想在加载列表内容时,在组合框列表中显示动态变化的加载文本。在获取内容时显示一些文本不是问题。问题是如何使文本动态更改。此代码将动态更改消息显示为
标签

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

public class SSCCE extends Application {

    @Override
    public void start(Stage stage) {

        VBox root = new VBox();

        final Label message = new Label("Loading");

        final Timeline timeline = new Timeline(new KeyFrame(
                Duration.ZERO, new EventHandler() {
                    @Override
                    public void handle(Event event) {
                        String messageText = message.getText();
                        message.setText(("Loading . . ."
                                .equals(messageText)) ? "Loading ."
                                : messageText + " .");
                    }
                }), new KeyFrame(Duration.millis(500)));
        timeline.setCycleCount(Timeline.INDEFINITE);

        root.getChildren().add(message);

        timeline.play();

        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();

    }

    public static void main(String[] args) {
        launch();
    }
}
但问题是,因为将
节点
对象放入项目列表是(而且看起来很奇怪)。我需要把它们放在别的东西里

我想我通过使用
SimpleStringProperty
解决了这个问题,因为它实现了
Observable

import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;

public class SSCCE2 extends Application {

    @Override
    public void start(Stage stage) {

        VBox root = new VBox();

        final SimpleStringProperty message = new SimpleStringProperty("Loading");

        final Timeline timeline = new Timeline(new KeyFrame(
                Duration.ZERO, new EventHandler() {
                    @Override
                    public void handle(Event event) {
                        String messageText = message.getValue();
                        message.setValue(("Loading . . ."
                                .equals(messageText)) ? "Loading ."
                                : messageText + " .");
                    }
                }), new KeyFrame(Duration.millis(500)));
        timeline.setCycleCount(Timeline.INDEFINITE);

        ComboBox<SimpleStringProperty> comboBox = new ComboBox<SimpleStringProperty>();
        comboBox.getItems().add(message);

        timeline.play();

        root.getChildren().add(comboBox);

        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();

    }

    public static void main(String[] args) {
        launch();
    }
}
导入javafx.animation.KeyFrame;
导入javafx.animation.Timeline;
导入javafx.application.application;
导入javafx.beans.property.SimpleStringProperty;
导入javafx.event.event;
导入javafx.event.EventHandler;
导入javafx.scene.scene;
导入javafx.scene.control.ComboBox;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
导入javafx.util.Duration;
公共类SSCCE2扩展了应用程序{
@凌驾
公众假期开始(阶段){
VBox root=新的VBox();
最终SimpleStringProperty消息=新SimpleStringProperty(“加载”);
最终时间线=新时间线(新关键帧(
Duration.ZERO,新的EventHandler(){
@凌驾
公共无效句柄(事件){
字符串messageText=message.getValue();
message.setValue((“正在加载…”
.equals(messageText))?“正在加载。”
:messageText+“);
}
}),新的关键帧(持续时间.millis(500));
timeline.setCycleCount(timeline.unfinite);
ComboBox ComboBox=新建ComboBox();
comboBox.getItems().add(消息);
timeline.play();
root.getChildren().add(组合框);
场景=新场景(根);
舞台场景;
stage.show();
}
公共静态void main(字符串[]args){
发射();
}
}
但它不起作用。它只是一直显示“加载”。是的。
ComboBox
当前显示了
SimpleStringProperty
toString()
方法,但是可以通过设置
ComboBox
的单元格工厂或转换器轻松修复。我没有在这里添加这一部分,因为我想让示例尽可能小。同样,这种方法会显示SimpleStringProperty的getValue(),它是不可观察的,我们又回到了原点

无论如何。在这个例子中,什么都没有发生。这对我来说很奇怪,因为
SimpleStringProperty
实现了
Observable
,这意味着将显示任何更改。

要在组合框本身中显示动画的“加载”消息,请创建一个
列表单元格
,并从时间线更新它;然后将其设置为
组合框的
按钮cell

import java.util.Arrays;
import java.util.List;

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.concurrent.Worker.State;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;

public class ComboBoxWithLoadingMessage extends Application {

    @Override
    public void start(Stage primaryStage) {


        ComboBox<String> combo = new ComboBox<>();

        // mock loading task:
        Task<List<String>> loadingTask = new Task<List<String>>() {
            @Override
            public List<String> call() throws Exception {
                // mimic long loading process:
                Thread.sleep(5000);
                return Arrays.asList("One", "Two", "Three", "Four", "Five");
            }
        };

        loadingTask.setOnSucceeded(e -> 
            combo.getItems().setAll(loadingTask.getValue()));


        ListCell<String> buttonCell = new ListCell<String>() {
            @Override
            public void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);
                State state = loadingTask.getState() ;
                if (state == State.SUCCEEDED ) {
                    if (empty) {
                        setText(null);
                    } else {
                        setText(item);
                    }
                } 
            }
        };          

        Timeline timeline = new Timeline(
                new KeyFrame(Duration.ZERO, e -> buttonCell.setText("Loading .")),
                new KeyFrame(Duration.millis(500), e -> buttonCell.setText("Loading . .")),
                new KeyFrame(Duration.millis(1000), e -> buttonCell.setText("Loading . . .")),
                new KeyFrame(Duration.millis(1500))
        );

        timeline.setCycleCount(Animation.INDEFINITE);

        loadingTask.stateProperty().addListener((obs, oldState, newState) -> {
            if (newState == State.RUNNING) {
                timeline.play();
            } else {
                timeline.stop();
                if (buttonCell.isEmpty()) {
                    buttonCell.setText(null);
                } else {
                    buttonCell.setText(buttonCell.getItem());
                }
            }
        });

        combo.setButtonCell(buttonCell);

        new Thread(loadingTask).start();

        StackPane root = new StackPane(combo);
        Scene scene = new Scene(root, 350, 120);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
使用此选项可以使动画看起来更好

.combo-box .placeholder {
    -fx-alignment: center-left ;
    -fx-padding: 5 ;
}
在外部样式表中

您也可以考虑将占位符设置为<代码> PraseScRebug < /C> >省略时间线等,这将是一个更简单的选择。

< P>在组合框中显示动画“加载”消息,创建一个<代码> ListCys<代码>,并从时间线中更新它;然后将其设置为
组合框的
按钮cell

import java.util.Arrays;
import java.util.List;

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.concurrent.Worker.State;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;

public class ComboBoxWithLoadingMessage extends Application {

    @Override
    public void start(Stage primaryStage) {


        ComboBox<String> combo = new ComboBox<>();

        // mock loading task:
        Task<List<String>> loadingTask = new Task<List<String>>() {
            @Override
            public List<String> call() throws Exception {
                // mimic long loading process:
                Thread.sleep(5000);
                return Arrays.asList("One", "Two", "Three", "Four", "Five");
            }
        };

        loadingTask.setOnSucceeded(e -> 
            combo.getItems().setAll(loadingTask.getValue()));


        ListCell<String> buttonCell = new ListCell<String>() {
            @Override
            public void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);
                State state = loadingTask.getState() ;
                if (state == State.SUCCEEDED ) {
                    if (empty) {
                        setText(null);
                    } else {
                        setText(item);
                    }
                } 
            }
        };          

        Timeline timeline = new Timeline(
                new KeyFrame(Duration.ZERO, e -> buttonCell.setText("Loading .")),
                new KeyFrame(Duration.millis(500), e -> buttonCell.setText("Loading . .")),
                new KeyFrame(Duration.millis(1000), e -> buttonCell.setText("Loading . . .")),
                new KeyFrame(Duration.millis(1500))
        );

        timeline.setCycleCount(Animation.INDEFINITE);

        loadingTask.stateProperty().addListener((obs, oldState, newState) -> {
            if (newState == State.RUNNING) {
                timeline.play();
            } else {
                timeline.stop();
                if (buttonCell.isEmpty()) {
                    buttonCell.setText(null);
                } else {
                    buttonCell.setText(buttonCell.getItem());
                }
            }
        });

        combo.setButtonCell(buttonCell);

        new Thread(loadingTask).start();

        StackPane root = new StackPane(combo);
        Scene scene = new Scene(root, 350, 120);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
使用此选项可以使动画看起来更好

.combo-box .placeholder {
    -fx-alignment: center-left ;
    -fx-padding: 5 ;
}
在外部样式表中

您也可以考虑将占位符设置为<代码> PraseScRebug < /C> >省略时间线等,这将是一个更简单的选择。

< P>在组合框中显示动画“加载”消息,创建一个<代码> ListCys<代码>,并从时间线中更新它;然后将其设置为
组合框的
按钮cell

import java.util.Arrays;
import java.util.List;

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.concurrent.Worker.State;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;

public class ComboBoxWithLoadingMessage extends Application {

    @Override
    public void start(Stage primaryStage) {


        ComboBox<String> combo = new ComboBox<>();

        // mock loading task:
        Task<List<String>> loadingTask = new Task<List<String>>() {
            @Override
            public List<String> call() throws Exception {
                // mimic long loading process:
                Thread.sleep(5000);
                return Arrays.asList("One", "Two", "Three", "Four", "Five");
            }
        };

        loadingTask.setOnSucceeded(e -> 
            combo.getItems().setAll(loadingTask.getValue()));


        ListCell<String> buttonCell = new ListCell<String>() {
            @Override
            public void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);
                State state = loadingTask.getState() ;
                if (state == State.SUCCEEDED ) {
                    if (empty) {
                        setText(null);
                    } else {
                        setText(item);
                    }
                } 
            }
        };          

        Timeline timeline = new Timeline(
                new KeyFrame(Duration.ZERO, e -> buttonCell.setText("Loading .")),
                new KeyFrame(Duration.millis(500), e -> buttonCell.setText("Loading . .")),
                new KeyFrame(Duration.millis(1000), e -> buttonCell.setText("Loading . . .")),
                new KeyFrame(Duration.millis(1500))
        );

        timeline.setCycleCount(Animation.INDEFINITE);

        loadingTask.stateProperty().addListener((obs, oldState, newState) -> {
            if (newState == State.RUNNING) {
                timeline.play();
            } else {
                timeline.stop();
                if (buttonCell.isEmpty()) {
                    buttonCell.setText(null);
                } else {
                    buttonCell.setText(buttonCell.getItem());
                }
            }
        });

        combo.setButtonCell(buttonCell);

        new Thread(loadingTask).start();

        StackPane root = new StackPane(combo);
        Scene scene = new Scene(root, 350, 120);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
使用此选项可以使动画看起来更好

.combo-box .placeholder {
    -fx-alignment: center-left ;
    -fx-padding: 5 ;
}
在外部样式表中

您也可以考虑将占位符设置为<代码> PraseScRebug < /C> >省略时间线等,这将是一个更简单的选择。

< P>在组合框中显示动画“加载”消息,创建一个<代码> ListCys<代码>,并从时间线中更新它;然后将其设置为
组合框的
按钮cell

import java.util.Arrays;
import java.util.List;

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.concurrent.Worker.State;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.ListCell;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;

public class ComboBoxWithLoadingMessage extends Application {

    @Override
    public void start(Stage primaryStage) {


        ComboBox<String> combo = new ComboBox<>();

        // mock loading task:
        Task<List<String>> loadingTask = new Task<List<String>>() {
            @Override
            public List<String> call() throws Exception {
                // mimic long loading process:
                Thread.sleep(5000);
                return Arrays.asList("One", "Two", "Three", "Four", "Five");
            }
        };

        loadingTask.setOnSucceeded(e -> 
            combo.getItems().setAll(loadingTask.getValue()));


        ListCell<String> buttonCell = new ListCell<String>() {
            @Override
            public void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);
                State state = loadingTask.getState() ;
                if (state == State.SUCCEEDED ) {
                    if (empty) {
                        setText(null);
                    } else {
                        setText(item);
                    }
                } 
            }
        };          

        Timeline timeline = new Timeline(
                new KeyFrame(Duration.ZERO, e -> buttonCell.setText("Loading .")),
                new KeyFrame(Duration.millis(500), e -> buttonCell.setText("Loading . .")),
                new KeyFrame(Duration.millis(1000), e -> buttonCell.setText("Loading . . .")),
                new KeyFrame(Duration.millis(1500))
        );

        timeline.setCycleCount(Animation.INDEFINITE);

        loadingTask.stateProperty().addListener((obs, oldState, newState) -> {
            if (newState == State.RUNNING) {
                timeline.play();
            } else {
                timeline.stop();
                if (buttonCell.isEmpty()) {
                    buttonCell.setText(null);
                } else {
                    buttonCell.setText(buttonCell.getItem());
                }
            }
        });

        combo.setButtonCell(buttonCell);

        new Thread(loadingTask).start();

        StackPane root = new StackPane(combo);
        Scene scene = new Scene(root, 350, 120);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
使用此选项可以使动画看起来更好

.combo-box .placeholder {
    -fx-alignment: center-left ;
    -fx-padding: 5 ;
}
在外部样式表中


您也可以考虑将占位符设置为<代码>进度指示器< /代码>,省略时间线等,这将是一个更简单的选择。

这很好。但这并不是我想要的。是否可以在
组合框
下拉列表中而不是按钮单元格中显示此加载消息?这在我的应用程序中是绝对必要的。是的,只需在电池工厂而不是按钮电池中进行。我将在几小时后回到计算机时更新答案。假设列表在加载完成之前是空的,实际上比这更简单:只需使用
组合框。占位符
属性。使用该示例更新了代码。在我的实现中,我实例化了ComboBox,然后将实例发送到另一个类并在那里显示(我希望在启动应用程序后立即启动任务,但ComboBox显示在另一个窗口中)。但是,除非在将占位符发送到其他类之前将其添加到窗格中,否则占位符不会显示。你知道为什么吗?这很好。但这并不是我想要的。是否可以在
组合框
下拉列表中而不是按钮单元格中显示此加载消息?这在我的应用程序中是绝对必要的。是的,只需在电池工厂而不是按钮电池中进行。我会在几小时后回到电脑前更新答案,假设列表是em