JavaFX – setbuttoncell for combobox
•
Java
I have a problem I can't solve I want to implement the progress bar into a combo box:
public class JavaFXApplication4 extends Application { @Override public void start(Stage primaryStage) { double y1 = 15; ProgressBar p1 = new ProgressBar(); p1.setLayoutY(y1); H@R_633_2419@ vb1 = new H@R_633_2419@(10); vb1.getChildren().addAll(new Label("Progressbar 1"),p1); double y2 = 15; ProgressBar p2 = new ProgressBar(); p2.setLayoutY(y2); H@R_633_2419@ vb2 = new H@R_633_2419@(10); vb2.getChildren().addAll(new Label("Progressbar 2"),p2); double y3 = 15; ProgressBar p3 = new ProgressBar(); p3.setLayoutY(y3); H@R_633_2419@ vb3 = new H@R_633_2419@(10); 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 Combo@R_633_2419@<H@R_633_2419@> combo = new Combo@R_633_2419@<>(); public TextChooser(H@R_633_2419@... 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,10)); combo.getItems().setAll(options); combo.setCellFactory(new Callback<ListView<H@R_633_2419@>,ListCell<H@R_633_2419@>>() { @Override public ListCell<H@R_633_2419@> call(ListView<H@R_633_2419@> p) { return new ListCell<H@R_633_2419@>() { @Override protected void updateItem(H@R_633_2419@ 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); } } }
Can you tell me how to use setbuttoncell to display the progressbar in the combo box?
Solution
For example:
public class JavaFXApplication5 extends Application { public static void main( String[] args ) { launch( args ); } public class ProgressData { private final DoubleProperty progressProp = new SimpleDoubleproperty(); private final StringProperty progressName = new SimpleStringproperty(); public ProgressData( String name,double progress ) { progressProp.set( progress ); progressName.set( name ); } public DoubleProperty progressproperty() { return progressProp; } public StringProperty nameproperty() { return progressName; } @Override // Lazy hack for the combo button. public String toString() { return progressName.get(); } } @Override public void start( Stage primaryStage ) { ProgressData vb1 = new ProgressData( "Progressbar 1",-1 ); ProgressData vb2 = new ProgressData( "Progressbar 2",0.2 ); ProgressData vb3 = new ProgressData( "Progressbar 3",0.3 ); TextChooser textChooser = new TextChooser( vb1,vb3 ); textChooser.setStyle( "-fx-font: 10px \"Verdana\";" ); StackPane root = new StackPane(); root.getChildren().add( textChooser ); Scene scene = new Scene( root,250 ); primaryStage.setTitle( "Hello World!" ); primaryStage.setScene( scene ); primaryStage.show(); } public static class TextChooser extends StackPane { private final Label label = new Label(); private final Combo@R_633_2419@<ProgressData> combo = new Combo@R_633_2419@<>(); public TextChooser(ProgressData... options) { StackPane.setAlignment( label,Pos.CENTER_LEFT ); StackPane.setAlignment( combo,Pos.CENTER_LEFT ); final ProgressBar labelBar = new ProgressBar(); label.visibleproperty().bind( combo.visibleproperty().not() ); label.setContentDisplay( ContentDisplay.RIGHT ); label.setGraphic( labelBar ); combo.getItems().setAll( options ); // This will change the label's text and the progress bar value. combo.getSelectionModel().selectedItemproperty().addListener( new changelistener<ProgressData>() { @Override public void changed( ObservableValue<? extends ProgressData> observable,ProgressData oldValue,ProgressData newValue ) { if ( labelBar.progressproperty().isBound() ) { labelBar.progressproperty().unbind(); } labelBar.progressproperty().bind( newValue.progressproperty() ); label.setText( newValue.nameproperty().get() ); } } ); combo.setCellFactory( new Callback<ListView<ProgressData>,ListCell<ProgressData>>() { @Override public ListCell<ProgressData> call( ListView<ProgressData> p ) { return new ListCell<ProgressData>() { private final ProgressBar cellBar = new ProgressBar(); { cellBar.setMouseTransparent( true ); setContentDisplay( ContentDisplay.RIGHT ); setGraphic( cellBar ); } @Override protected void updateItem( ProgressData item,boolean empty ) { super.updateItem( item,empty ); if ( item != null && ! empty ) { if ( cellBar.progressproperty().isBound() ) { cellBar.progressproperty().unbind(); } cellBar.progressproperty().bind( item.progressproperty() ); setText( item.nameproperty().get() ); } } }; } } ); combo.getSelectionModel().select( 0 ); combo.setVisible( true ); label.setOnMouseEntered( new EventHandler<MouseEvent>() { @Override public void handle( MouseEvent event ) { combo.setVisible( true ); } } ); combo.showingproperty().addListener( new InvalidationListener() { @Override public void invalidated( Observable observable ) { if ( !combo.isShowing() ) { combo.setVisible( false ); } } } ); combo.setOnMouseExited( new EventHandler<MouseEvent>() { @Override public void handle( MouseEvent event ) { if ( !combo.isShowing() ) { combo.setVisible( false ); } } } ); getChildren().setAll( label,combo ); } } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码