JavaFX | Pos Class

Pos class is a part of JavaFX. Pos class contains values which state the horizontal and vertical positioning or alignment. Pos class inherits Enum class.
Commonly Used Methods:
| Method | Explanation |
|---|---|
| getHpos() | Returns the horizontal alignment. |
| getVpos() | Returns the vertical alignment. |
| valueOf(String n) | Returns the Pos object whose name is the string specified. |
| values() | Returns an array which contains all the Pos values. |
Below programs illustrate the use of Pos Class:
- Java Program to create a tilepane and add a specified Pos value as its alignment: In this program we will create a TilePane named tile_pane. Add Label named label and some buttons to the tile_pane. Set the Alignment of the tile_pane using the setAlignment() function. Set the alignment of the tile_pane to the Pos value TOP_LEFT. Add the tile_pane to the scene and add the scene to the stage and call the show() function to display the final results.
// Java Program to create a tilepane and add// a specified Pos value as its alignmentimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.geometry.*;importjavafx.scene.paint.*;importjavafx.scene.canvas.*;importjavafx.scene.text.*;importjavafx.scene.Group;importjavafx.scene.shape.*;publicclassPos_1extendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("Pos");// create a labelLabel label =newLabel("this is Pos example");// create a Tile paneTilePane tile_pane =newTilePane(label);// create and add buttons to tilepanefor(inti =1; i <=7; i++) {tile_pane.getChildren().add(newButton("Button "+ i));}// set Alignment of panetile_pane.setAlignment(Pos.TOP_LEFT);// create a sceneScene scene =newScene(tile_pane,400,300);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:
- Java Program to create a TilePane and a combobox that contains different values of Pos: In this program we will create a TilePane named tile_pane. Add Label named label and some buttons to the tile_pane. Set the Alignment of the tile_pane using the setAlignment() function. We will set the alignment of the tile_pane to the Pos value BASELINE_CENTER. Store all the names of Pos values in a String array. Now create a combobox which will contain the names of Pos values and also cranate a Action Event to handle the combobox events. The Event handler will set the Alignment of the tilepane to the chosen pos value. Now create a VBox and add the tilepane and the combo box to vbox. Finally, add the vbox to the scene and add the scene to the stage and call the show() function to display the final results.
// Java Program to create a TilePane and// a combobox that contains different// values of Posimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.stage.Stage;importjavafx.geometry.*;importjavafx.scene.paint.*;importjavafx.scene.canvas.*;importjavafx.scene.text.*;importjavafx.scene.Group;importjavafx.scene.shape.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.collections.*;publicclassPos_2extendsApplication {// launch the applicationpublicvoidstart(Stage stage){try{// set title for the stagestage.setTitle("Pos Class");// create a labelLabel label =newLabel("This is Pos Class Example");// create a Tile paneTilePane tile_pane =newTilePane(label);// create and add buttons to tilepanefor(inti =1; i <=7; i++) {tile_pane.getChildren().add(newButton("Button "+ i));}// set Alignment of panetile_pane.setAlignment(Pos.BASELINE_CENTER);// pos namesString pos_name[] = {"BASELINE_CENTER","BASELINE_LEFT","BASELINE_RIGHT","BOTTOM_CENTER","BOTTOM_LEFT","BOTTOM_RIGHT","CENTER","CENTER_LEFT","CENTER_RIGHT","TOP_CENTER","TOP_LEFT","TOP_RIGHT"};// Create a combo boxComboBox combo_box =newComboBox(FXCollections.observableArrayList(pos_name));// Create action eventEventHandler<ActionEvent> event =newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent e){// set Alignement of the TilePanetile_pane.setAlignment(Pos.valueOf((String)combo_box.getValue()));}};// Set on actioncombo_box.setOnAction(event);// create a VBoxVBox vbox =newVBox(30, combo_box, tile_pane);// set Alignemnetvbox.setAlignment(Pos.CENTER);// create a sceneScene scene =newScene(vbox,400,300);// set the scenestage.setScene(scene);stage.show();}catch(Exception e) {System.out.println(e.getMessage());}}// Main Methodpublicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:
Note: The above programs might not run in an online IDE please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/geometry/Pos.html




