I'm using RichTextFX and I was surprised that the CodeArea does not come with a scrollbar enabled by default. How can I get this to appear?
This worked for me. I used an AnchorPane as a root node.
CodeArea codeArea = new CodeArea();
VirtualizedScrollPane sp = new VirtualizedScrollPane(codeArea);
anchorPane.getChildren().add(sp);
anchorPane.setLeftAnchor(sp, 0.0);
anchorPane.setRightAnchor(sp, 0.0);
anchorPane.setBottomAnchor(sp, 0.0);
anchorPane.setTopAnchor(sp, 0.0);
codeArea.prefWidthProperty().bind(anchorPane.widthProperty());
codeArea.prefHeightProperty().bind(anchorPane.heightProperty());
Related
I am having a problem with JavaFX 3D, the problem is as follows:
When I turn my perspective camera around, the last added box (blue box) overlaps the first added box (red box), here is a screenshot:
can anyone tell me why is this happening? And is there a way to fix it? (the boxes are literally 2 box classes with a width, height, depth, position and color)
Minimal reproducible example since somebody asked for it:
Box box1 = new Box();
Box box2 = new Box();
box1.setWidth(300);
box2.setWidth(300);
box1.setHeight(300);
box2.setHeight(300);
box1.setDepth(300);
box2.setDepth(300);
box1.setTranslateX(300);
box2.setTranslateX(300);
box1.setTranslateY(300);
box2.setTranslateX(300);
Group root = new Group();
root.getChildren().addAll(box, box2);
PerspectiveCamera cam = new PerspectiveCamera();
Scene scene = new Scene(root);
scene.setCamera(camera);
stage.setScene(scene);
stage.show();
where stage is the stage inside public void start(Stage stage), JavaFX's default run method (any class that extends Application should implement it)
You probably have to add a subscene with the depth buffer enabled. See: https://openjfx.io/javadoc/15/javafx.graphics/javafx/scene/SubScene.html#%3Cinit%3E(javafx.scene.Parent,double,double,boolean,javafx.scene.SceneAntialiasing)
Solution:
I used the following constructor:
new Scene(root, WIDTH, HEIGHT, true, SceneAntialiasing.BALANCED);
instead of:
new Scene(root);
I'am not familiar with JavaFx so I'am trying to code my JavaFx Components in Java Controller classes and calling them in main, my problem is that I cant size my layouts with all the width of the the window, I have this simple code :
public Acceuil(){
LAB_POST = new Button("Posts");
LAB_EVENT = new Button("Evennements");
.....
// my two boxes
VBox leftPane = new VBox();
FlowPane bpane = new FlowPane();
leftPane.setPadding(new Insets(20));
leftPane.setSpacing(30);
leftPane.setAlignment(Pos.CENTER);
leftPane.setStyle("-fx-background-color:#34495e");
leftPane.setMinWidth(300);
leftPane.setPrefSize(300, 600);
bpane.setStyle("-fx-background-color: red;"); // to see the space that it took
bpane.setMaxWidth(Double.MAX_VALUE);
bpane.setMaxWidth(Double.MAX_VALUE);
bpane.setMaxWidth(Double.MAX_VALUE);
bpane.setMaxWidth(Double.MAX_VALUE);
leftPane.getChildren().addAll(TXT_SEARCH, LAB_POST....);
this.getChildren().addAll(leftPane,bpane);
this.setMinHeight(600);
this.setMinWidth(900);
}
(Ps: I tried the FlowPane,BorderPane,AnchorPane and even Boxes)
So I'am Expecting to have the bpane in all the space that remains in my window but I get this,
So I'am wondering what I need to type to let the bpane take all the space, Thank you
I agree with Sedrick we need to know what is the container of these elements but you can try this:
bpane.setStyle("-fx-background-color: red;");
HBox.setHgrow(bpane, Priority.ALWAYS); // Add this line
Also if it is inside an AnchorPane you can try this:
AnchorPane.setTopAnchor(this, 0.0);
AnchorPane.setRightAnchor(this, 0.0);
AnchorPane.setBottomAnchor(this, 0.0);
AnchorPane.setLeftAnchor(this, 0.0);
Can you please tell me, how can I make a pane scrollable? I am able to make an image scrollable, like this:
VBox root = new VBox();
ScrollPane sp = new ScrollPane();
sp.setContent(new ImageView(new Image(getClass().getResourceAsStream("test.png"))));
root.getChildren().add(sp);
But when I am trying to make a whole pane scrollable, which consists of some Buttons and a small animation, there happens nothing regarding scrollbars. There are no scrollbars. How can I solve that?
Thank you in advance.
I tried to do it by myself, and it is because your scroll pane has fixed width and height, by resizing window, your scroll pane doesn't resize on itself. You can use anchor pane so scroll pane will resize with anchor pane. You could do something like this:
ImageView image = new ImageView(new Image("wp1951596.jpg"));
ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(image);
scrollPane.setPrefSize(400, 400);
AnchorPane.setTopAnchor(scrollPane, 0.);
AnchorPane.setRightAnchor(scrollPane, 0.);
AnchorPane.setBottomAnchor(scrollPane, 0.);
AnchorPane.setLeftAnchor(scrollPane, 0.);
rootPane.getChildren().add(scrollPane);
I would also recommend you using JavaFX with .fxml files. Then you can create your layout in SceneBuilder and it will make everything easier and faster.
Due to programming an Image Viewer with JavaFX I implemented an image library with following structure:
HBox - ScrollPane - VBox
This is the basic structure, the library objects are constructed as following:
StackPane - ImageView - Label
Because there's no setAlignment method for ScrollPane, I put the whole thing in a HBox, even though I don't need the other features of it.
These methods are responsible for the generation and initialization of the library:
private HBox createScrollableLibrary()
{
libraryContent = new VBox();
libraryContent.setPadding(new Insets(10));
libraryContent.setAlignment(Pos.CENTER);
libraryContent.setStyle("-fx-background-color: transparent");
libraryContent.setSpacing(10);
libraryScroll = new ScrollPane();
libraryScroll.setHbarPolicy(ScrollBarPolicy.NEVER);
libraryScroll.setStyle("-fx-background: rgb(0, 0, 0, 0.3);"
+ "-fx-background-color: rgb(0, 0, 0, 0.3);");
libraryScroll.setMinWidth(135);
libraryScroll.setMaxWidth(135);
libraryScroll.setContent(libraryContent);
HBox root = new HBox();
root.setPickOnBounds(false);
root.getChildren().add(libraryScroll);
return root;
}
And:
public void initLibrary()
{
Runnable work = () ->
{
ObservableList<StackPane> previewList = FXCollections.observableArrayList();
ImageView preview;
Label label;
StackPane previewAndLabel;
for(int i = 0; i < picturePaths.size(); i++)
{
String path = picturePaths.get(i).toURI().toString();
preview = new ImageView(new Image(path));
preview.setPreserveRatio(true);
preview.setFitWidth(100);
//label = new Label(new File(path).getName());
label = new Label("IMAGE");
StackPane.setAlignment(label, Pos.CENTER_LEFT);
previewAndLabel = new StackPane();
previewAndLabel.getChildren().addAll(preview, label);
previewList.add(previewAndLabel);
}
Platform.runLater(() -> GUI.getLibraryContent().getChildren().addAll(previewList));
};
loading = new Thread(work);
if(loading.isAlive())
loading.interrupt();
loading.start();
}
Info: picturePaths is an ObservableList which contains the paths of all selected images which in turn are selected with a FileChooser.
Now my question(s):
I know it's very inefficient to use large sized pictures as preview images.
Even though I increased my heap space to 1GB it slows down and throws a java.lang.OutOfMemoryError exception when I'm adding more pictures, I'm guessing the limit is about 20-25 of large sized pictures.
How can I "slim" a normal picture with, for example a resolution of 1920x1080 to 1024x720 ? Or is there another opportunity to make my library use small and "fast" images? And can I buffer my already "calculated" images to make my Image Viewer faster?
Appreciate any tips and help. Please critisize my code if needed!
One can try setting the size constraints on the preview pictures during the Image constructor and not afterwards.
Background
I am trying to build a image gallery based on the description here.
I have a StackPane, with ScrollPane and AnchorPane as its children, added in the same order.
ScrollPane contains a list of images contained in ImageView's, while the Anchorpane has two buttons, anchored to the left and right corners, to scroll the images !
Problem
The scrolling is working fine, as the buttons are placed on the AnchorPane, which is the top child on the StackPane.
I want to implement the double-click on the imageView to open it in fullscreen. Since the imageView is a child of ScrollPane, it is not able to listen to the mouse event.
Is there a way through which I can make the ImageView listen to the mouseEvent ?
Instead of placing the AnchorPane and ScrollPane in a StackPane, try using the AnchorPane as the root of this structure. Add the ScrollPane to it first with appropriate anchors and then add the buttons. That way the buttons will still be on top, but there will be nothing interfering with the mouse events on the content of the scroll pane.
ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(...);
// scrollPane fills entire anchor pane:
AnchorPane.setLeftAnchor(scrollPane, 0.0);
AnchorPane.setRightAnchor(scrollPane, 0.0);
AnchorPane.setTopAnchor(scrollPane, 0.0);
AnchorPane.setBottomAnchor(scrollPane, 0.0);
Button button1 = new Button(...);
Button button2 = new Button(...);
// button1 in top left:
AnchorPane.setTopAnchor(button1, 0.0);
AnchorPane.setLeftAnchor(button1, 0.0);
// button2 in top right:
AnchorPane.setTopAnchor(button2, 0.0);
AnchorPane.setRightAnchor(button2, 0.0);
AnchorPane anchorPane = new AnchorPane();
anchorPane.getChildren().addAll(scrollPane, button1, button2);