JavaFx .jar file not running correctly - java

Im developing an application using javafx, and it works fine when running from net beans, but when i try to run the application through the .jar file,the application starts and i´m able to use some of its functionalities, but not all of them.
When it starts always gives me this error:
This is the a part code :
#Override
public void start(Stage primaryStage)
{
bd=new BD();
bd.iniciarConexao();
stage= primaryStage;
stage.setTitle("Gestão de eventos - AAC");
layerPane = new StackPane();
initRoot();
scene = new Scene(layerPane, 1020, 700);
scene.getStylesheets().addAll(ProjectV2.class.getResource("ProjectV2.css").toExternalForm(),
ProjectV2.class.getResource("calendarstyle.css").toExternalForm());
//MainToolBar-----------------------------------------------------------
nomeAplicacao = new Label("Gestão de eventos - AAC");
hbNomeAplicacao= new HBox();
hbEmblemas = new HBox(40);
emblemas = new ArrayList<>();
toolBar = new ToolBar();
logo = new ImageView(new Image(ProjectV2.class.getResourceAsStream("images/gestaoDeEventos2_2.png")));
//----------------------------------------------------------------------
spacer1= new Region();
spacer2= new Region();
spacer3= new Region();
spacer4= new Region();
centerPane= new BorderPane();
paScrollPane= new ScrollPane();
paVb= new VBox();
paVb.setPrefHeight(500);
hbSubtitulo= new HBox();
subtitulo= new Label("Funcionalidades");
//----------------------------------------------------------------------
//LogToolBar------------------------------------------------------------
logToolBar= new ToolBar();
loginButton = new MenuButton();
loginButton.setId("SettingsButton");
loginButton.setPopupSide(Side.TOP);
loginButton.setGraphic(new ImageView(new Image(ProjectV2.class.getResourceAsStream("images/business_user2.png"))));
mLogin = MenuItemBuilder.create().text("Login").build();
mAddFunc = MenuItemBuilder.create().text("Mudar password").build();
mLogout = MenuItemBuilder.create().text("Logout").build();
//mEditUser = MenuItemBuilder.create().text("Editar funcionário").build();
mFactura= MenuItemBuilder.create().text("Factura").build();
//----------------------------------------------------------------------
stage.setScene(scene);
stage.show();
toogleMaximized();
}
public void toogleMaximized()
{
screen = Screen.getScreensForRectangle(stage.getX(), stage.getY(), 1, 1).get(0);
if (maximized)
{
maximized = false;
if (backupWindowBounds != null) {
stage.setX(backupWindowBounds.getMinX());
stage.setY(backupWindowBounds.getMinY());
stage.setWidth(backupWindowBounds.getWidth());
stage.setHeight(backupWindowBounds.getHeight());
}
}
else
{
maximized = true;
backupWindowBounds = new Rectangle2D(stage.getX(), stage.getY(), stage.getWidth(), stage.getHeight());
stage.setX(screen.getVisualBounds().getMinX());
stage.setY(screen.getVisualBounds().getMinY());
stage.setWidth(screen.getVisualBounds().getWidth());
stage.setHeight(screen.getVisualBounds().getHeight());
}
}

Related

How to change between wireframe and solid in javafx

3d software allow user to change draw mode dinamically. It can be implemented on javafx ?
Changing draw mode with radio buttons
In this approach a Box instance change its DrawMode with radiobuttons.
This is a single class javafx you can try .
App.java
public class App extends Application {
#Override
public void start(Stage stage) {
var perspective = new PerspectiveCamera(true);
perspective.setNearClip(0.1);
perspective.setFarClip(500);
perspective.setTranslateZ(-150);
Shape3D cube = new Box(50, 50, 50);
cube.setCullFace(CullFace.NONE);
cube.setMaterial(new PhongMaterial(Color.CORAL));
var toggleGroup = new ToggleGroup();
var solid = new RadioButton("solid");
solid.setToggleGroup(toggleGroup);
solid.setSelected(true);
var wire = new RadioButton("wireframe");
wire.setToggleGroup(toggleGroup);
var hBox = new HBox(solid, wire);
toggleGroup.selectedToggleProperty().addListener((o) -> {
Toggle selectedToggle = toggleGroup.getSelectedToggle();
if (selectedToggle == solid) {
cube.setDrawMode(DrawMode.FILL);
}
if (selectedToggle == wire) {
cube.setDrawMode(DrawMode.LINE);
}
});
var group3d = new Group(perspective, cube);
var subscene = new SubScene(group3d, 300, 400, true, SceneAntialiasing.BALANCED);
subscene.setCamera(perspective);
var stack = new StackPane(subscene, hBox);
stage.setScene(new Scene(stack, 300, 400));
stage.show();
}
public static void main(String[] args) {
launch();
}
}

JavaFx VBox children not growing past a certain size

I been trying to create a desktop app with javafx and have been hardstuck trying to figure out why the GridPane and HBox in the VBox have been refusing to expand past a certain point in the vertical direction. I have set their .vgrow properties to Priority.ALWAYS and they still seem to not want to expand when I resize the window.
The code is shown below:
package display;
import java.io.File;
import javafx.beans.property.SimpleStringProperty;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
import javafx.event.Event;
import javafx.event.EventHandler;
public class OxygenDisplay
{
private String openFileName;
private String fileContents;
private String activeVaultPath;
private final Stage primaryStage;
private final Scene primaryScene;
private final MenuBar topMenu;
private final GridPane middleStruct;
private final HBox bottomOptions;
public OxygenDisplay(String fileName, String fileContents, String path)
{
this.openFileName = fileName;
this.fileContents = fileContents;
this.activeVaultPath = path;
// Create node structure for menu bar
this.topMenu = this.generateTopMenuBar();
// Create node structure for vault display and text editor
this.middleStruct = this.generateMiddleStructure();
// Create node structure for lower options bar
this.bottomOptions = this.generateBottomOptions();
// Post view to user
VBox root = new VBox();
for (Node child: root.getChildren())
{
VBox.setVgrow(child, Priority.ALWAYS);
}
root.getChildren().addAll(
this.topMenu,
this.middleStruct,
this.bottomOptions
);
this.primaryScene = new Scene(root, 400, 300);
this.primaryStage = new Stage();
// Init all listeners
this.initListeners();
this.primaryStage.setTitle("Oxygen");
this.primaryStage.setScene(primaryScene);
this.primaryStage.show();
}
private void initListeners()
{
// Add window height and width listeners
this.primaryStage.widthProperty().addListener((obsv, oldv, newv) ->
{
// this.mdEditor.prefWidth(newv.doubleValue());
// this.mdEditor.maxWidth(newv.doubleValue());
});
this.primaryStage.heightProperty().addListener((obsv, oldv, newv) ->
{
// this.mdEditor.prefHeight(newv.doubleValue());
// this.mdEditor.maxHeight(newv.doubleValue());
});
}
public String getOpenFileName()
{
return this.openFileName;
}
public void setOpenFileName(String file)
{
this.openFileName = file;
}
public String getFileContents()
{
return this.fileContents;
}
public void setFileContents(String data)
{
this.fileContents = data;
}
private MenuBar generateTopMenuBar()
{
MenuBar topBar = new MenuBar();
Menu file = new Menu("File");
Menu edit = new Menu("Edit");
Menu view = new Menu("View");
Menu help = new Menu("Help");
SeparatorMenuItem fileSeparator = new SeparatorMenuItem();
SeparatorMenuItem editSeparator = new SeparatorMenuItem();
// TODO: Add event handlers for these options
MenuItem open = new MenuItem("Open");
MenuItem newNote = new MenuItem("New Note");
MenuItem newVault = new MenuItem("New Vault");
MenuItem save = new MenuItem("Save");
MenuItem saveAs = new MenuItem("Save as");
file.getItems().addAll(
open,
newNote,
newVault,
fileSeparator,
save,
saveAs
);
// TODO: Add event handlers for these options
MenuItem copy = new MenuItem("Copy");
MenuItem paste = new MenuItem("Paste");
MenuItem cut = new MenuItem("Cut");
MenuItem find = new MenuItem("Find");
edit.getItems().addAll(
cut,
copy,
paste,
editSeparator,
find
);
// TODO: Add event handlers for these options
MenuItem appearance = new MenuItem("Appearance");
view.getItems().addAll(
appearance
);
// TODO: Add event handlers for these options
MenuItem about = new MenuItem("About");
help.getItems().addAll(
about
);
topBar.getMenus().addAll(
file,
edit,
view,
help
);
return topBar;
}
private void generateFileStructure(File node, TreeItem<String> root)
{
// TODO: Order the tree structure so that files are always before
// directories. Currently it is based on file names.
if (node.isDirectory())
{
TreeItem<String> item = new TreeItem<>(node.getName());
root.getChildren().add(item);
for (File f: node.listFiles())
{
TreeItem<String> temp = new TreeItem<>();
item.getChildren().add(temp);
// When parent is expanded continue the recursion
item.addEventHandler(TreeItem.branchExpandedEvent(),
new EventHandler() {
#Override
public void handle(Event event)
{
generateFileStructure(f, item);
item.getChildren().remove(temp);
item.removeEventHandler(TreeItem.branchExpandedEvent(),
this);
}
});
}
} else
{
root.getChildren().add(new TreeItem<>(node.getName()));
}
}
private TreeView<String> generateVaultStructure()
{
File fileInputDirLocation = new File(this.activeVaultPath);
File[] fileList = fileInputDirLocation.listFiles();
TreeView<String> vaultStruct = new TreeView<>();
TreeItem<String> root =
new TreeItem<>(fileInputDirLocation.getAbsoluteFile().getName());
// create tree
assert fileList != null;
for (File file: fileList)
{
this.generateFileStructure(file, root);
}
vaultStruct.setRoot(root);
return vaultStruct;
}
private GridPane generateMiddleStructure()
{
GridPane gridPane = new GridPane();
TreeView<String> vaultStruct = this.generateVaultStructure();
TextArea mdEditor = new TextArea();
mdEditor.setText(this.fileContents);
gridPane.add(vaultStruct, 0, 0);
gridPane.add(mdEditor, 1, 0);
// For resizing purposes
GridPane.setHgrow(vaultStruct, Priority.NEVER);
GridPane.setHgrow(mdEditor, Priority.ALWAYS);
GridPane.setVgrow(vaultStruct, Priority.ALWAYS);
GridPane.setVgrow(mdEditor, Priority.ALWAYS);
return gridPane;
}
private HBox generateBottomOptions()
{
HBox bottomOptions = new HBox();
Label openFileDisplay = new Label("");
openFileDisplay.textProperty().bind(
new SimpleStringProperty(this.openFileName)
);
bottomOptions.getChildren().addAll(
openFileDisplay
);
// For resizing purposes
HBox.setHgrow(bottomOptions, Priority.ALWAYS);
return bottomOptions;
}
}
Example image:
The red area is what I need to remove
Any help is appreciated in finding out why this is occuring? Let me know if anyone wants to run the above code as it requires a seperate class that extends Application for javafx.
The fix was the order of the for loop which gave all children process Priority.ALWAYS:
Old:
// Post view to user
VBox root = new VBox();
for (Node child: root.getChildren())
{
VBox.setVgrow(child, Priority.ALWAYS);
}
root.getChildren().addAll(
this.topMenu,
this.middleStruct,
this.bottomOptions
);
New:
// Post view to user
VBox root = new VBox();
root.setMaxHeight(Double.MAX_VALUE);
root.getChildren().addAll(
this.topMenu,
this.middleStruct,
this.bottomOptions
);
for (Node child: root.getChildren())
{
VBox.setVgrow(child, Priority.ALWAYS);
}
Although now the GridPane and Label/HBox expand together due to both of the panes having Priority.ALWAYS. Hence, to overcome this is to give GridPane maximum priority.
New Version 2:
// Post view to user
VBox root = new VBox();
root.setMaxHeight(Double.MAX_VALUE);
root.getChildren().addAll(
this.topMenu,
this.middleStruct,
this.bottomOptions
);
VBox.setVgrow(this.middleStruct, Priority.ALWAYS);
VBox.setVgrow(this.bottomOptions, Priority.NEVER);
Cheers.

How do I get the selected string from a combobox?

This is my main class. I'm trying to get the string that is being selected from the combobox and "get the wall color" from that string. The problem is that when I run the application, after hitting the playbutton I get an error saying that in line 75 - if (stringWallColor.equals("Default - Black")), stringWallColor is null.
Does that I mean that it uses the public static String stringWallColor which is null as default? And how can I fix this?
public static String stringWallColor;
public static void main(String[] args) {
launch(args);
System.out.println("Done!");
}
public void start(Stage primaryStage) throws Exception {
Main.primaryStage = primaryStage;
MenuBar MENU = new MenuBar();
MenuGenerator.menuCreator(MENU);
Button playButton = new Button("Play Game");
ComboBox<String> wallColorCombo = new ComboBox<>();
wallColorCombo.setPromptText("Choose wall color");
wallColorCombo.getItems().addAll(
"Default - Black",
"Dark Green",
"Dark Red",
"Dark Gray",
"Saddle Brown",
"Midnight Blue",
"Dark Magenta",
"Crimson",
"Navy");
stringWallColor = wallColorCombo.getSelectionModel().getSelectedItem();
gameGrid = new GridPane();
GridPane root = new GridPane();
root.add(MENU,0,0);
root.add(gameGrid, 0, 1);
gameScene = new Scene(root,600,625);
GridPane startGrid = new GridPane();
startGrid.setHgap(20);
startGrid.setVgap(20);
playButton.setLineSpacing(10);
startGrid.add(playButton,8,12);
startGrid.add(wallColorCombo,7,12);
GridPane root0= new GridPane();
root0.add(startGrid,0,1);
Scene startScene = new Scene(root0, 400, 400);
primaryStage.setScene(startScene);
primaryStage.setTitle(GameEngine.GAME_NAME);
primaryStage.show();
System.out.println("Default save file not loaded yet");
playButton.setOnAction(e -> {
MenuGenerator.loadDefaultSaveFile(primaryStage);
System.out.println("Default save file loaded");
primaryStage.setScene(gameScene); });
}
public static Color getWallColor() {
Color wallColor = Color.BLACK;
if (stringWallColor.equals("Default - Black"))
wallColor = Color.BLACK;
if (stringWallColor.equals("Dark Green"))
wallColor = Color.DARKGREEN;
if (stringWallColor.equals("Dark Red"))
wallColor = Color.DARKRED;
if (stringWallColor.equals("Dark Gray"))
wallColor = Color.DARKGRAY;
if (stringWallColor.equals("Saddle Brown"))
wallColor = Color.SADDLEBROWN;
if (stringWallColor.equals("Midnight Blue"))
wallColor = Color.MIDNIGHTBLUE;
if (stringWallColor.equals("Dark Magenta"))
wallColor = Color.DARKMAGENTA;
if (stringWallColor.equals("Crimson"))
wallColor = Color.CRIMSON;
if (stringWallColor.equals("Navy"))
wallColor = Color.NAVY;
return wallColor;
}
}
Solved by setting stringWallColor = wallColorCombo.getSelectionModel().getSelectedItem(); as an action for the playbutton.

JavaFX CheckBoxTreeItem graphic disappear/appear when expanding/collapsing

I'm trying to make TreeView with CheckBoxTreeItems. When I collapse/expand a CheckBoxTreeItems the image I set up does not display correctly. I googled but I couldn't find correct answer. On Stack Overflow, I found a similar problem, but I didn't get a valid answer.
E.g
JavaFX CheckBoxTreeItem graphic disappear when siblings collapse
JavaFX CheckBoxTreeItem: Graphic disappears if graph is extended
Any ideas?
public class ClientApplication extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(final Stage stage) {
ImageView folderIcon = new ImageView();
Image folderImage = new Image("image/folder.png");
folderIcon.setImage(folderImage);
folderIcon.setFitWidth(16);
folderIcon.setFitHeight(16);
CheckBoxTreeItem<String> rootItem = new CheckBoxTreeItem<String>("folder", folderIcon);
rootItem.setExpanded(true);
for (int i = 0; i < 4; i++) {
CheckBoxTreeItem<String> checkBoxTreeItem = new CheckBoxTreeItem<String>("Sample" + (i + 1), folderIcon);
rootItem.getChildren().add(checkBoxTreeItem);
}
final TreeView<String> tree = new TreeView<String>(rootItem);
tree.setCellFactory(CheckBoxTreeCell.<String>forTreeView());
tree.setRoot(rootItem);
tree.setShowRoot(true);
StackPane root = new StackPane();
root.getChildren().add(tree);
stage.setScene(new Scene(root, 300, 250));
stage.show();
}
}
enter image description here
I tried to use the ideas provided by #Jai,But when I click the expand/collapse icon, there is still a problem.Attachment is a screenshot.Thanks in advance.
enter image description here
ImageView is a JavaFX control. This means that each instance represents a unique control you see on your screen. You should never use the same instance for multiple locations in your GUI.
On the other hand, Image represents an image (i.e. an array of pixel data), so it is reusable.
This should work:
#Override
public void start(final Stage stage) {
final Image folderImage = new Image("image/folder.png");
CheckBoxTreeItem<String> rootItem = new CheckBoxTreeItem<String>("folder", createImageView(folderImage));
rootItem.setExpanded(true);
for (int i = 0; i < 4; i++) {
CheckBoxTreeItem<String> checkBoxTreeItem = new CheckBoxTreeItem<String>("Sample" + (i + 1), createImageView(folderImage));
rootItem.getChildren().add(checkBoxTreeItem);
}
final TreeView<String> tree = new TreeView<String>(rootItem);
tree.setCellFactory(CheckBoxTreeCell.<String>forTreeView());
tree.setRoot(rootItem);
tree.setShowRoot(true);
StackPane root = new StackPane();
root.getChildren().add(tree);
stage.setScene(new Scene(root, 300, 250));
stage.show();
}
private ImageView createImageView(Image folderImage) {
ImageView folderIcon = new ImageView();
folderIcon.setImage(folderImage);
folderIcon.setFitWidth(16);
folderIcon.setFitHeight(16);
return folderIcon;
}

Listen for transformation changes

Is there way to listen for transformation changes? I'd like to get notified when for example a cube has turned. Especially I'am interested in getLocalToSceneTransform.
Here is my try:
#Override
public void start(Stage primaryStage) throws Exception {
final Group root = new Group();
final Scene scene = new Scene(root);
final Box cube = new Box(1, 1, 1);
cube.setRotationAxis(Rotate.Y_AXIS);
cube.setMaterial(new PhongMaterial(Color.RED));
root.getChildren().add(cube);
cube.getLocalToSceneTransform().addEventHandler(TransformChangedEvent.TRANSFORM_CHANGED, (e) -> {
// never get called
System.out.println("Transformation has changed");
});
final Camera camera = new PerspectiveCamera(true);
camera.setTranslateZ(-4);
scene.setCamera(camera);
final Timeline turnCube = new Timeline();
turnCube.getKeyFrames().add(new KeyFrame(Duration.seconds(0), new KeyValue(cube.rotateProperty(), 0)));
turnCube.getKeyFrames().add(new KeyFrame(Duration.seconds(5), new KeyValue(cube.rotateProperty(), 360)));
turnCube.setCycleCount(Timeline.INDEFINITE);
turnCube.play();
primaryStage.setWidth(1024);
primaryStage.setHeight(768);
primaryStage.setScene(scene);
primaryStage.show();
}
While the cube is turned the EventHandler get never called.
You can add a changeListener to the cube's localToSceneTransformProperty() to get notified when the cube is rotated.
cube.localToSceneTransformProperty().addListener((value, oldValue, newValue) -> {
System.out.println("Transformation has changed");
});

Categories