Snippets
Created by
Terah
Empty file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | package application;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Pane;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
FlowPane fp = new FlowPane();
fp.setHgap(10);
fp.setVgap(15);
root.setBottom(fp);
root.setTop(new Label("ASK YOUR QUESTION/SEARCH FOR YOUR VIDEO :)"));
TextField tf = new TextField("start typing");
tf.setMinSize(250, 250);
root.setCenter(tf);
Button b = new Button("ADD TAGS");
b.setStyle("-fx-background-color: green;");
b.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent paramT) {
fp.getChildren().add(new Tag("New Tag" + fp.getChildren().size()));
}
});
root.setRight(b);
Scene scene = new Scene(root,500,400);
scene.getStylesheets().add(getClass().
getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
private class Tag extends Label{
public Tag() {
super();
initTag();
}
private Tag(String arg0, Node arg1) {
super(arg0, arg1);
}
public Tag(String arg0) {
super(arg0);
initTag();
}
private final void initTag(){
Path path = new Path();
/**
* you will need to increase the 5 if you want the close button to be big
*/
path.getElements().addAll(
new MoveTo(0,0),new LineTo(5, 5),
new MoveTo(5,0), new LineTo(0,5));
path.setStyle("-fx-stroke: red;");
path.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent paramT) {
Node n = Tag.this.getParent();
if(n instanceof Pane){//of course it is
((Pane)n).getChildren().remove(Tag.this);
}
}
});
setPadding(new Insets(3,5,3,5));
setGraphic(path);
setContentDisplay(ContentDisplay.RIGHT);
setGraphicTextGap(8);
graphicProperty().addListener(new ChangeListener<Node>() {
@Override
public void changed(ObservableValue<? extends Node> paramObservableValue,
Node paramT1, Node paramT2) {
if(paramT2 != path){
setGraphic(path);
}
}
});
setStyle("-fx-background-color: gold; "
+ "-fx-border-radius: 3;"
+ "-fx-border-color: red;");
}
}
public static void main(String[] args) {
launch(args);
}
}
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.