From: Debayan Sutradhar Date: Tue, 22 Dec 2020 01:43:59 +0530 Subject: Implemented Custom combo box --- Implemented Custom combo box --- --- 'a/src/main/java/com/StreamPi/Util/ComboBox/StreamPiComboBox.java' +++ b/src/main/java/com/StreamPi/Util/ComboBox/StreamPiComboBox.java @@ -1,14 +1,24 @@ package com.StreamPi.Util.ComboBox; +import java.util.ArrayList; import java.util.List; +import com.StreamPi.Util.FormHelper.SpaceFiller; +import com.StreamPi.Util.FormHelper.SpaceFiller.FillerType; + +import org.kordamp.ikonli.javafx.FontIcon; + import javafx.scene.control.Label; +import javafx.scene.control.ScrollPane; +import javafx.scene.control.ToggleButton; +import javafx.scene.control.ScrollPane.ScrollBarPolicy; import javafx.scene.layout.HBox; import javafx.scene.layout.StackPane; +import javafx.scene.layout.VBox; -public class StreamPiComboBox extends HBox +public class StreamPiComboBox extends HBox { - private List options; + private List options; private static StackPane stackPaneParent; @@ -18,10 +28,11 @@ public class StreamPiComboBox extends HB stackPaneParent.getStyleClass().add("combobox_pane_parent"); } - public StreamPiComboBox(List options) + public StreamPiComboBox(List options) { setup(); - this.options = options; + + setOptions(options); } public StreamPiComboBox() @@ -29,23 +40,127 @@ public class StreamPiComboBox extends HB setup(); } - private Label currentSelected; + private Label currentSelectedLabel; private void setup() { - getStyleClass().add("streampi_combo_box"); + buttons = new ArrayList<>(); + + getStyleClass().add("combo_box"); + setOnMouseClicked(event->show()); + + currentSelectedLabel = new Label(); - getChildren().add() + FontIcon fontIcon = new FontIcon(); + fontIcon.getStyleClass().add("combo_box_drop_down_icon"); + + getChildren().addAll( + currentSelectedLabel, + new SpaceFiller(FillerType.HBox), + fontIcon + ); } - public void setOptions(List options) + public void setOptions(List options) { this.options = options; + setCurrentSelectedItemIndex(0); + } + + + private int currentIndex = 0; + private List buttons; + + public ScrollPane getPopupScrollPane() + { + buttons.clear(); + + ScrollPane scrollPane = new ScrollPane(); + scrollPane.setHbarPolicy(ScrollBarPolicy.NEVER); + scrollPane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED); + + scrollPane.getStyleClass().add("combo_box_popup"); + + VBox vBox = new VBox(); + vBox.getStyleClass().add("combo_box_popup_vbox"); + vBox.prefWidthProperty().bind(scrollPane.widthProperty().subtract(10)); + + scrollPane.setContent(vBox); + + for(int i = 0;i + { + setCurrentSelectedItemIndex((int) optionButton.getUserData()); + + if(streamPiComboBoxListener != null) + streamPiComboBoxListener.onNewItemSelected(options.get(currentIndex)); + + destroy(); + }); + vBox.getChildren().addAll(optionButton); + buttons.add(optionButton); + } + + return scrollPane; + } + + public int getCurrentIndex() + { + return currentIndex; } - public void popup() + public T getCurrentSelectedItem() { + return options.get(currentIndex); + } + + public void setCurrentSelectedItemIndex(int index) + { + this.currentIndex = index; + setCurrentSelectedLabelText(streamPiComboBoxFactory.getOptionDisplayText(options.get(index))); + } + + public void setCurrentSelectedItem(T object) + { + setCurrentSelectedItemIndex(options.indexOf(object)); } - public void + private void setCurrentSelectedLabelText(String text) + { + currentSelectedLabel.setText(text); + } + + private StreamPiComboBoxListener streamPiComboBoxListener = null; + + public void setStreamPiComboBoxListener(StreamPiComboBoxListener streamPiComboBoxListener) + { + this.streamPiComboBoxListener = streamPiComboBoxListener; + } + + public void show() + { + stackPaneParent.getChildren().add(getPopupScrollPane()); + stackPaneParent.toFront(); + stackPaneParent.setVisible(true); + } + + public void destroy() + { + stackPaneParent.getChildren().clear(); + stackPaneParent.toBack(); + stackPaneParent.setVisible(false); + } + + private StreamPiComboBoxFactory streamPiComboBoxFactory; + public void setStreamPiComboBoxFactory(StreamPiComboBoxFactory streamPiComboBoxFactory) + { + this.streamPiComboBoxFactory = streamPiComboBoxFactory; + } } \ No newline at end of file --- 'a/src/main/java/com/StreamPi/Util/ComboBox/StreamPiComboBoxFactory.java' +++ b/src/main/java/com/StreamPi/Util/ComboBox/StreamPiComboBoxFactory.java @@ -1,6 +1,6 @@ package com.StreamPi.Util.ComboBox; -public abstract class StreamPiComboBoxFactory +public abstract class StreamPiComboBoxFactory { - public abstra + public abstract String getOptionDisplayText(T object); } \ No newline at end of file --- /dev/null +++ b/src/main/java/com/StreamPi/Util/ComboBox/StreamPiComboBoxListener.java @@ -0,0 +1,5 @@ +package com.StreamPi.Util.ComboBox; + +public abstract class StreamPiComboBoxListener { + public abstract void onNewItemSelected(T selectedItem); +} --- 'a/src/main/java/module-info.java' +++ b/src/main/java/module-info.java @@ -1,9 +1,9 @@ module com.StreamPi.Util { - requires org.kordamp.ikonli.javafx; - requires org.kordamp.ikonli.fontawesome5; - requires javafx.base; - requires javafx.controls; + requires transitive org.kordamp.ikonli.javafx; + requires transitive org.kordamp.ikonli.fontawesome5; + requires transitive javafx.base; + requires transitive javafx.controls; exports com.StreamPi.Util.Version; exports com.StreamPi.Util.Exception; @@ -11,4 +11,5 @@ module com.StreamPi.Util { exports com.StreamPi.Util.FormHelper; exports com.StreamPi.Util.StartAtBoot; exports com.StreamPi.Util.Alert; + exports com.StreamPi.Util.ComboBox; } \ No newline at end of file