Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- javac FxBorderPaneExample1.java && java FxBorderPaneExample1
- ||
- #!/bin/bash
- export JFX=javafx-sdk-11.0.2/lib/
- export MODS=javafx.controls,javafx.fxml,javafx.graphics,javafx.web
- javac --module-path $JFX --add-modules $MODS $1.java && java --module-path $JFX --add-modules $MODS $1
- */
- import javafx.application.*;
- import javafx.event.*;
- import javafx.geometry.*;
- import javafx.scene.*;
- import javafx.scene.control.*;
- import javafx.scene.layout.*;
- import javafx.scene.canvas.*;
- import javafx.scene.text.*;
- import javafx.stage.*;
- import javafx.scene.input.*;
- import javafx.scene.shape.*;
- import javafx.collections.*;
- import javafx.scene.control.cell.*;
- public class FxBorderPaneExample1 extends Application
- {
- public static void main(String[] args) {
- Application.launch(args);
- }
- @Override
- public void start(Stage stage) {
- stage.setTitle("Hello JavaFX + Menu,StatusBar,Events");
- Menu m1 = new Menu("File");
- MenuItem mi1 = new MenuItem("Open");
- MenuItem mi2 = new MenuItem("Save");
- MenuItem mi3 = new MenuItem("Quit");
- m1.getItems().add(mi1);
- m1.getItems().add(mi2);
- m1.getItems().add(new SeparatorMenuItem());
- m1.getItems().add(mi3);
- m1.setStyle(""
- +"-fx-padding: 2 5 2 5;"
- );
- Menu m2 = new Menu("Help");
- MenuItem mi4 = new MenuItem("Doc");
- MenuItem mi5 = new MenuItem("About");
- m2.getItems().add(mi4);
- m2.getItems().add(new SeparatorMenuItem());
- m2.getItems().add(mi5);
- m2.setStyle(""
- +"-fx-padding: 2 5 2 5;"
- );
- MenuBar menuBar = new MenuBar();
- menuBar.getMenus().add(m1);
- menuBar.getMenus().add(m2);
- menuBar.setStyle(""
- //+"-fx-padding: 0;"
- //+"-fx-margin: 5;"
- //+"-fx-background-color: white;"
- //+"-fx-height:;"
- // "-fx-my-menu-color: ghostwhite;"+
- // "-fx-my-menu-color-highlighted: lightgray;"+
- // "-fx-my-menu-font-color: #FFFFFF;"+
- // "-fx-my-menu-font-color-highlighted: red;"+
- // "-fx-border-style: solid inside;" +
- // "-fx-border-color: transparent;"//+
- // "-fx-focus-color: green;" +
- // +"-fx-font-size: 12px;"
- // +"-fx-font-family: Times;"
- );
- BorderPane statusBar = new BorderPane();
- statusBar.setLeft(new Text("left"));
- statusBar.setRight(new Text("right"));
- statusBar.setStyle(""
- +"-fx-padding: 2 5 2 5;"
- +"-fx-margin: 5;"
- +"-fx-border-style: solid inside;"
- +"-fx-border-color: lightgray;"
- +"-fx-background-color: linear-gradient(to bottom, ghostwhite, lightgray);"
- //+"-fx-background-color: transparent;"
- );
- statusBar.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
- @Override
- public void handle(MouseEvent e) {
- statusBar.setLeft(new Text("-"));
- }
- });
- mi1.setOnAction(e -> {
- statusBar.setLeft(new Text("open..."));
- });
- mi2.setOnAction(e -> {
- statusBar.setLeft(new Text("save..."));
- table();
- });
- mi3.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent e) {
- Platform.exit();
- }
- });
- mi4.setOnAction(e -> {
- statusBar.setLeft(new Text(menuBar.styleProperty().toString()));
- });
- TextArea textArea = new TextArea();
- ListView listView = new ListView();
- //Region left = new Region();
- //Region right = new Region();
- SplitPane.setResizableWithParent(listView, Boolean.FALSE);
- SplitPane center = new SplitPane(listView, textArea);
- center.setPrefSize(400, 400);
- BorderPane root = new BorderPane();
- root.setPrefSize(600, 400);
- root.setTop(menuBar);
- //root.setLeft(listView);
- //root.setCenter(textArea);
- root.setCenter(center);
- root.setBottom(statusBar);
- root.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
- @Override
- public void handle(MouseEvent e) {
- statusBar.setLeft(new Text(root.getWidth()+" "+root.getHeight()));
- listView.getItems().add(e.getX()+" "+e.getY());
- }
- });
- textArea.addEventFilter(MouseEvent.MOUSE_MOVED, new EventHandler<MouseEvent>() {
- @Override
- public void handle(MouseEvent e) {
- textArea.appendText(e.getX()+" "+e.getY()+"\n");
- }
- });
- Scene scene = new Scene(root);
- stage.setScene(scene);
- stage.show();
- }
- private void table() {
- Stage stage2 = new Stage();
- stage2.setTitle("Table View Sample");
- stage2.setWidth(300);
- stage2.setHeight(500);
- TableView<Person> table = new TableView<Person>();
- ObservableList<Person> data = FXCollections.observableArrayList(
- new Person("Jacob", "Smith", "jacob.smith@example.com"),
- new Person("Isabella", "Johnson", "isabella.johnson@example.com"),
- new Person("Ethan", "Williams", "ethan.williams@example.com"),
- new Person("Emma", "Jones", "emma.jones@example.com"),
- new Person("Michael", "Brown", "michael.brown@example.com")
- );
- TableColumn firstNameCol = new TableColumn("First Name");
- TableColumn lastNameCol = new TableColumn("Last Name");
- TableColumn emailCol = new TableColumn("Email");
- firstNameCol.setCellValueFactory( new PropertyValueFactory<Person,String>("firstName") );
- lastNameCol.setCellValueFactory( new PropertyValueFactory<Person,String>("lastName") );
- emailCol.setCellValueFactory( new PropertyValueFactory<Person,String>("email") );
- table.getColumns().addAll(firstNameCol, lastNameCol, emailCol);
- table.setItems(data);
- table.setEditable(true);
- BorderPane bp2 = new BorderPane();
- bp2.setTop(new Label("Address Book"));
- bp2.setCenter(table);
- //bp2.setBottom((new StatusBar(new Text(".."))).get());
- bp2.setBottom(new Label("..."));
- Scene scene = new Scene(bp2);
- stage2.setScene(scene);
- stage2.show();
- }
- public class StatusBar extends BorderPane {
- BorderPane sbbp = new BorderPane();
- private Text left = new Text();
- private Text middle = new Text();
- private Text right = new Text();
- private StatusBar(Text l) {
- this.left = l;
- sbbp.setLeft(l);
- }
- private StatusBar(Text l, Text r) {
- this.left = l;
- this.right = r;
- }
- private StatusBar(Text l, Text m, Text r) {
- this.left = l;
- this.middle = m;
- this.right = r;
- }
- public BorderPane get() {
- return sbbp;
- }
- }
- public static class Person {
- private String firstName;
- private String lastName;
- private String email;
- private Person(String fName, String lName, String email) {
- this.firstName = new String(fName);
- this.lastName = new String(lName);
- this.email = new String(email);
- }
- public String getFirstName() {
- return firstName;
- }
- public void setFirstName(String fName) {
- firstName=fName;
- }
- public String getLastName() {
- return lastName;
- }
- public void setLastName(String fName) {
- lastName=fName;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String fName) {
- email=fName;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement