Example usage for javafx.scene.control Button setOnAction

List of usage examples for javafx.scene.control Button setOnAction

Introduction

In this page you can find the example usage for javafx.scene.control Button setOnAction.

Prototype

public final void setOnAction(EventHandler<ActionEvent> value) 

Source Link

Usage

From source file:Main.java

@Override
public void start(Stage primaryStage) {
    BorderPane root = new BorderPane();
    Scene scene = new Scene(root, 400, 250, Color.WHITE);

    GridPane gridpane = new GridPane();
    gridpane.setPadding(new Insets(5));
    gridpane.setHgap(10);/*from   w  w  w.jav  a 2  s  .c o m*/
    gridpane.setVgap(10);

    ColumnConstraints column1 = new ColumnConstraints(150, 150, Double.MAX_VALUE);
    ColumnConstraints column2 = new ColumnConstraints(50);
    ColumnConstraints column3 = new ColumnConstraints(150, 150, Double.MAX_VALUE);

    column1.setHgrow(Priority.ALWAYS);
    column3.setHgrow(Priority.ALWAYS);

    gridpane.getColumnConstraints().addAll(column1, column2, column3);

    // Candidates label
    Label candidatesLbl = new Label("Candidates");
    GridPane.setHalignment(candidatesLbl, HPos.CENTER);
    gridpane.add(candidatesLbl, 0, 0);

    // Heroes label
    Label heroesLbl = new Label("Letters");
    gridpane.add(heroesLbl, 2, 0);
    GridPane.setHalignment(heroesLbl, HPos.CENTER);

    final ObservableList<String> candidates = FXCollections.observableArrayList("A", "B", "C", "D");

    final ListView<String> candidatesListView = new ListView<>(candidates);
    gridpane.add(candidatesListView, 0, 1);

    final ObservableList<String> heroes = FXCollections.observableArrayList();
    final ListView<String> heroListView = new ListView<>(heroes);
    gridpane.add(heroListView, 2, 1);

    Button sendRightButton = new Button(" > ");
    sendRightButton.setOnAction((ActionEvent event) -> {
        String potential = candidatesListView.getSelectionModel().getSelectedItem();
        if (potential != null) {
            candidatesListView.getSelectionModel().clearSelection();
            candidates.remove(potential);
            heroes.add(potential);
        }
    });

    Button sendLeftButton = new Button(" < ");
    sendLeftButton.setOnAction((ActionEvent event) -> {
        String notHero = heroListView.getSelectionModel().getSelectedItem();
        if (notHero != null) {
            heroListView.getSelectionModel().clearSelection();
            heroes.remove(notHero);
            candidates.add(notHero);
        }
    });

    // place the buttons
    VBox vbox = new VBox(5);
    vbox.setAlignment(Pos.CENTER);
    vbox.getChildren().addAll(sendRightButton, sendLeftButton);

    GridPane.setHalignment(vbox, HPos.CENTER);
    gridpane.add(vbox, 1, 1);

    // place the grid
    root.setCenter(gridpane);
    GridPane.setVgrow(root, Priority.ALWAYS);
    primaryStage.setScene(scene);
    primaryStage.show();
}

From source file:org.pdfsam.ui.news.NewsStageControllerTest.java

@Override
protected Parent getRootNode() {
    Button button = new Button("show");
    button.setOnAction(
            e -> eventStudio().broadcast(new ShowStageRequest(), NewsStageController.NEWSSTAGE_EVENTSTATION));
    applicationContext.getBean(NewsStage.class);
    applicationContext.getBean(NewsStageController.class);
    return button;
}

From source file:isbnfilter.ISBNFilter.java

@Override
public void start(Stage primaryStage) {
    Button btn = new Button();
    btn.setText("Say 'Hello World'");
    btn.setOnAction(new EventHandler<ActionEvent>() {

        @Override/*from   www .java 2  s  . c o m*/
        public void handle(ActionEvent event) {
            System.out.println("Hello World!");
            try {
                List<String[]> isbns = readFile(primaryStage); //Lee y mete todo en un string
                ISBNConverter classISBN = new ISBNConverter();
                classISBN.createNewFile(isbns);
            } catch (IOException | JSONException ex) {
                Logger.getLogger(ISBNFilter.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });

    StackPane root = new StackPane();
    root.getChildren().add(btn);

    Scene scene = new Scene(root, 300, 250);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);

    primaryStage.show();
}

From source file:org.pdfsam.ui.dialog.OverwriteConfirmationDialog.java

private Button buildButton(String text, boolean result) {
    Button button = new Button(text);
    button.getStyleClass().addAll(Style.BUTTON.css());
    button.setOnAction(e -> {
        this.overwrite = result;
        hide();/*w  w w.jav  a2s. c om*/
    });
    return button;
}

From source file:net.sf.anathema.framework.presenter.action.about.AnathemaAboutAction.java

private void showCloseButton(MigPane parent, final Stage aboutStage) {
    Button close = new Button(getString("Help.AboutDialog.CloseButton"));
    close.setOnAction(new EventHandler<ActionEvent>() {
        @Override// w  w w . j a  v a  2 s  .  co  m
        public void handle(ActionEvent actionEvent) {
            closeDialog(aboutStage);
        }
    });
    parent.add(close, new CC().dockSouth().alignX("center"));
}

From source file:be.makercafe.apps.makerbench.editors.TextEditor.java

/**
 * Creates the toolBar for the editor./*from   w  ww .  ja  v  a 2 s.  com*/
 *
 * @return
 */

private ToolBar createToolBar() {

    ToolBar toolBar = new ToolBar();
    toolBar.setOrientation(Orientation.HORIZONTAL);

    Button btnSave = GlyphsDude.createIconButton(MaterialDesignIcon.FLOPPY, "Save");
    btnSave.setOnAction(this::handleSaveButton);

    toolBar.getItems().add(btnSave);

    return toolBar;

}

From source file:com.thomaskuenneth.tkmactuning.TKMacTuning.java

@Override
public void start(Stage primaryStage) {
    TabPane tabPane = new TabPane();
    tabPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);
    // Found here: http://stackoverflow.com/a/17488304/5956451
    tabPane.getStyleClass().add("floating");

    // TODO: propper error handling
    JSONTokener t = new JSONTokener(getClass().getResourceAsStream("resources/plugins.json"));
    JSONArray a = new JSONArray(t);
    a.forEach((Object anObject) -> {
        JSONObject jsonObject = (JSONObject) anObject;
        addPlugin(tabPane, jsonObject.getString("class"), jsonObject.getString("pluginName"));
    });/*w w  w.j av  a2 s .  c  om*/

    FlowPane buttonsPane = new FlowPane(Orientation.HORIZONTAL);
    buttonsPane.setPadding(LayoutConstants.PADDING_1);
    buttonsPane.setHgap(LayoutConstants.HORIZONTAL_CONTROL_GAP);
    buttonsPane.setAlignment(Pos.BASELINE_LEFT);
    final Button buttonReread = new Button(getString("reread"));
    buttonReread.setOnAction(event -> {
        PluginManager.reread(this);
    });
    buttonsPane.getChildren().add(buttonReread);
    final Button buttonApply = new Button(getString("apply"));
    buttonApply.setOnAction(event -> {
        PluginManager.save(this);
    });
    buttonsPane.getChildren().add(buttonApply);

    statusbar = new StatusBar();
    BorderPane borderPane = new BorderPane(tabPane);
    borderPane.setTop(buttonsPane);
    borderPane.setBottom(statusbar);
    primaryStage.setScene(new Scene(borderPane, 800, 600));
    primaryStage.setTitle(getString("application_name"));

    ready();
    primaryStage.show();
}

From source file:org.jacp.demo.components.ContactAddDialog.java

private void createAddContactDialog() {
    final VBox box = new VBox();
    box.getStyleClass().add("jacp-option-pane");
    box.setMaxSize(300, Region.USE_PREF_SIZE);
    // the title//w  w  w .j av  a  2s . c o  m
    final Label title = new Label("Add new category");
    title.setId(GlobalConstants.CSSConstants.ID_JACP_CUSTOM_TITLE);
    VBox.setMargin(title, new Insets(2, 2, 10, 2));

    final HBox hboxInput = new HBox();
    final Label nameLabel = new Label("category name:");
    HBox.setMargin(nameLabel, new Insets(2));
    final TextField nameInput = new TextField();
    HBox.setMargin(nameInput, new Insets(0, 0, 0, 5));
    HBox.setHgrow(nameInput, Priority.ALWAYS);
    hboxInput.getChildren().addAll(nameLabel, nameInput);

    final HBox hboxButtons = new HBox();
    hboxButtons.setAlignment(Pos.CENTER_RIGHT);
    final Button ok = new Button("OK");
    HBox.setMargin(ok, new Insets(6, 5, 4, 2));
    final Button cancel = new Button("Cancel");
    HBox.setMargin(cancel, new Insets(6, 2, 4, 5));
    cancel.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(final ActionEvent arg0) {
            JACPModalDialog.getInstance().hideModalDialog();
        }
    });

    ok.setDefaultButton(true);
    ok.setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(final ActionEvent actionEvent) {
            final String catName = nameInput.getText();
            if (catName != null && StringUtils.hasText(catName)) {
                // contacts
                final Contact contact = new Contact();
                contact.setFirstName(catName);
                parent.getContext().send(contact);
                JACPModalDialog.getInstance().hideModalDialog();
            }
        }
    });

    hboxButtons.getChildren().addAll(ok, cancel);
    box.getChildren().addAll(title, hboxInput, hboxButtons);
    JACPModalDialog.getInstance().showModalDialog(box);
}

From source file:clientechat.TestList.java

@Override
public void start(Stage primaryStage) {

    WebView wv = new WebView();
    Button btn = new Button();
    TextField textField = new TextField();

    VBox vb = new VBox();

    WebEngine appendEngine = wv.getEngine();
    btn.setOnAction(new EventHandler<ActionEvent>() {
        @Override//from  ww w .jav a  2 s.  com
        public void handle(ActionEvent event) {
            executejQuery(appendEngine, "$('#content').append(\"<p align=right><b>World!"
                    + escapeHtml(textField.getText()) + "<b><p>\");");

            executejQuery(appendEngine,
                    "$(\"html, body\").animate({ scrollTop: $(document).height()-$(window).height() });");
        }
    });

    wv.getEngine().loadContent(
            "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
                    + "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" + "<head>\n"
                    + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n"
                    + "<title>Documento sin ttulo</title>\n" + "</head>\n" + "\n" + "<body id=\"content\">\n"
                    + " \n" + "</body>\n" + "</html>");

    vb.getChildren().addAll(wv, btn, textField);

    Scene scene = new Scene(vb);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
}

From source file:Main.java

private void createClipList(GridPane grid) {
    final VBox vbox = new VBox(30);
    vbox.setAlignment(Pos.TOP_CENTER);/*from  w  w  w.  ja v  a 2 s. c  o  m*/

    final Label clipLabel = new Label("Code Monkey To-Do List:");
    clipLabel.setId("clipLabel");

    final Button getUpButton = new Button("Get Up, Get Coffee");
    getUpButton.setPrefWidth(300);
    getUpButton.setOnAction(createPlayHandler(coffeeClip));

    final Button goToJobButton = new Button("Go to Job");
    goToJobButton.setPrefWidth(300);
    goToJobButton.setOnAction(createPlayHandler(jobClip));

    final Button meetingButton = new Button("Have Boring Meeting");
    meetingButton.setPrefWidth(300);
    meetingButton.setOnAction(createPlayHandler(meetingClip));

    final Hyperlink link = new Hyperlink("About Code Monkey...");
    link.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            WebView wv = new WebView();
            wv.getEngine().load("http://www.jonathancoulton.com/2006/04/14/" + "thing-a-week-29-code-monkey/");

            Scene scene = new Scene(wv, 720, 480);

            Stage stage = new Stage();
            stage.setTitle("Code Monkey");
            stage.setScene(scene);
            stage.show();
        }
    });

    vbox.getChildren().addAll(clipLabel, getUpButton, goToJobButton, meetingButton, link);

    GridPane.setHalignment(vbox, HPos.CENTER);
    GridPane.setHgrow(vbox, Priority.ALWAYS);
    GridPane.setVgrow(vbox, Priority.ALWAYS);
    grid.add(vbox, 0, 0, GridPane.REMAINING, 1);
}