JavaFX ChoiceBox in a MenuBar - java

I make an application with JavaFX.
I want to have a ChoiceBox in my top MenuBar to select the language.
I want to disable the blue surrounding when the language control is selected. How do I do this?

Set the Id of the Menu that contains the ChoiceBox to transparent and use the following CSS:
#transparent:hover,
#transparent:focused,
#transparent:showing {
-fx-background: transparent;
}
Unfortunately I couldn't figure out how to do this with transparent as a class yet.
Example
Output:
Code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Foo extends Application {
#Override
public void start(Stage stage) throws Exception {
Menu fileMenu = new Menu("File");
ChoiceBox<String> languageBox = new ChoiceBox<String>();
languageBox.getItems().addAll("English", "Deutsch");
Menu languageMenu = new Menu();
languageMenu.setId("transparent");
languageMenu.setGraphic(languageBox);
MenuBar menuBar = new MenuBar();
menuBar.getMenus().addAll(fileMenu, languageMenu);
BorderPane root = new BorderPane();
root.setTop(menuBar);
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
stage.setScene(scene);
stage.setTitle("MCVE");
stage.setWidth(640);
stage.setHeight(480);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Related

JavaFX: Set gap between graphic node and text in MenuItem

What is the way to set a gap between the text and graphic of a MenuItem.
JavaFX Labeled subclasses have a setGraphicTextGap(double value) method that does exactly what I'm looking for, but MenuItem class doesn't inherit from Labeled.
Sample code:
ImageView newFilterIcon = new ImageView(new Image(newFilterIconUrl));
MenuItem newFilterMenuItem = new MenuItem("New Filter", newFilterIcon);
Menu filterMenu = new Menu("Filter");
filterMenu.getItems().add(newFilterMenuItem);
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.image.ImageView;
import javafx.stage.Stage;
public class MenuGapApp extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) throws Exception {
ImageView imageView = new ImageView("https://image.shutterstock.com/image-vector/home-flat-icon-you-can-260nw-451922449.jpg");
MenuItem menuItem = new MenuItem("New filter", imageView);
Menu menu = new Menu("Filter", null, menuItem);
MenuBar menuBar = new MenuBar(menu);
Scene scene = new Scene(menuBar);
scene.getStylesheets().add(getClass().getResource("/menu-gap.css").toExternalForm());
stage.setScene(scene);
stage.show();
}
}
menu-gap.css
.menu-item > .graphic-container {
-fx-padding: 0em 10em 0em 0em;
}

Is is possible to separate javafx ui files?

I'm creating an app with javafx and I want to separate ui files.
Like: Tabs in separate file and just import and create new Tab in main app.
I have tried it, but when I try to set it on main app like borderPane.setCenter(tabPane); it says that The method setCenter(Node) in the type BorderPane is not applicable for the arguments (Tabs)
Main.java app code
import javafx.application.Application;
import javafx.scene.layout.*;
import javafx.scene.paint.*;
import javafx.scene.*;
import javafx.stage.Stage;
import utils.Tabs;
public class Main extends Application {
#Override
public void start(Stage window) throws Exception {
Group root = new Group();
Scene scene = new Scene(root, 400, 250, Color.WHITE);
BorderPane borderPane = new BorderPane();
Tabs tabs = new Tabs();
// bind to take available space
borderPane.prefHeightProperty().bind(scene.heightProperty());
borderPane.prefWidthProperty().bind(scene.widthProperty());
borderPane.setCenter(tabs);
root.getChildren().add(borderPane);
window.setTitle("Computer Security — Demos");
window.setScene(scene);
window.show();
}
public static void main(String[] args) {
launch(args);
}
}
Tabs.java
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.HBox;
public class Tabs extends TabPane {
public Tabs() {
TabPane tabPane = new TabPane();
Tab tab = new Tab("Tab: ");
HBox hbox = new HBox();
hbox.getChildren().add(new Label("Tab"));
hbox.setAlignment(Pos.CENTER);
tab.setContent(hbox);
tabPane.getTabs().add(tab);
}
}

Label will not show on GUI

I have a GUI, however, when I try to add a Label the screen it does not seem to add correctly.
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.stage.Stage;
public class GUI extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
Label labe = new Label("Test");
Button button = new Button("ASDf");
TilePane tilePane = new TilePane();
tilePane.getChildren().addAll(labe, button);
Scene scene = new Scene(tilePane, 100, 100);
primaryStage.setScene(scene);
primaryStage.show();
}
}
I get the following result When I run the above code
I am running this in IntelliJ with java version 1.7
How do I get my label to show also?

JavaFX/FXML is it possible to show a Hbox/VBox after clicking a MenuButton

I recently started coding in FXML/JavaFX using Eclipse and one of the projects I'm working on requires me to make a drop down menu with combobox, checkboxes etc.. So my question is would it be possible to make the MenuButton display a VBox/HBox when clicked with those inside?
Here is a one of the simplest example of menu:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.stage.Stage;
public class MenuFX extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
Group group = new Group();
Scene scene = new Scene(group, 800, 600);
MenuBar menuBar = new MenuBar();
Menu someValues = new Menu("Values");
for (int i = 0; i < 60; i++) {
MenuItem item = new MenuItem("Value " + i);
someValues.getItems().add(item);
}
menuBar.getMenus().add(someValues);
group.getChildren().addAll(menuBar);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
More expanding example here.

Why doesn't button add to scene?

package application;
import java.awt.Button;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
public class Main extends Application {
Button button; //Declare Button
public static void main(String[] args) {
launch(args); //calls application
}
#Override
public void start(Stage primaryStage) throws Exception {
//comes from application pack
primaryStage.setTitle("Title Of Window"); //Main Stage
button = new Button (); //Creates Button
button.setLabel("Click Me");
StackPane layout = new StackPane();
layout.getChildren().add(button); //Button in Scene
Scene scene = new Scene (layout, 300, 250); //Sets Scene
primaryStage.setScene(scene); //Stage and Scene
primaryStage.show();
}
Hey guys, so this is first time I am creating something in JavaFX and I was wondering why doesn't my button in Scene add in Layout.GetChildren() line, it keeps on displaying red line underneath add. I am using Eclipse IDE.
you have imported import java.awt.Button, you must import Button class in the javafx package.

Categories

Resources