Tải bản đầy đủ - 0 (trang)
1-16. Adding Tabs to the UI

1-16. Adding Tabs to the UI

Tải bản đầy đủ - 0trang

CHAPTER 1  JAVAFX FUNDAMENTALS



left.setOnAction(action);

menu.getItems().add(left);

MenuItem right = new MenuItem("Right");

right.setOnAction(action);

menu.getItems().add(right);

MenuItem top = new MenuItem("Top");

top.setOnAction(action);

menu.getItems().add(top);

MenuItem bottom = new MenuItem("Bottom");

bottom.setOnAction(action);

menu.getItems().add(bottom);

menuBar.getMenus().add(menu);

BorderPane borderPane = new BorderPane();

// generate 10 tabs

for (int i = 0; i < 10; i++) {

Tab tab = new Tab();

tab.setText("Tab" + i);

HBox hbox = new HBox();

hbox.getChildren().add(new Label("Tab" + i));

hbox.setAlignment(Pos.CENTER);

tab.setContent(hbox);

tabPane.getTabs().add(tab);

}

// add tab pane

borderPane.setCenter(tabPane);

// bind to take available space

borderPane.prefHeightProperty().bind(scene.heightProperty());

borderPane.prefWidthProperty().bind(scene.widthProperty());

// added menu bar

borderPane.setTop(menuBar);

// add border Pane

root.getChildren().add(borderPane);

primaryStage.setScene(scene);

primaryStage.show();

}

private EventHandler changeTabPlacement(final TabPane tabPane) {

return new EventHandler() {

public void handle(ActionEvent event) {

MenuItem mItem = (MenuItem) event.getSource();



61



CHAPTER 1  JAVAFX FUNDAMENTALS



String side = mItem.getText();

if ("left".equalsIgnoreCase(side)) {

tabPane.setSide(Side.LEFT);

} else if ("right".equalsIgnoreCase(side)) {

tabPane.setSide(Side.RIGHT);

} else if ("top".equalsIgnoreCase(side)) {

tabPane.setSide(Side.TOP);

} else if ("bottom".equalsIgnoreCase(side)) {

tabPane.setSide(Side.BOTTOM);

}

}

};

}

Figure 1-22 displays the tabs application, which allows a user to change the tab orientation.



Figure 1-22. TabPane



How It Works

When you use the TabPane control, you might already know the orientation in which you want your tabs

to appear. This application allows you to set the orientation by the menu options for Left, Right, Top,

and Bottom.

To use the TabPane you will immediately notice how similar it is to Java Swing’s JTabbedPanel class.

Instead of adding JPanel instances, you simply add javafx.scene.control.Tab instances. The following

code snippet adds Tab controls into a tab pane control:

TabPane tabPane = new TabPane();

Tab tab = new Tab();

tab.setText("Tab" + i);

tabPane.getTabs().add(tab);

When changing the orientation the TabPane control, use the setSide() method. The following code

line sets the orientation of the tab pane control:

tabPane.setSide(Side.BOTTOM);



62



CHAPTER 1  JAVAFX FUNDAMENTALS



1-17. Developing a Dialog Box

Problem

You want to create an application that simulates a change password dialog box.



Solution

Use JavaFX’s stage (javafx.stage.Stage) and scene (javafx.scene.Scene) APIs to create a dialog box.

The following source code listing is an application that simulates a change password dialog box. The

application contains menu options to pop up the dialog box. In addition to the menu options, the user

will have the ability to set the dialog box’s modal state (modality).

/**

* Developing A Dialog

* @author cdea

*/

public class DevelopingADialog extends Application {

static Stage LOGIN_DIALOG;

static int dx = 1;

static int dy = 1;

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

Application.launch(args);

}

private static Stage createLoginDialog(Stage parent, boolean modal) {

if (LOGIN_DIALOG != null) {

LOGIN_DIALOG.close();

}

return new MyDialog(parent, modal, "Welcome to JavaFX!");

}

@Override

public void start(final Stage primaryStage) {

primaryStage.setTitle("Chapter 1-17 Developing a Dialog");

Group root = new Group();

Scene scene = new Scene(root, 433, 312, Color.WHITE);

MenuBar menuBar = new MenuBar();

menuBar.prefWidthProperty().bind(primaryStage.widthProperty());

Menu menu = new Menu("Home");



63



CHAPTER 1  JAVAFX FUNDAMENTALS



// add change password menu itme

MenuItem newItem = new MenuItem("Change Password", null);

newItem.setOnAction(new EventHandler() {

public void handle(ActionEvent event) {

if (LOGIN_DIALOG == null) {

LOGIN_DIALOG = createLoginDialog(primaryStage, true);

}

LOGIN_DIALOG.sizeToScene();

LOGIN_DIALOG.show();

}

});

menu.getItems().add(newItem);

// add separator

menu.getItems().add(new SeparatorMenuItem());

// add non modal menu item

ToggleGroup modalGroup = new ToggleGroup();

RadioMenuItem nonModalItem = RadioMenuItemBuilder.create()

.toggleGroup(modalGroup)

.text("Non Modal")

.selected(true)

.build();

nonModalItem.setOnAction(new EventHandler() {

public void handle(ActionEvent event) {

LOGIN_DIALOG = createLoginDialog(primaryStage, false);

}

});

menu.getItems().add(nonModalItem);

// add modal selection

RadioMenuItem modalItem = RadioMenuItemBuilder.create()

.toggleGroup(modalGroup)

.text("Modal")

.selected(true)

.build();

modalItem.setOnAction(new EventHandler() {

public void handle(ActionEvent event) {

LOGIN_DIALOG = createLoginDialog(primaryStage, true);

}

});

menu.getItems().add(modalItem);

// add separator

menu.getItems().add(new SeparatorMenuItem());



64



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

1-16. Adding Tabs to the UI

Tải bản đầy đủ ngay(0 tr)

×