javafx error after export but running in debug - javafx

My project does not run after export, but in netbeans 8.2 working.
This happens only when load exceeding 14 images (13 images works fine). Here my code and the jarerror.
In debug netbeans and eclipse work all.
I tried with scene builder and without, with every IDE and got same error.
<(https://postimg.org/image/wfxomlxkb/9a5ee3f5>?
package javafxprimoprogetto;
import java.net.URL;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javax.imageio.ImageIO;
public class JavaFXprimoprogetto extends Application {
#Override
public void start(Stage primaryStage) {
Image castl = newImage("file:///C:/Users/lorys/Documents/NetBeansProjects/JavaFXprimoprogetto/src/javafxprimoprogetto/castle.png");
ImageView castle = new ImageView(castl);
castle.setFitHeight(900.0);
castle.setFitWidth(1600.0);
castle.setPickOnBounds(true);
castle.setPreserveRatio(true);
Image warriorferm = new Image("file:///C:/Users/lorys/Documents/NetBeansProjects/JavaFXprimoprogetto/src/javafxprimoprogetto/warriorfermogif.gif");
ImageView warfermo = new ImageView(warriorferm);
warfermo.setFitHeight(150.0);
warfermo.setFitWidth(200.0);
warfermo.setLayoutX(419.0);
warfermo.setLayoutY(339.0);
warfermo.setPickOnBounds(true);
warfermo.setPreserveRatio(true);
Image slimeferm = new Image("file:///C:/Users/lorys/Documents/NetBeansProjects/JavaFXprimoprogetto/src/javafxprimoprogetto/slimefermo.gif");
ImageView slimefermo = new ImageView(slimeferm);
slimefermo.setFitHeight(113.0);
slimefermo.setFitWidth(187.0);
slimefermo.setLayoutX(426.0);
slimefermo.setLayoutY(401.0);
slimefermo.setPickOnBounds(true);
slimefermo.setPreserveRatio(true);
slimefermo.setVisible(false);
Image wormcammina_1 = new Image("file:///C:/Users/lorys/Documents/NetBeansProjects/JavaFXprimoprogetto/src/javafxprimoprogetto/slime2cammina.png");
ImageView wormcammina1 = new ImageView(wormcammina_1);
wormcammina1.setFitHeight(150.0);
wormcammina1.setFitWidth(200.0);
wormcammina1.setLayoutX(20.0);
wormcammina1.setLayoutY(20.0);
wormcammina1.setPickOnBounds(true);
wormcammina1.setPreserveRatio(true);
wormcammina1.setVisible(true);
Imagewormcammina_2=newImage("file:///C:/Users/lorys/Documents/NetBeansProjects/JavaFXprimoprogetto/src/javafxprimoprogetto/slime3 copia.png");
ImageView wormcammina2 = new ImageView(wormcammina_2);
wormcammina2.setFitHeight(150.0);
wormcammina2.setFitWidth(200.0);
wormcammina2.setLayoutX(10.0);
wormcammina2.setLayoutY(10.0);
wormcammina2.setPickOnBounds(true);
wormcammina2.setPreserveRatio(true);
wormcammina2.setVisible(true);
Image wormcammina_3 = new Image("file:///C:/Users/lorys/Documents/NetBeansProjects/JavaFXprimoprogetto/src/javafxprimoprogetto/slime4 copia.png");
ImageView wormcammina3 = new ImageView(wormcammina_3);
wormcammina3.setFitHeight(150.0);
wormcammina3.setFitWidth(200.0);
wormcammina3.setLayoutX(30.0);
wormcammina3.setLayoutY(30.0);
wormcammina3.setPickOnBounds(true);
wormcammina3.setPreserveRatio(true);
wormcammina3.setVisible(true);
Image wormcammina_4 = new Image("file:///C:/Users/lorys/Documents/NetBeansProjects/JavaFXprimoprogetto/src/javafxprimoprogetto/slime5 copia.png");
ImageView wormcammina4 = new ImageView(wormcammina_4);
wormcammina4.setFitHeight(150.0);
wormcammina4.setFitWidth(200.0);
wormcammina4.setLayoutX(40.0);
wormcammina4.setLayoutY(40.0);
wormcammina4.setPickOnBounds(true);
wormcammina4.setPreserveRatio(true);
wormcammina4.setVisible(true);
Image wormcammina_5 = new Image("file:///C:/Users/lorys/Documents/NetBeansProjects/JavaFXprimoprogetto/src/javafxprimoprogetto/slime5 copia.png");
ImageView wormcammina5 = new ImageView(wormcammina_5);
wormcammina5.setFitHeight(150.0);
wormcammina5.setFitWidth(200.0);
wormcammina5.setLayoutX(70.0);
wormcammina5.setLayoutY(70.0);
wormcammina5.setPickOnBounds(true);
wormcammina5.setPreserveRatio(true);
wormcammina5.setVisible(true);
Image wormcammina_6 = new Image("file:///C:/Users/lorys/Documents/NetBeansProjects/JavaFXprimoprogetto/src/javafxprimoprogetto/slime6 copia.png");
ImageView wormcammina6 = new ImageView(wormcammina_6);
wormcammina6.setFitHeight(150.0);
wormcammina6.setFitWidth(200.0);
wormcammina6.setLayoutX(45.0);
wormcammina6.setLayoutY(45.0);
wormcammina6.setPickOnBounds(true);
wormcammina6.setPreserveRatio(true);
wormcammina6.setVisible(true);
Image wormcammina_7 = new Image("file:///C:/Users/lorys/Documents/NetBeansProjects/JavaFXprimoprogetto/src/javafxprimoprogetto/slime7 copia.png");
ImageView wormcammina7 = new ImageView(wormcammina_7);
wormcammina7.setFitHeight(150.0);
wormcammina7.setFitWidth(200.0);
wormcammina7.setLayoutX(50.0);
wormcammina7.setLayoutY(50.0);
wormcammina7.setPickOnBounds(true);
wormcammina7.setPreserveRatio(true);
wormcammina7.setVisible(true);
Image wormcammina_8 = new Image("file:///C:/Users/lorys/Documents/NetBeansProjects/JavaFXprimoprogetto/src/javafxprimoprogetto/slime8 copia.png");
ImageView wormcammina8 = new ImageView(wormcammina_8);
wormcammina8.setFitHeight(150.0);
wormcammina8.setFitWidth(200.0);
wormcammina8.setLayoutX(80.0);
wormcammina8.setLayoutY(80.0);
wormcammina8.setPickOnBounds(true);
wormcammina8.setPreserveRatio(true);
wormcammina8.setVisible(true);
Image wormcammina_9 = new Image("file:///C:/Users/lorys/Documents/NetBeansProjects/JavaFXprimoprogetto/src/javafxprimoprogetto/slime9 copia.png");
ImageView wormcammina9 = new ImageView(wormcammina_9);
wormcammina9.setFitHeight(150.0);
wormcammina9.setFitWidth(200.0);
wormcammina9.setLayoutX(90.0);
wormcammina9.setLayoutY(90.0);
wormcammina9.setPickOnBounds(true);
wormcammina9.setPreserveRatio(true);
wormcammina9.setVisible(true);
Image wormcammina_10 = new Image("file:///C:/Users/lorys/Documents/NetBeansProjects/JavaFXprimoprogetto/src/javafxprimoprogetto/slime10 copia.png");
ImageView wormcammina10 = new ImageView(wormcammina_10);
wormcammina10.setFitHeight(150.0);
wormcammina10.setFitWidth(200.0);
wormcammina10.setLayoutX(99.0);
wormcammina10.setLayoutY(99.0);
wormcammina10.setPickOnBounds(true);
wormcammina10.setPreserveRatio(true);
wormcammina10.setVisible(true);
Image raggioattakk1_1 = new Image("file:///C:/Users/lorys/Documents/NetBeansProjects/JavaFXprimoprogetto/src/javafxprimoprogetto/raggioattak1 copia.png");
ImageView flashattacco1_1= new ImageView(raggioattakk1_1);
flashattacco1_1.setFitHeight(150.0);
flashattacco1_1.setFitWidth(200.0);
flashattacco1_1.setPickOnBounds(true);
flashattacco1_1.setPreserveRatio(true);
flashattacco1_1.setVisible(true);
Image wormprendidann= new Image("file:///C:/Users/lorys/Documents/NetBeansProjects/JavaFXprimoprogetto/src/javafxprimoprogetto/slimeprendidanno.png");
ImageView wormprendidanno= new ImageView(wormprendidann);
wormprendidanno.setFitHeight(113.0);
wormprendidanno.setFitWidth(187.0);
wormprendidanno.setLayoutX(568.0);
wormprendidanno.setLayoutY(369.0);
wormprendidanno.setPickOnBounds(true);
wormprendidanno.setPreserveRatio(true);
wormprendidanno.setVisible(true);
Image warcammin= new Image("file:///C:/Users/lorys/Documents/NetBeansProjects/JavaFXprimoprogetto/src/javafxprimoprogetto/warcamminaoriginalegif.gif");
ImageView warcammina= new ImageView(warcammin);
warcammina.setFitHeight(150.0);
warcammina.setFitWidth(200.0);
warcammina.setPickOnBounds(true);
warcammina.setPreserveRatio(true);
warcammina.setVisible(true);
Rectangle veraposizioneworm = new Rectangle();
Rectangle veraposizionewar = new Rectangle();
veraposizioneworm.setArcHeight(5.0);
veraposizioneworm.setArcWidth(5.0);
veraposizioneworm.setFill(javafx.scene.paint.Color.DODGERBLUE);
veraposizioneworm.setHeight(10.0);
veraposizioneworm.setLayoutX(426.0);
veraposizioneworm.setLayoutY(390.0);
veraposizioneworm.setStroke(javafx.scene.paint.Color.BLACK);
veraposizioneworm.setStrokeType(javafx.scene.shape.StrokeType.INSIDE);
veraposizioneworm.setWidth(150.0);
veraposizionewar.setArcHeight(5.0);
veraposizionewar.setArcWidth(5.0);
veraposizionewar.setFill(javafx.scene.paint.Color.valueOf("#1f93ff"));
veraposizionewar.setHeight(10.0);
veraposizionewar.setLayoutX(496.0);
veraposizionewar.setLayoutY(451.0);
veraposizionewar.setStroke(javafx.scene.paint.Color.BLACK);
veraposizionewar.setStrokeType(javafx.scene.shape.StrokeType.INSIDE);
veraposizionewar.setWidth(20.0);
Image raggioattakk1_2= new Image("file:///C:/Users/lorys/Documents/NetBeansProjects/JavaFXprimoprogetto/src/javafxprimoprogetto/raggioattak2 copia.png");
ImageView flashattacco1_2= new ImageView(raggioattakk1_2);
flashattacco1_2.setFitHeight(150.0);
flashattacco1_2.setFitWidth(200.0);
flashattacco1_2.setPickOnBounds(true);
flashattacco1_2.setPreserveRatio(true);
flashattacco1_2.setVisible(true);
Image fl3= new Image("file:///C:/Users/lorys/Documents/NetBeansProjects/JavaFXprimoprogetto/src/javafxprimoprogetto/raggioattak3 copia.png");
ImageView flashattacco1_3= new ImageView(fl3);
flashattacco1_3.setFitHeight(150.0);
flashattacco1_3.setFitWidth(200.0);
flashattacco1_3.setPickOnBounds(true);
flashattacco1_3.setPreserveRatio(true);
flashattacco1_3.setVisible(true);
Image warshoo7= new Image("file:///C:/Users/lorys/Documents/NetBeansProjects/JavaFXprimoprogetto/src/javafxprimoprogetto/war7 copia.png");
ImageView warshoot7= new ImageView(warshoo7);
warshoot7.setFitHeight(150.0);
warshoot7.setFitWidth(200.0);
warshoot7.setPickOnBounds(true);
warshoot7.setPreserveRatio(true);
warshoot7.setVisible(true);
Image warshoo6= new Image("file:///C:/Users/lorys/Documents/NetBeansProjects/JavaFXprimoprogetto/src/javafxprimoprogetto/war6_5 copia.png");
ImageView warshoot6= new ImageView(warshoo6);
warshoot6.setFitHeight(150.0);
warshoot6.setFitWidth(200.0);
warshoot6.setPickOnBounds(true);
warshoot6.setPreserveRatio(true);
warshoot6.setVisible(true);
Image warshoo5= new Image("file:///C:/Users/lorys/Documents/NetBeansProjects/JavaFXprimoprogetto/src/javafxprimoprogetto/war6 copia.png");
ImageView warshoot5= new ImageView(warshoo5);
warshoot5.setFitHeight(150.0);
warshoot5.setFitWidth(200.0);
warshoot5.setPickOnBounds(true);
warshoot5.setPreserveRatio(true);
warshoot5.setVisible(true);
Image warshoo4= new Image("file:///C:/Users/lorys/Documents/NetBeansProjects/JavaFXprimoprogetto/src/javafxprimoprogetto/war5 copia.png");
ImageView warshoot4= new ImageView(warshoo4);
warshoot4.setFitHeight(150.0);
warshoot4.setFitWidth(200.0);
warshoot4.setPickOnBounds(true);
warshoot4.setPreserveRatio(true);
warshoot4.setVisible(true);
Image warshoo3= new Image("file:///C:/Users/lorys/Documents/NetBeansProjects/JavaFXprimoprogetto/src/javafxprimoprogetto/war4 copia.png");
ImageView warshoot3= new ImageView(warshoo3);
warshoot3.setFitHeight(150.0);
warshoot3.setFitWidth(200.0);
warshoot3.setPickOnBounds(true);
warshoot3.setPreserveRatio(true);
warshoot3.setVisible(true);
Image warshoo2= new Image("file:///C:/Users/lorys/Documents/NetBeansProjects/JavaFXprimoprogetto/src/javafxprimoprogetto/war3 copia.png");
ImageView warshoot2= new ImageView(warshoo2);
warshoot2.setFitHeight(150.0);
warshoot2.setFitWidth(200.0);
warshoot2.setPickOnBounds(true);
warshoot2.setPreserveRatio(true);
warshoot2.setVisible(true);
Image warshoo1= new Image("file:///C:/Users/lorys/Documents/NetBeansProjects/JavaFXprimoprogetto/src/javafxprimoprogetto/war2 copia.png");
ImageView warshoot1= new ImageView(warshoo1);
warshoot1.setFitHeight(150.0);
warshoot1.setFitWidth(200.0);
warshoot1.setPickOnBounds(true);
warshoot1.setPreserveRatio(true);
warshoot1.setVisible(true);
//anchorpane occhio alle posizioni dei figli
AnchorPane root = new AnchorPane();
root.getChildren().addAll(castle,warfermo,slimefermo,wormcammina1,wormcammina2,wormcammina3,wormcammina4,wormcammina5
,wormcammina6,wormcammina7,wormcammina8,wormcammina9,wormcammina10,flashattacco1_1);
Scene scene = new Scene(root, 1600, 900);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
} }

Related

I'm trying to figure out how to make my VBox inside a BorderPane expand with the BorderPane/Stage, but still stay within a certain bounds

I am trying to figure out how to make my VBox, only go to the size of 300 pixel's wide, but i would like to have it at say 250 pixels wide when the program is initialized, then when the user clicks full screen, I want it to expand, but not necessarily with the entire space it would have. I want it to only go to 300 pixels (and have the 3 buttons inside do the same thing) but I'm not sure how to do that. I'm having trouble determining PrefSize and CompSize actual meanings and uses. Any help would be great.
I am also having kind of the same problem with the Label, inside the HBox, that is inside a SplitPane, that is inside a BorderPane. Any explanation of why what you are suggesting will work, will help me with future problems like this. Thank you
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class MainStarUI extends Application {
#Override
public void start(Stage primaryStage){
MenuBar mainMenuOne = addMenuBar();
VBox leftVBoxOne = addVbox();
//AnchorPane midPaneOne = addAnchorPane();
//HBox topHBoxOne = addHBox();
SplitPane midSplitPane = addSplitPane();
BorderPane mainPane = new BorderPane();
mainPane.setTop(mainMenuOne);
mainPane.setLeft(leftVBoxOne);
mainPane.setCenter(midSplitPane);
primaryStage.setMinWidth(1440);
primaryStage.setMinHeight(900);
Scene mainScene = new Scene(mainPane);
primaryStage.setScene(mainScene);
primaryStage.show();
}
public MenuBar addMenuBar(){
Menu menuOne = new Menu("File");
Menu menuTwo = new Menu("Edit");
Menu menuThree = new Menu("Help");
Menu menuFour = new Menu("Exit");
MenuItem menuItemOne = new MenuItem("File");
MenuItem menuItemTwo = new MenuItem("Open");
MenuItem menuItemThree = new MenuItem("Exit");
menuOne.getItems().add(menuItemOne);
menuOne.getItems().add(menuItemTwo);
menuFour.getItems().add(menuItemThree);
MenuBar mainMenuOne = new MenuBar();
mainMenuOne.getMenus().add(menuOne);
mainMenuOne.getMenus().add(menuTwo);
mainMenuOne.getMenus().add(menuThree);
mainMenuOne.getMenus().add(menuFour);
mainMenuOne.maxHeight(25);
mainMenuOne.minHeight(25);
return mainMenuOne;
}
public VBox addVbox(){
VBox leftVBox = new VBox();
leftVBox.setMinWidth(300);
leftVBox.setPrefWidth(300);
leftVBox.setPadding(new Insets(15));
leftVBox.setSpacing(20);
leftVBox.setStyle("-fx-background-color: #336699;");
Button firstButton = new Button("Ships, Components, Items & Weaponry");
firstButton.setMinSize(270, 270);
firstButton.setMaxSize(270, 270);
Button secondButton = new Button("Trading, Mining, Refining & Commodities");
secondButton.setMinSize(270, 270);
secondButton.setMaxSize(270,270);
Button thirdButton = new Button("Star Systems, Planets, Moons & Locations");
thirdButton.setMinSize(270,270);
thirdButton.setMaxSize(270, 270);
leftVBox.getChildren().addAll(firstButton, secondButton, thirdButton);
return leftVBox;
}
public HBox addHBox(){
Image logoImage = new Image("SCImages/TaktikalLogo1.jpg");
ImageView logoImageView = new ImageView();
logoImageView.setImage(logoImage);
logoImageView.setPreserveRatio(false);
logoImageView.setFitWidth(160);
logoImageView.setFitHeight(160);
logoImageView.setSmooth(true);
logoImageView.setCache(true);
Label topLabel = new Label("STAR CITIZEN INFONET & DATABASE");
topLabel.setFont(new Font("Arial", 48));
topLabel.setTextFill(Color.WHITE);
topLabel.setMinHeight(160);
topLabel.setMaxHeight(160);
HBox topHBox = new HBox();
topHBox.setStyle("-fx-background-color: black");
topHBox.setMinHeight(180);
topHBox.setMaxHeight(180);
topHBox.setPrefWidth(1090);
topHBox.getChildren().addAll(logoImageView, topLabel);
topHBox.setPadding(new Insets(10));
topHBox.setSpacing(10);
return topHBox;
}
public SplitPane addSplitPane(){
HBox topHBoxOne = addHBox();
AnchorPane anchorSplitPane = new AnchorPane();
SplitPane mainSplitPane = new SplitPane();
mainSplitPane.setOrientation(Orientation.VERTICAL);
mainSplitPane.setDividerPosition(1, 200);
mainSplitPane.setPrefSize(1090, 850);
mainSplitPane.getItems().addAll(topHBoxOne, anchorSplitPane);
return mainSplitPane;
}
public static void main(String[] args) {
launch(args);
}
}
I actually put my VBox inside an AnchorPane, and attached it to the anchors, and everything worked perfectly after I set my preferred height and width.

Alert box is not showing in the center of the application using JavaFX [duplicate]

This question already has answers here:
Center stage on parent stage
(2 answers)
Closed 4 years ago.
I am trying to pop-up the box in the center of the application when I resize the application or move it.
I tried with css alignment and also using Java. Is it possible if I don't use pane and directly add box in the scene?
Here is my code:
public Boolean call(String question) {
final Stage dialogStage = new Stage(StageStyle.UNDECORATED);
dialogStage.initModality(Modality.WINDOW_MODAL);
dialogStage.initOwner(owner);
dialogStage.setTitle("ConfirmTitle"); // WIP, waiting for the strings&trans
final Button ok = new Button(
nmsGuiContainer.getI18nService().getMessage("com.mlnms.gui.fmwk.main.container.ok")); // WIP,
// waiting
// for
// the
// strings&trans
ok.getStyleClass().add(HTML_POPUP_BUTTON_STYLE);
final Button cancel = new Button(
nmsGuiContainer.getI18nService().getMessage("com.mlnms.gui.fmwk.main.container.cancel")); // WIP,
// waiting
// for the
// strings&trans
cancel.getStyleClass().add(HTML_POPUP_BUTTON_STYLE);
final Text text = new Text(question);
text.getStyleClass().add(HTML_POPUP_STYLE);
final Insets ins = new Insets(10);
final VBox box = new VBox();
box.setAlignment(Pos.BOTTOM_CENTER);
box.setSpacing(10);
box.setPadding(ins);
final HBox buttons = new HBox(10);
buttons.getChildren().addAll(ok, cancel);
buttons.setAlignment(Pos.CENTER);
buttons.setPadding(ins);
box.getChildren().addAll(text, buttons);
box.getStyleClass().add(HTML_POPUP_STYLE);
StackPane pane = new StackPane();
pane.setAlignment(box, Pos.CENTER);
pane.getChildren().add(box);
Scene scene = new Scene(pane);
try {
URL javafxCss = nmsGuiContainer.getBundleContext().getBundle()
.getResource(NmsGuiContainer.JAVAFX_CSS_URL);
scene.getStylesheets().add(javafxCss.toExternalForm());
} catch (Exception e) {
LOGGER.error("Cannot load the CSS file for JavaFX components ", e);
}
dialogStage.setScene(scene);
ok.setCancelButton(false);
final boolean[] res = new boolean[1];
ok.setOnAction(new CloseDialogHandler(dialogStage, res));
cancel.setCancelButton(true);
cancel.setOnAction(new CloseDialogHandler(dialogStage, null));
dialogStage.centerOnScreen();
nmsGuiContainer.fadeContainer();
dialogStage.showAndWait();
nmsGuiContainer.unfadeContainer();
return res[0];
}
Here is a screenshot of the alertbox:
The Stage.initOwner() method does exactly what you need. While you do call it in your example code, I do not know what owner you are passing to it.
Here is a sample that demonstrates how to do this.
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
VBox root = new VBox(10);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(10));
Button btnShowAlert = new Button("Show Alert!");
// Set the action to show the alert
btnShowAlert.setOnAction(e -> {
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setHeaderText("This is centered over the main window!");
alert.setContentText("Move the main window and show the alert again!");
alert.initOwner(primaryStage);
alert.showAndWait();
});
root.getChildren().add(btnShowAlert);
primaryStage.setTitle("Centered Alerts");
primaryStage.setScene(new Scene(root));
primaryStage.setWidth(500);
primaryStage.setHeight(300);
primaryStage.show();
}
}

JavaFX Change ScrollPane ViewPort background color

I need to change the gray background color of the ScrollPane so where you see WHITE and BOLD Labels i want the background to be white.
Image from the Application.
Code:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.CheckMenuItem;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.RadioMenuItem;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;
public class MainApp extends Application {
private static final int WINDOW_WIDTH = 800;
private static final int WINDOW_HEIGHT = 200;
private String family = "Helvetica";
private TextFlow textFlow = new TextFlow();
#Override
public void start(Stage stage) {
HBox hbox = new HBox();
Group group = getGroup();
hbox.getChildren().add(group);
hbox.setAlignment(Pos.TOP_LEFT);
hbox.getStyleClass().add("hbox");
MenuBar menuBar = new MenuBar();
menuBar.prefWidthProperty().bind(stage.widthProperty());
// File menu - new, save, exit
Menu fileMenu = new Menu("File");
MenuItem newMenuItem = new MenuItem("New");
MenuItem saveMenuItem = new MenuItem("Save");
MenuItem exitMenuItem = new MenuItem("Exit");
exitMenuItem.setOnAction(actionEvent -> Platform.exit());
fileMenu.getItems().addAll(newMenuItem, saveMenuItem,
new SeparatorMenuItem(), exitMenuItem);
Menu webMenu = new Menu("Web");
CheckMenuItem htmlMenuItem = new CheckMenuItem("HTML");
htmlMenuItem.setSelected(true);
webMenu.getItems().add(htmlMenuItem);
CheckMenuItem cssMenuItem = new CheckMenuItem("CSS");
cssMenuItem.setSelected(true);
webMenu.getItems().add(cssMenuItem);
Menu sqlMenu = new Menu("SQL");
ToggleGroup tGroup = new ToggleGroup();
RadioMenuItem mysqlItem = new RadioMenuItem("MySQL");
mysqlItem.setToggleGroup(tGroup);
RadioMenuItem oracleItem = new RadioMenuItem("Oracle");
oracleItem.setToggleGroup(tGroup);
oracleItem.setSelected(true);
sqlMenu.getItems().addAll(mysqlItem, oracleItem,
new SeparatorMenuItem());
Menu tutorialManu = new Menu("Tutorial");
tutorialManu.getItems().addAll(
new CheckMenuItem("Java"),
new CheckMenuItem("JavaFX"),
new CheckMenuItem("Swing"));
sqlMenu.getItems().add(tutorialManu);
menuBar.getMenus().addAll(fileMenu, webMenu, sqlMenu);
ScrollPane scrollPane = new ScrollPane(hbox);
scrollPane.setFitToHeight(true);
scrollPane.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);
scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);
BorderPane root = new BorderPane(scrollPane);
root.setPadding(new Insets(15));
root.setTop(menuBar);
//root.setCenter(hbox);
Scene scene = new Scene(root, WINDOW_WIDTH, WINDOW_HEIGHT);
//scene.getStylesheets().add("layoutstyle.css");
stage.setTitle("Dummy Title");
stage.setScene(scene);
stage.show();
Text text3 = new Text("\nDummy text");
text3.setFont(Font.font(family, 22));
textFlow.getChildren().add(text3);
}
private Group getGroup(){
double size = 50;
textFlow = new TextFlow();
textFlow.setLayoutX(40);
textFlow.setLayoutY(40);
Text text1 = new Text("White");
text1.setFont(Font.font(family, size));
text1.setFill(Color.RED);
Text text2 = new Text("\nBold");
text2.setFill(Color.ORANGE);
text2.setFont(Font.font(family, FontWeight.BOLD, size));
Text text3 = new Text("\n World");
text3.setFill(Color.GREEN);
text3.setFont(Font.font(family, FontPosture.ITALIC, size));
textFlow.getChildren().addAll(text1, text2, text3);
return new Group(textFlow);
}
public static void main(String[] args) {
launch(args);
}
}
You have to modify the .viewport color of the ScrollPane with css:
CSS code:
/*The ViewPort of the ScrollPane */
.scroll-pane .viewport {
-fx-background-color: white; /*or whatever you want*/
}
.scroll-pane {
-fx-background-color:transparent;
}
And change these lines of code from your application:
//........
//.........
root.setPadding(new Insets(15));
root.setTop(menuBar);
//root.setCenter(hbox);
Scene scene = new Scene(root, WINDOW_WIDTH, WINDOW_HEIGHT);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
stage.setTitle("Dummy Title");
stage.setScene(scene);
stage.show();
//........
//.........
Finally mention that the css file which contains the css code,in this example has to be in the same source folder as the Application.java file.
I guess your question is, how to apply a white background?
via css-style (an entry in your css-File would be better for that):
root.setStyle("-fx-background-color:white");
menuBar.styleProperty().bind(root.styleProperty());
via api
Background background = new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY));
root.setBackground(background);
menuBar.setBackground(background);

Move viewpoint of ScrollPane on Button click

There is concept of my JavaFX applictions:
All screens in one ScrollPane.
For example, if user click on button "Options" in loginScreen i want to animate Y-value of ViewPort to move it to optionsScreen.
How can i programmatically move viewport of ScrollPane with smooth animation?
Or you can offer better idea?
How can I programmatically move viewport of ScrollPane with smooth animation?
You can use a combination of Timeline and vvalueProperty of ScrollPane to perform a smooth animation of scrolling.
Here is a simple application where you I have three sections
Top
Center
Bottom
I am changing the vvalue of the ScrollPane through a Timeline on the action of the button.
The vvalue can have a value between 0.0 to 1.0, so you may have to do your own calculations to find the exact value which you want to be assigned to it.
The KeyValue performs the operation on scrollRoot.vvalueProperty().
The KeyFrame for the complete timeline is set at 500 milliseconds. You may increase or decrease it depending on the time you want the animation to run.
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class Main extends Application {
// For just adjusting the center rectangle w.r.t ScrollPane
private final int ADJUSTMENT_RATIO = 175;
#Override
public void start(Stage stage) {
Rectangle topRegion = new Rectangle(300, 300, Color.ALICEBLUE);
StackPane top = new StackPane(topRegion, new Label("Top"));
Rectangle centerRegion = new Rectangle(300, 300, Color.GOLDENROD);
StackPane center = new StackPane(centerRegion, new Label("center"));
Rectangle bottomRegion = new Rectangle(300, 300, Color.BISQUE);
StackPane bottom = new StackPane(bottomRegion, new Label("bottom"));
Button topButton = new Button("Top");
Button centerButton = new Button("Center");
Button bottomButton = new Button("Bottom");
HBox buttonBox = new HBox(15, topButton, centerButton, bottomButton);
buttonBox.setPadding(new Insets(20));
buttonBox.setAlignment(Pos.CENTER);
final VBox root = new VBox();
root.setSpacing(10);
root.setPadding(new Insets(10, 10, 10, 10));
root.getChildren().addAll(top, center, bottom);
ScrollPane scrollRoot = new ScrollPane(root);
Scene scene = new Scene(new VBox(buttonBox, scrollRoot));
stage.setTitle("Market");
stage.setWidth(350);
stage.setHeight(400);
stage.setScene(scene);
stage.show();
topButton.setOnAction(event -> {
final Timeline timeline = new Timeline();
final KeyValue kv = new KeyValue(scrollRoot.vvalueProperty(), 0.0);
final KeyFrame kf = new KeyFrame(Duration.millis(500), kv);
timeline.getKeyFrames().add(kf);
timeline.play();
});
centerButton.setOnAction(event -> {
final Timeline timeline = new Timeline();
final KeyValue kv = new KeyValue(scrollRoot.vvalueProperty(), (top.getBoundsInLocal().getHeight() + ADJUSTMENT_RATIO) / root.getHeight());
final KeyFrame kf = new KeyFrame(Duration.millis(500), kv);
timeline.getKeyFrames().add(kf);
timeline.play();
});
bottomButton.setOnAction(event -> {
final Timeline timeline = new Timeline();
final KeyValue kv = new KeyValue(scrollRoot.vvalueProperty(), 1.0);
final KeyFrame kf = new KeyFrame(Duration.millis(500), kv);
timeline.getKeyFrames().add(kf);
timeline.play();
});
}
public static void main(String[] args) {
launch(args);
}
}
Output

Tooltip isn't being displayed on ScrollPane

Following the tutorial here I tried to create a Tooltip on a ScrollPane using the following code:
final ScrollPane scroll = new ScrollPane();
scroll.addEventHandler(MouseEvent.MOUSE_MOVED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent t) {
pointer = MouseInfo.getPointerInfo();
point = pointer.getLocation();
color = robot.getPixelColor((int) point.getX(), (int) point.getY());
Tooltip tooltip = new Tooltip();
tooltip.setText(" " + color);
tooltip.activatedProperty();
scroll.setTooltip(tooltip);
System.out.println("Color at: " + point.getX() + "," + point.getY() + " is: " + color);
}
});
The tooltip however refuses to show itself on the ScrollPane but the output of "Color at: ..." is being printed so I am sure that handle is being called.
EDIT : On the suggestion of jewelsea , I tried putting the eventHandler on the content ,rather than the pane, but to no effect.
If I understand what you're trying to do, you really only need to install the tooltip once, and then just modify its text as the mouse moves.
This works for me:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Tooltip;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.PixelReader;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class ImageTooltipTest extends Application {
#Override
public void start(Stage primaryStage) {
BorderPane root = new BorderPane();
Image image = new Image("http://www.publicdomainpictures.net/pictures/30000/velka/tropical-paradise.jpg");
final ImageView imageView = new ImageView();
imageView.setImage(image);
final ScrollPane scroller = new ScrollPane();
scroller.setContent(imageView);
final Tooltip tooltip = new Tooltip();
scroller.setTooltip(tooltip);
scroller.getContent().addEventHandler(MouseEvent.MOUSE_MOVED, event -> {
Image snapshot = scroller.getContent().snapshot(null, null);
int x = (int) event.getX();
int y = (int) event.getY();
PixelReader pixelReader = snapshot.getPixelReader();
Color color = pixelReader.getColor(x, y);
String text = String.format("Red: %.2f%nGreen: %.2f%nBlue: %.2f",
color.getRed(),
color.getGreen(),
color.getBlue());
tooltip.setText(text);
});
root.setCenter(scroller);
Scene scene = new Scene(root, 800, 600);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Resources