ToolBar
In JavaFX, a ToolBar is a horizontal or vertical container that holds buttons, controls, or other nodes, usually used for app toolbars, like those in editors or browsers.
- Typically contains shortcuts, actions, or tools.
- Can contain
Button,ToggleButton,Label,ComboBox, etc. - Can be oriented horizontally or vertically.
Basic Example
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ToolBar;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class ToolBarExample extends Application {
@Override
public void start(Stage stage) {
// Create toolbar buttons
Button btnNew = new Button("New");
Button btnOpen = new Button("Open");
Button btnSave = new Button("Save");
// Create ToolBar
ToolBar toolBar = new ToolBar(btnNew, btnOpen, btnSave);
// Add ToolBar to top of BorderPane
BorderPane root = new BorderPane();
root.setTop(toolBar);
Scene scene = new Scene(root, 400, 200);
stage.setTitle("ToolBar Example");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
Result:
- A horizontal toolbar appears at the top with buttons
New,Open,Save.
Vertical Toolbar
ToolBar verticalToolBar = new ToolBar(btnNew, btnOpen, btnSave);
verticalToolBar.setOrientation(Orientation.VERTICAL);
- Now the buttons are stacked vertically.
Key Methods & Properties
toolBar.getItems(); // get all items in the toolbar
toolBar.setOrientation(Orientation.HORIZONTAL/Orientation.VERTICAL);
- Can add any Node, not just buttons:
ComboBox,Label,Separator,ToggleButton… - Use Separator to group buttons visually:
toolBar.getItems().add(new Separator());
FXML Version
<ToolBar xmlns:fx="http://javafx.com/fxml">
<items>
<Button text="New"/>
<Button text="Open"/>
<Button text="Save"/>
<Separator/>
<Button text="Exit"/>
</items>
</ToolBar>
Use Cases
- Editor toolbar (New, Open, Save, Undo, Redo)
- Image editing app (Brush, Eraser, Color picker)
- File manager shortcuts (Copy, Paste, Delete)
- Any action bar for quick access
💡 Tip: Combine ToolBar with BorderPane:
- Top → main app toolbar
- Left → vertical navigation toolbar
This is how real desktop apps structure their interface.