我有一个带进度条的组合框的例子。Combobox进度条无法正常工作
package javafxapplication4;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;
public class JavaFXApplication4 extends Application
{
@Override
public void start(Stage primaryStage)
{
double y1 = 15;
ProgressBar p1 = new ProgressBar();
p1.setLayoutY(y1);
VBox vb1 = new VBox();
vb1.getChildren().addAll(new Label("Progressbar 1"), p1);
double y2 = 15;
ProgressBar p2 = new ProgressBar();
p2.setLayoutY(y2);
VBox vb2 = new VBox();
vb2.getChildren().addAll(new Label("Progressbar 2"), p2);
double y3 = 15;
ProgressBar p3 = new ProgressBar();
p3.setLayoutY(y3);
VBox vb3 = new VBox();
vb3.getChildren().addAll(new Label("Progressbar 3"), p3);
TextChooser textChooser = new TextChooser(
vb1, vb2, vb3
);
textChooser.setStyle("-fx-font: 10px \"Verdana\";");
StackPane root = new StackPane();
root.getChildren().add(textChooser);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static class TextChooser extends StackPane {
private Label label = new Label();
private ComboBox<VBox> combo = new ComboBox<>();
public TextChooser(VBox... options) {
StackPane.setAlignment(label, Pos.CENTER_LEFT);
StackPane.setAlignment(combo, Pos.CENTER_LEFT);
label.graphicProperty().bind(
//combo.getSelectionModel().selectedItemProperty()
combo.getSelectionModel().selectedItemProperty()
);
label.visibleProperty().bind(
combo.visibleProperty().not()
);
//label.setPadding(new Insets(0, 0, 0, 10));
combo.getItems().setAll(options);
combo.setCellFactory(new Callback<ListView<VBox>, ListCell<VBox>>() {
@Override public ListCell<VBox> call(ListView<VBox> p) {
return new ListCell<VBox>() {
@Override protected void updateItem(VBox item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
} else {
setGraphic(item);
}
}
};
}
});
combo.getSelectionModel().select(0);
combo.setVisible(true);
label.setOnMouseEntered(event -> combo.setVisible(true));
combo.showingProperty().addListener(observable -> {
if (!combo.isShowing()) {
combo.setVisible(false);
}
});
combo.setOnMouseExited(event -> {
if (!combo.isShowing()) {
combo.setVisible(false);
}
});
getChildren().setAll(label, combo);
}
public static void main(String[] args)
{
launch(args);
}
}
}
我想在应用程序工作时显示带有工作后台进程的进度条。当我将鼠标移到进度条上时,我想查看所有进程。但由于某些原因代码工作不正常 - 当我选择进度条时,没有进度条显示。你能帮我看看这段代码吗?
非常好的例子。谢谢! –