JavaFX: MenuBar showing "..." - java

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);

Related

How to create scrollable "drop down" of check boxes in Java FX?

I am new to JavaFX and I'm trying to create a "dropdown list" of check items. I am trying to make the drop down scrollable. I can easily do this with a ComboBox (using setVisibleowCount(int)), but ComboBox only allows for 1 item to be chosen before closing the dialogue and doesn't seem to be the right object to use.
I am currently using a menu button with CheckMenuItems. ListView seems like it could be useful, but I'm not quite sure how to integrate that. If anyone can help that'd be great. Thanks.
Current Status
Since you cannot use a CheckComboBox I would see if an Accordion + TitledPane fits with what your doing.
Here is an example:
public class Main extends Application {
#Override
public void start(Stage stage) throws Exception{
VBox root = new VBox();
root.getChildren().add(new Label("Select Number of Checkboxes you feel like clicking"));
VBox vBox = new VBox();
for (int i = 0; i < 5; i++)
vBox.getChildren().add(new CheckBox("i:" + i));
ScrollPane scrollPane = new ScrollPane(vBox);
//Easily changeable Max Height
scrollPane.setMaxHeight(10);
// Create TitledPane.
TitledPane titledPane = new TitledPane("Check Boxes", scrollPane);
//Add to Accordion
Accordion accordion = new Accordion(titledPane);
//Add to root VBox
root.getChildren().add(accordion);
root.getChildren().add(new Label("Some Other Content"));
stage = new Stage();
stage.setHeight(200);
stage.setScene(new Scene(root));
stage.setAlwaysOnTop(true);
stage.show();
}
public static void main(String[] args) { launch(args); }
}

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);

chart and Button in javafx doesn't render

I want to render an array of buttons and then a piechart to the screen. I've tried almost every method I could but something doesn't seems to work. either alone array of buttons(usercontrol()) or pie(the graph) can be render but when I try to do both it only render the array of buttons.plz don't worry about return types of function. any help will be really appreciated.
public class Layout {
// returns Windows height and width
private final double width = 600;
private final double height = 400;
private Button[] userControl() { // navigation bar buttons
Button[] buttons = new Button[3];
buttons[0] = new Button("BUY Share!"); // Buy shares buttons
buttons[0].setLayoutX(width - 100);
buttons[0].setLayoutY(10);
buttons[1] = new Button("Sell Shares!"); // Sell shares buttons
buttons[1].setLayoutX(width - 200);
buttons[1].setLayoutY(10);
buttons[2] = new Button("Show Share"); // Show shares buttons
buttons[2].setLayoutX(width - 300);
buttons[2].setLayoutY(10);
return buttons;
}
public void pie() {
ObservableList<PieChart.Data> shareHolders
= FXCollections.observableArrayList(
new PieChart.Data("user1", 13),
new PieChart.Data("user2", 25),
new PieChart.Data("user3", 10),
new PieChart.Data("user4", 22),
new PieChart.Data("user5", 30));
PieChart chart = new PieChart(shareHolders);
chart.setTitle("Share Holders Shares");
VBox pie = new VBox();
pie.setLayoutY(100);
pie.getChildren().addAll(chart);
pane().getChildren().add(pie);
// return pie;
}
private Pane pane() {
Pane pane = new Pane();
pane.getChildren().addAll(userControl());
return pane;
}
public Stage window() {
//pane().getChildren().add();
pie();
Scene scene = new Scene(pane(), 600, 400);
Stage primaryStage = new Stage();
primaryStage.setScene(scene);
primaryStage.setTitle("ShareHolders!");
primaryStage.show();
return primaryStage;
}
}
Your problem is that you are creating a new Pane every time you call the pane method. You need to change it, perhaps by using a global Pane object.
//First, declare a global Pane.
static Pane pane = new Pane();
//Make your pie() method return the pie VBox.
public VBox pie() {
/*Blah blah blah, making the pie...*/
return pie//Remember, pie is a VBox, which is why we are returning the VBox.
}
//Later, when you build your window, add the pie and the buttons to the GLOBAL PANE...
public Stage window() {
pane.getChildren().add(pie()); //...right here.
pane.getChildren().addAll(userControl());
/*Build the primary stage...*/
return primaryStage;
}
This should get you your desired result.

Drop down menu behaving strange

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());

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