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 alignment
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.geometry.*;
import
javafx.scene.paint.*;
import
javafx.scene.canvas.*;
import
javafx.scene.text.*;
import
javafx.scene.Group;
import
javafx.scene.shape.*;
Â
Âpublic
class
Pos_1
extends
Application {
Â
ÂÂ Â Â Â
// launch the application
   Â
public
void
start(Stage stage)
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
try
{
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// set title for the stage
           Â
stage.setTitle(
"Pos"
);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// create a label
           Â
Label label =
new
Label(
"this is Pos example"
);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// create a Tile pane
           Â
TilePane tile_pane =
new
TilePane(label);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// create and add buttons to tilepane
           Â
for
(
int
i =
1
; i <=
7
; i++) {
               Â
tile_pane.getChildren().add(
new
Button(
"Button "
+ i));
           Â
}
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// set Alignment of pane
           Â
tile_pane.setAlignment(Pos.TOP_LEFT);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// create a scene
           Â
Scene scene =
new
Scene(tile_pane,
400
,
300
);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
// set the scene
           Â
stage.setScene(scene);
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
stage.show();
       Â
}
Â
ÂÂ Â Â Â Â Â Â Â
catch
(Exception e) {
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
System.out.println(e.getMessage());
       Â
}
   Â
}
Â
ÂÂ Â Â Â
// Main Method
   Â
public
static
void
main(String args[])
   Â
{
Â
ÂÂ Â Â Â Â Â Â Â
// launch the application
       Â
launch(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 Pos
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.*;
import
javafx.scene.layout.*;
import
javafx.stage.Stage;
import
javafx.geometry.*;
import
javafx.scene.paint.*;
import
javafx.scene.canvas.*;
import
javafx.scene.text.*;
import
javafx.scene.Group;
import
javafx.scene.shape.*;
import
javafx.event.ActionEvent;
import
javafx.event.EventHandler;
import
javafx.collections.*;
 Â
Âpublic
class
Pos_2
extends
Application {
 Â
Â// launch the application
public
void
start(Stage stage)
{
Â
ÂÂ Â Â Â
try
{
Â
ÂÂ Â Â Â Â Â Â Â
// set title for the stage
       Â
stage.setTitle(
"Pos Class"
);
Â
ÂÂ Â Â Â Â Â Â Â
// create a label
       Â
Label label =
new
Label(
"This is Pos Class Example"
);
Â
ÂÂ Â Â Â Â Â Â Â
// create a Tile pane
       Â
TilePane tile_pane =
new
TilePane(label);
Â
ÂÂ Â Â Â Â Â Â Â
// create and add buttons to tilepane
       Â
for
(
int
i =
1
; i <=
7
; i++) {
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
tile_pane.getChildren().add(
new
Button(
"Button "
+ i));
       Â
}
Â
ÂÂ Â Â Â Â Â Â Â
// set Alignment of pane
       Â
tile_pane.setAlignment(Pos.BASELINE_CENTER);
Â
ÂÂ Â Â Â Â Â Â Â
// pos names
       Â
String 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 box
       Â
ComboBox combo_box =Â
         Â
new
ComboBox(FXCollections.observableArrayList(pos_name));
Â
ÂÂ Â Â Â Â Â Â Â
// Create action event
       Â
EventHandler<ActionEvent> event =
       Â
new
EventHandler<ActionEvent>() {
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â
public
void
handle(ActionEvent e)
           Â
{
Â
ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
// set Alignement of the TilePane
               Â
tile_pane.setAlignment(Pos.valueOf(
                    Â
(String)combo_box.getValue()));
           Â
}
       Â
};
Â
ÂÂ Â Â Â Â Â Â Â
// Set on action
       Â
combo_box.setOnAction(event);
Â
ÂÂ Â Â Â Â Â Â Â
// create a VBox
       Â
VBox vbox =
new
VBox(
30
, combo_box, tile_pane);
Â
ÂÂ Â Â Â Â Â Â Â
// set Alignemnet
       Â
vbox.setAlignment(Pos.CENTER);
Â
ÂÂ Â Â Â Â Â Â Â
// create a scene
       Â
Scene scene =
new
Scene(vbox,
400
,
300
);
Â
ÂÂ Â Â Â Â Â Â Â
// set the scene
       Â
stage.setScene(scene);
Â
ÂÂ Â Â Â Â Â Â Â
stage.show();
   Â
}
Â
ÂÂ Â Â Â
catch
(Exception e) {
Â
ÂÂ Â Â Â Â Â Â Â
System.out.println(e.getMessage());
   Â
}
}
Â
Â// Main Method
public
static
void
main(String args[])
{
Â
ÂÂ Â Â Â
// launch the application
   Â
launch(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