package bug; import com.sun.javafx.collections.ObservableListWrapper; import java.util.Arrays; import javafx.application.Application; import javafx.beans.property.SimpleObjectProperty; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.Event; import javafx.event.EventHandler; import javafx.geometry.HPos; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.control.Accordion; import javafx.scene.control.Button; import javafx.scene.control.CheckBox; import javafx.scene.control.CheckBoxBuilder; import javafx.scene.control.ChoiceBox; import javafx.scene.control.ColorPicker; import javafx.scene.control.Control; import javafx.scene.control.Label; import javafx.scene.control.ListView; import javafx.scene.control.MenuItem; import javafx.scene.control.PasswordFieldBuilder; import javafx.scene.control.ProgressBarBuilder; import javafx.scene.control.ProgressIndicatorBuilder; import javafx.scene.control.RadioButtonBuilder; import javafx.scene.control.Separator; import javafx.scene.control.SeparatorBuilder; import javafx.scene.control.SplitMenuButton; import javafx.scene.control.SplitMenuButtonBuilder; import javafx.scene.control.SplitPane; import javafx.scene.control.SplitPaneBuilder; import javafx.scene.control.Tab; import javafx.scene.control.TabPane; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.TextFieldBuilder; import javafx.scene.control.TitledPane; import javafx.scene.control.ToggleButtonBuilder; import javafx.scene.control.TreeItem; import javafx.scene.control.TreeView; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.scene.layout.StackPaneBuilder; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.Rectangle; import javafx.scene.text.TextAlignment; import javafx.stage.Stage; import javafx.util.Callback; public class Bug extends Application { private GridPane gpGlobal = null; private GridPane gpLocal = null; private VBox vbLocal = null; private ChoiceBox cbExisting = null; private ChoiceBox cbCreating = null; private Button bUpdateExisting = null; private Button bUpdateCreating = null; private enum Controls { SplitPane, Accordion, TitledPane } private ChoiceBox getChoice() { ChoiceBox cb = new ChoiceBox(); for (ControlsFactory c : ControlsFactory.values()) { cb.getItems().add(c); } return cb; } private GridPane initLocalGp() { GridPane gp = new GridPane(); gp.setPrefSize(202, 202); gp.setMinSize(202, 202); gp.setHgap(3); gp.setVgap(3); return gp; } @Override public void start(Stage stage) throws Exception { gpGlobal = new GridPane(); gpLocal = initLocalGp(); vbLocal = new VBox(); bUpdateExisting = new Button("Update1"); bUpdateCreating = new Button("Update2"); //gpGlobal.setPrefWidth(400); vbLocal.setPrefWidth(200); cbCreating = getChoice(); cbExisting = getChoice(); bUpdateExisting.setOnMouseClicked(new EventHandler() { @Override public void handle(Event t) { if (cbExisting.getValue() != null) { gpLocal = initLocalGp(); gpLocal.add(cbExisting.getValue().createNode(), 0, 0); gpGlobal.getChildren().clear(); gpGlobal.add(gpLocal, 0, 0); gpGlobal.add(vbLocal, 1, 0); } } }); bUpdateCreating.setOnMouseClicked(new EventHandler() { @Override public void handle(Event t) { if (cbCreating.getValue() != null) { gpLocal.add(cbCreating.getValue().createNode(), 1, 0); gpGlobal.getChildren().clear(); gpGlobal.add(gpLocal, 0, 0); gpGlobal.add(vbLocal, 1, 0); } } }); vbLocal.getChildren().addAll(cbExisting, bUpdateExisting, cbCreating, bUpdateCreating); gpGlobal.add(gpLocal, 0, 0); gpGlobal.add(vbLocal, 1, 0); Scene scene = new Scene(gpGlobal, 500, 500); stage.setScene(scene); stage.show(); } /** * The main() method is ignored in correctly deployed JavaFX application. * main() serves only as fallback in case the application can not be * launched through deployment artifacts, e.g., in IDEs with limited FX * support. NetBeans ignores main(). * * @param args the command line arguments */ public static void main(String[] args) { launch(args); } public enum ControlsFactory { ColorPickers(new ControlFactory() { public Control createControl() { ColorPicker cp = new ColorPicker(); return cp; } }), CheckBoxes(new ControlFactory() { public Control createControl() { return CheckBoxBuilder.create().text("Check box the first line" + "\nthe sec long line" + "\nthe third line").graphic(new Rectangle(20, 20, Color.web("lightblue"))).focusTraversable(false).build(); } }), RadioButtons(new ControlFactory() { public Control createControl() { return RadioButtonBuilder.create().text("Radio the first line" + "\nthe sec long line" + "\nthe third line").graphic(new Rectangle(20, 20, Color.web("lightblue"))).focusTraversable(false).textAlignment(TextAlignment.RIGHT).build(); } }), TextFields(new ControlFactory() { public Control createControl() { return TextFieldBuilder.create().text("Text box the first line" + "\nthe sec long line" + "\nthe third line").focusTraversable(false).build(); } }), PasswordFields(new ControlFactory() { public Control createControl() { return PasswordFieldBuilder.create().promptText("Password box the first line" + "\nthe sec long line" + "\nthe third line").focusTraversable(false).build(); } }), Separators(new ControlFactory() { public Control createControl() { Separator sep = SeparatorBuilder.create().halignment(HPos.CENTER).build(); sep.setPrefWidth(80); return sep; } }), ProgressIndicators(new ControlFactory() { public Node createControl() { return ProgressIndicatorBuilder.create().progress(0.85).focusTraversable(false).build(); } }), ProgressBars(new ControlFactory() { public Node createControl() { return ProgressBarBuilder.create().progress(0.25).focusTraversable(false).build(); } }), ListViews(new ControlFactory() { public Node createControl() { ListView list = new ListView(); ObservableListWrapper items = new ObservableListWrapper(Arrays.asList("One", "Two", "Three", "Four", "Five", "Six", "long line long line long line long line")); list.setItems(items); list.setPrefWidth(100); list.setPrefHeight(100); list.setFocusTraversable(false); return list; } }), PressedToggleButtons(new ControlFactory() { public Control createControl() { return ToggleButtonBuilder.create().selected(true).text("Button the first line" + "\nthe sec long line" + "\nthe third line").focusTraversable(false).graphic(new Rectangle(10, 10, Color.RED)).build(); } }), UnPressedToggleButtons(new ControlFactory() { public Control createControl() { return ToggleButtonBuilder.create().selected(false).text("Button the first line" + "\nthe sec long line" + "\nthe third line").focusTraversable(false).graphic(new Rectangle(10, 10, Color.RED)).build(); } }), SplitMenuButtons(new ControlFactory() { public Node createControl() { SplitMenuButton smb = SplitMenuButtonBuilder.create() .text("Split box the first line" + "\nthe sec long line" + "\nthe third line") .items(new MenuItem("Split box the first line" + "\nthe sec long line" + "\nthe third line", new Rectangle(10, 10, Color.BLUE))) .graphic(new Rectangle(10, 10, Color.RED)) .build(); smb.setFocusTraversable(false); return smb; } }), TabPanes(new ControlFactory() { public Node createControl() { TabPane pane = new TabPane(); Tab tab = new Tab(); tab.setText("tab 1"); HBox content1 = new HBox(10); content1.getChildren().addAll(new Button("Button"), new Label("Label"), new Rectangle(40, 40, Color.TOMATO)); tab.setContent(content1); Tab tab2 = new Tab(); tab2.setText("tab 2"); tab2.setContent(new Circle(40, Color.RED)); pane.getTabs().addAll(tab, tab2); pane.setFocusTraversable(false); return pane; } }), TitledPanes(new ControlFactory() { public Node createControl() { TitledPane tpane = new TitledPane(); tpane.setGraphic(new Label("Title")); VBox content = new VBox(5); content.getChildren().addAll(new Label("Label"), new Button("Button"), new CheckBox("Check box")); tpane.setContent(content); tpane.setAnimated(false); return tpane; } }), TableViews(new ControlFactory() { public Node createControl() { ObservableList items = FXCollections.observableArrayList(); for (int i = 0; i < 10; i++) { items.add(new Person("name " + i, "surname " + i)); } TableColumn column1 = new TableColumn("First Name"); column1.setCellValueFactory(new Callback, ObservableValue>() { @Override public ObservableValue call(final TableColumn.CellDataFeatures p) { SimpleObjectProperty text = new SimpleObjectProperty(); text.setValue(new Label(p.getValue().getFirstName())); return text; } }); TableColumn column2 = new TableColumn("Last Name"); column2.setCellValueFactory(new Callback, ObservableValue>() { @Override public ObservableValue call(final TableColumn.CellDataFeatures p) { SimpleObjectProperty text = new SimpleObjectProperty(); text.setValue(new Label(p.getValue().getLastName())); return text; } }); TableView table = new TableView(items); table.getColumns().setAll(column1, column2); table.setPrefHeight(200); table.setFocusTraversable(false); return table; } }), TreeViews(new ControlFactory() { public Node createControl() { TreeItem root = new TreeItem("ROOT", new Rectangle(20, 20, Color.CHOCOLATE)); root.setExpanded(true); TreeItem firstBrunch = new TreeItem("brunch 1"); firstBrunch.setExpanded(true); firstBrunch.getChildren().addAll(new TreeItem("first item"), new TreeItem("second item", new Rectangle(20, 20, Color.DARKGREY))); root.getChildren().addAll(firstBrunch); TreeItem secondBrunch = new TreeItem("brunch 2"); secondBrunch.getChildren().addAll(new TreeItem("first item"), new TreeItem("second item", new Rectangle(20, 20, Color.DARKGREY))); root.getChildren().addAll(secondBrunch); TreeView tree = new TreeView(root); tree.setFocusTraversable(false); return tree; } }), Accordions(new ControlFactory() { public Node createControl() { TitledPane pane1 = new TitledPane(); pane1.setGraphic(new Label("title 1\nLong text long text")); pane1.setContent(new Rectangle(100, 40, Color.SKYBLUE)); TitledPane pane2 = new TitledPane(); pane2.setGraphic(new Label("title 2\nLong text long text")); pane2.setContent(new Rectangle(100, 40, Color.BLUEVIOLET)); Accordion acc = new Accordion(); acc.getPanes().addAll(pane1, pane2); acc.setExpandedPane(pane2); pane2.setAnimated(false); acc.setFocusTraversable(false); return acc; } }), SplitPanes(new ControlFactory() { public Node createControl() { SplitPane pane = SplitPaneBuilder.create().items( StackPaneBuilder.create().children(new Rectangle(40, 40, Color.WHITESMOKE)).build(), StackPaneBuilder.create().children(new Rectangle(40, 40, Color.BLUE)).build(), StackPaneBuilder.create().children(new Rectangle(40, 40, Color.RED)).build()).prefWidth(150).prefHeight(150).build(); pane.setDividerPositions(0.33, 0.67); pane.setFocusTraversable(false); return pane; } }),; private ControlFactory factory; ControlsFactory(ControlFactory factory) { this.factory = factory; } public Node createNode() { return factory.createControl(); } private interface ControlFactory { /** * @return current control instance */ public abstract Node createControl(); } public static class Person { Person(String firstName, String lastName) { this.firstName = new SimpleStringProperty(firstName); this.lastName = new SimpleStringProperty(lastName); } public StringProperty firstName; public void setFirstName(String value) { firstName.set(value); } public String getFirstName() { return firstName.get(); } public StringProperty firstNameProperty() { if (firstName == null) { firstName = new SimpleStringProperty(); } return firstName; } public StringProperty lastName; public void setLastName(String value) { lastName.set(value); } public String getLastName() { return lastName.get(); } public StringProperty lastNameProperty() { if (lastName == null) { lastName = new SimpleStringProperty(); } return lastName; } } } }