Snippets

Terah TagDisplay

Created by Terah
Empty file.
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)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.