Implementing label bar in JavaFX
Demonstration answer: (3:10 a.m. on May 29)
**10 / 7 / 2016 * * you can find the code in GitHub
Answer to practical questions: (19:53 on May 22)
The title may not be very good, but what I want to do is something in JavaFX:
YouTube's:
Stackoverflow (with and autocomplete):
Question: I don't need to write code for me Instead, I want to know how to use JavaFX and some ideas to implement it
Solution
For labels, you can use a that contains a text (label name) node and a custom style H@R_160_2419 @(button (x)) By playing with the background and border, you can achieve the desired appearance of the label
The onAction handler for this button should remove the tag from its parent
For the entire tab bar, you can use another H@R_160_2419 @. use appropriate borders for correct appearance In addition to the tag, add a textfield without background as the last element, and set the hgrow attribute of the textfield to priority Always to overwrite the remaining free space
The onAction handler of this textfield adds a new tag and clears the contents of the textfield
You can use controlsfx's autocomplete feature with textfield or implement its own custom appearance
public class TagBar extends H@R_160_2419@ { private final ObservableList<String> tags; private final TextField inputTextField; public ObservableList<String> getTags() { return tags; } public TagBar() { getStyleClass().setAll("tag-bar"); getStylesheets().add(getClass().getResource("style.css").toExternalForm()); tags = FXCollections.observableArrayList(); inputTextField = new TextField(); inputTextField.setOnAction(evt -> { String text = inputTextField.getText(); if (!text.isEmpty() && !tags.contains(text)) { tags.add(text); inputTextField.clear(); } }); inputTextField.prefHeightproperty().bind(this.heightproperty()); H@R_160_2419@.setHgrow(inputTextField,Priority.ALWAYS); inputTextField.setBackground(null); tags.addListener((Listchangelistener.Change<? extends String> change) -> { while (change.next()) { if (change.wasPermutated()) { ArrayList<Node> newSublist = new ArrayList<>(change.getTo() - change.getFrom()); for (int i = change.getFrom(),end = change.getTo(); i < end; i++) { newSublist.add(null); } for (int i = change.getFrom(),end = change.getTo(); i < end; i++) { newSublist.set(change.getPermutation(i),getChildren().get(i)); } getChildren().subList(change.getFrom(),change.getTo()).clear(); getChildren().addAll(change.getFrom(),newSublist); } else { if (change.wasRemoved()) { getChildren().subList(change.getFrom(),change.getFrom() + change.getRemovedSize()).clear(); } if (change.wasAdded()) { getChildren().addAll(change.getFrom(),change.getAddedSubList().stream().map(Tag::new).collect(Collectors.toList())); } } } }); getChildren().add(inputTextField); } private class Tag extends H@R_160_2419@ { public Tag(String tag) { getStyleClass().setAll("tag"); Button removeButton = new Button("X"); removeButton.setOnAction((evt) -> tags.remove(tag)); Text text = new Text(tag); H@R_160_2419@.setMargin(text,new Insets(0,5)); getChildren().addAll(text,removeButton); } } }
style. CSS file
.tag-bar { -fx-border-color: blue; -fx-spacing: 3; -fx-padding: 3; -fx-max-height: 30; } .tag-bar .tag { -fx-background-color: lightblue; -fx-alignment: center; } .tag-bar .tag .button { -fx-background-color: transparent; }
@Override public void start(Stage primaryStage) { Button btn = new Button("Sort"); StackPane.setAlignment(btn,Pos.BOTTOM_CENTER); TagBar tagBar = new TagBar(); btn.setOnAction((ActionEvent event) -> { FXCollections.sort(tagBar.getTags()); }); Button btn2 = new Button("add \"42\""); btn2.setOnAction(evt -> { if (!tagBar.getTags().contains("42")) { tagBar.getTags().add("42"); } }); V@R_160_2419@ root = new V@R_160_2419@(); root.getChildren().addAll(tagBar,btn,btn2); root.setPrefSize(300,400); Scene scene = new Scene(root); primaryStage.setScene(scene); primaryStage.show(); }