Drop down menu behaving strange - java

I am trying to make a drop down menu in javaFX. I get the menu to display but they show as "..." instead of the name of the menu. The other problem is when i click the drop down menu it drops the option inside it and then right after pops it up again. The content of the drop down menu shows correctly.
This is where javaFX inits and shows the scene with it's content (Only the drop down menues).
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Memory");
BorderPane root = new BorderPane();
Scene scene = new Scene(root , HEIGHT, LENGHT);
canvas = new Canvas(HEIGHT, LENGHT);
menuBar = new GameMenu();
root.getChildren().add(menuBar.createMenu());
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}
This is Where I create the menu bar.
public class GameMenu {
MenuBar menuBar;
public GameMenu(){
menuBar = new MenuBar();
Menu menuFile = new Menu("File");
MenuItem optionStartGame = new MenuItem("New game");
MenuItem optionLoadGame = new MenuItem("Load Game");
menuFile.getItems().addAll(optionStartGame, optionLoadGame);
Menu menuEdit = new Menu("Edit");
Menu menuView = new Menu("View");
menuBar.getMenus().addAll(menuFile, menuEdit, menuView);
}
public MenuBar createMenu() {
return menuBar;
}
}

The only problem with your code is that you are using getChildren method on your root BorderPane to populate it.
You can add the MenuBar to the top of the BorderPane like:
root.setTop(menuBar.createMenu());

Related

Creating new Transparent Stage on Event JavaFX

I'm trying to create a new Stage when a button is pressed.
It works but the problem is that I'd like this Stage to be fully transparent and lets us see what's behind the screen.
Code
Dimension Sizescreen = Toolkit.getDefaultToolkit().getScreenSize();
//Main stage with option menu
Pane window = new Pane();
Scene scene = new Scene(window);
stage.setTitle("Notification Extender");
//Create the button SetLooker
Button SetLooker = new Button("Set Looker");
//Add a Event when pressed
SetLooker.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent e) {
//Create a sub-Stage
Pane subwindow = new Pane();
Scene subscene = new Scene(subwindow);
Stage substage = new Stage();
substage.setTitle("Notification Extender");
//Set this subStage Transparent
substage.initStyle(StageStyle.TRANSPARENT);
subscene.setFill(Color.TRANSPARENT);
substage.setWidth(Sizescreen.getWidth());
substage.setHeight(Sizescreen.getHeight());
substage.setX(0);
substage.setY(0);
//Create a a graphique element
Rectangle redrec = new Rectangle(120,40,50,50);
redrec.setStroke(Color.RED);
redrec.setStrokeWidth(2);
redrec.setFill(Color.TRANSPARENT);
//Add the graphique element to the sub-stage
subwindow.getChildren().add(redrec);
//Show the sub-stage
substage.setScene(subscene);
substage.show();
}
});
//Add the button to the main stage
window.getChildren().add(SetLooker);
//Show the main stage
stage.setScene(scene);
stage.show();
The problem is that when I press the button it shows the stage but it's not transparent at all it's completely white.
I've also tried to change the main Stage, but I cannot change it once it has been shown.
You also need to remove the background from the root of your new scene:
subwindow.setBackground(null);

how to lock the size of a Stage in java

I made a very simple java application with 3 scenes and 3 buttons.
button3 (b3) opens a popup but I set the image to lock size but the window can still be changed by using the mouse on one of the corners.
is there a way to lock the size so the curson can't edit this?
Here's the simplified code (just to get and idea)
public class Handler extends Application
{
#Override
public void start(Stage primaryStage) throws Exception {
Button button1 = new Button("AD");
VBox layout1 = new VBox(button1);
layout2.setAlignment(Pos.CENTER);
Pane layout2 = new Pane();
layout3.setStyle("-fx-background-image: url(ad.jpg);" +
"-fx-background-repeat: no-repeat;" +
"-fx-background-size: initial;");
Scene scene1 = new Scene(layout1);
Scene scene2 = new Scene(layout2);
//Stage (window)
primaryStage.setTitle("Basic Handler");
primaryStage.setScene(scene1);
primaryStage.setHeight(400);
primaryStage.setWidth(400);
primaryStage.show();
button1.setOnAction(event -> {
Stage PopUp = new Stage();
PopUp.setHeight(266);
PopUp.setWidth(474);
PopUp.setTitle("Sponsored AD");
PopUp.setScene(scene2);
PopUp.showAndWait();
});
}
}
Just add this in your stage :
PopUp.setResizable(false);

Add a MenuItem to multiple Menus

I'm currently working on a MenuBar for a library(for books) in JavaFX8. I would like to add a MenuItem to several menus. If I do that, the output is a warning saying that this MenuItem has already been added and in the Application the Book Menu is not shown.
Code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
public void start(Stage stage) {
//Layout: BorderPane
BorderPane layout = new BorderPane();
//Define what is where
layout.setTop(addMenuBar());
//make it visible
Scene scene = new Scene(layout, 900, 600);
stage.setScene(scene);
stage.setTitle("Library");
stage.show();
}
public MenuBar addMenuBar() {
//create the menubar
MenuBar menu = new MenuBar();
//create the menus
Menu fileMenu = new Menu("File");
Menu bookMenu = new Menu("Book");
Menu manageMenu = new Menu("Manage");
Menu sortMenu = new Menu("Sort");
//create the menu items
MenuItem exitItem = new MenuItem("Exit");
MenuItem booksAlphabetical = new MenuItem("Books alphabetical");
MenuItem encAlphabetical = new MenuItem("Encyclopedias alphabetical");
MenuItem addBook = new MenuItem("Add book");
MenuItem addCategory = new MenuItem("Add category");
MenuItem addPublisher = new MenuItem("Add Publisher");
MenuItem booksReview = new MenuItem("Books sorted by review");
//make the menus ready
//file menu
fileMenu.getItems().add(exitItem);
//bookMenu
bookMenu.getItems().addAll(addBook, booksAlphabetical,
encAlphabetical, booksReview);
//manageMenu
manageMenu.getItems().addAll(addBook, addCategory, addPublisher);
//sortMenu
sortMenu.getItems().addAll(booksAlphabetical, encAlphabetical, booksReview);
//add menus to the menubar
menu.getMenus().addAll(fileMenu, bookMenu, manageMenu, sortMenu);
//return the menubar
return menu;
}
}
Thanks in advance!
Simply said, it happens because you put a node to the two different parents (see the booksAlphabetical used twice):
bookMenu.getItems().addAll(addBook, booksAlphabetical, encAlphabetical, booksReview);
sortMenu.getItems().addAll(booksAlphabetical, encAlphabetical, booksReview);
It's not known where the MenuItems booksAlphabetical, encAlphabetical, booksReview should appear, so they will be added only to the last one sortMenu.
Why does it happen? The JavaFX API is built that each Node has a collection of properties, including a parent property, that is only and only one.
The solution is to create them for the second time to make them unique since you want to use them twice.
MenuItem booksAlphabeticalSort = new MenuItem("Books alphabetical");
MenuItem encAlphabeticalSort = new MenuItem("Encyclopedias alphabetical");
MenuItem booksReviewSort = new MenuItem("Books sorted by review");
sortMenu.getItems().addAll(booksAlphabeticalSort, encAlphabeticalSort, booksReviewSort);

JavaFX: MenuBar showing "..."

So: i have here a piece of code that I am using to add a menu to my application.
Stage window;
private void buildWindow() {
window = new Stage();
window.setTitle("FACE");
//MENU
MenuBar mBLaunch = new MenuBar();
Menu fileLaunch = new Menu("File");
MenuItem saveLaunch = new MenuItem("Save");
MenuItem exitLaunch = new MenuItem("Exit");
fileLaunch.getItems().addAll(saveLaunch, new SeparatorMenuItem(), exitLaunch);
mBLaunch.getMenus().add(fileLaunch);
//HouseKeeping
BorderPane bPLaunch = new BorderPane();
bPLaunch.getChildren().addAll(mBLaunch);
Scene launch = new Scene(bPLaunch);
window.setScene(launch);
window.setMinHeight(500);
window.setMinWidth(500);
window.show();
}
However: when I run this code it produces:
So, my question being, how can I make it display something else than ...?
Thanks for the help in advance.
BorderPane places each child at a specific location: center, top, bottom, left, right. If you just add nodes to its list of children, it doesn’t know where to place them.
Replace this:
bPLaunch.getChildren().addAll(mBLaunch);
with this:
bPLaunch.setTop(mBLaunch);

How to Achieve One Menubar for more then one scenes in javaFx

I am basically new to Java FX 2.
Scenario:
I have 3 Scenes and I want a way to add menu-bar such that I don't i don't want to explicitly remove the menu bar from previous scene and add it to new one. Like Some thing a Parent Scene or some way menu-bar is attached to Stage. I mean menu-bar is added just one time and always be present whatever scene is in front or not.
If This is Possible How Can I do this.
Here is the Default Example Provided by Oracle Docs of JavaFX http://docs.oracle.com/javafx/2/ui_controls/MenuSample.java.html
public class Main extends Application {
final ImageView pic = new ImageView();
final Label name = new Label();
final Label binName = new Label();
final Label description = new Label();
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
stage.setTitle("Menu Sample");
Scene scene = new Scene(new VBox(), 400, 350);
scene.setFill(Color.OLDLACE);
MenuBar menuBar = new MenuBar();
// --- Graphical elements
final VBox vbox = new VBox();
vbox.setAlignment(Pos.CENTER);
vbox.setSpacing(10);
vbox.setPadding(new Insets(0, 10, 0, 10));
makeContentsForVBox();// in this vBox area will be fill with name pic desrciption
vbox.getChildren().addAll(name, binName, pic, description); // name is lable
// --- Menu File
Menu menuFile = new Menu("File");
MenuItem add = new MenuItem("Shuffle",
new ImageView(new Image(getClass().getResourceAsStream("new.png"))));
add.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
shuffle();
vbox.setVisible(true);
}
});
MenuItem clear = new MenuItem("Clear");
clear.setAccelerator(KeyCombination.keyCombination("Ctrl+X"));
clear.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
vbox.setVisible(false);
}
});
MenuItem exit = new MenuItem("Exit");
exit.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent t) {
System.exit(0);
}
});
menuFile.getItems().addAll(add, clear, new SeparatorMenuItem(), exit);
((VBox) scene.getRoot()).getChildren().addAll(menuBar, vbox);
stage.setScene(scene);
stage.show();
}
}
So Here menuBar is added to a scene. if i swap the scene and bring an other scene in front ... What will i do. i think I remove menuBar from this scene and add to other or simply add to new one. so every time i have to do this when i change. Is there any way to avoid this??
The approach I would prefer is to use a Scene with BorderPane as its root
scene.setRoot(borderPane);
You can add the MenuBar to the top of the BorderPane and at its Center you can place SplitPane
BorderPane borderPane = new BorderPane();
borderPane.setTop(menuBar);
borderPane.setCenter(splitPane);
Whenever you need to switch to WebView just replace it with SplitPane :
borderPane.setCenter(webView);
Following this approach, your MenuBar will always remain on TOP and you can switch between SplitPane and WebView

Categories

Resources