I want a TextArea's font-size to increase or decrease depending on the width property of the Scene it's in. But I don't want the font-size to grow beyond 16px or shrink beyond 10px.
I found this older post that has the code for making TextArea's font-size grow and shrink from being binded to the Scene's width property but I'm not sure how to add the conditional Bindings for the functionality I want.
This is the code from that post which suits my needs:
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class FontBind extends Application {
private DoubleProperty fontSize = new SimpleDoubleProperty(10);
private IntegerProperty blues = new SimpleIntegerProperty(50);
#Override
public void start(Stage primaryStage) {
Button btn = new Button("click me, I change color");
btn.setOnAction((evt)->{blues.set(blues.get()+20);});//max?
Label lbl = new Label("I'm a label");
TextArea ta = new TextArea("Lots of text can be typed\nand even number 1234567890");
HBox hbox = new HBox(new Label("I never change"));
VBox child = new VBox(btn, lbl, ta);
VBox root = new VBox(child, hbox);
Scene scene = new Scene(root, 300, 250);
fontSize.bind(scene.widthProperty().add(scene.heightProperty()).divide(50));
child.styleProperty().bind(Bindings.concat("-fx-font-size: ", fontSize.asString(), ";"
,"-fx-base: rgb(100,100,",blues.asString(),");"));
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Any help would be very much appreciated. I really want to learn more about using Bindings but I'm having trouble understanding how to implement the Bindings methods
After applying the color adjust effect and redrawing the canvas , the canvas is not being cleared so that i can update the canvas with some other image, the images are overlapping.
I have a Border Pane containing Scroll Pane to adjust large images
The scroll Pane contains the canvas
A function that clears the canvas then adjusts the brightness and applies the effect on canvas.
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.ScrollPane;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application {
Stage primaryStage;
// load the image
Image image = new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Gatto_europeo4.jpg/1024px-Gatto_europeo4.jpg");
// the container for the image as a javafx node
Canvas canvas = new Canvas(600,700);
GraphicsContext gc = canvas.getGraphicsContext2D();
#Override
public void start(Stage primaryStage) throws Exception{
this.primaryStage = primaryStage;
primaryStage.setTitle("Test");
BorderPane root = new BorderPane();
// container for image layers
ScrollPane scrollPane = new ScrollPane();
// use scrollpane for image view in case the image is large
scrollPane.setContent(canvas);
gc.drawImage(image,0,0,canvas.getWidth(),canvas.getHeight());
changeBrightness(canvas,image);// function to add effect on canvas
// put scrollpane in scene
root.setCenter(scrollPane);
primaryStage.setScene(new Scene(root, 780.0, 620.0));
primaryStage.show();
}
private void changeBrightness(Canvas canvas, Image image) {
//clearing canvas before changing brightness working
gc.clearRect(0,0,canvas.getWidth(),canvas.getHeight());
ColorAdjust colorAdjust = new ColorAdjust();
colorAdjust.setBrightness(0.3);
gc.setEffect(colorAdjust);
gc.drawImage(image,0,0,canvas.getWidth(),canvas.getHeight());
//trying to clear canvas after changing brightness
gc.clearRect(0,0,canvas.getWidth(),canvas.getHeight());
gc.drawImage(image,0,0,canvas.getWidth()/2,canvas.getHeight()/2);
}
public static void main(String[] args) {
launch(args);
}
}
You have to remove the effect on your Graphics object before calling gc.clearRect(...)
Here the code:
package com.simple.test;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.ScrollPane;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class Main extends Application {
Stage primaryStage;
// load the image
Image image = new Image("https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Gatto_europeo4.jpg/1024px-Gatto_europeo4.jpg");
// the container for the image as a javafx node
Canvas canvas = new Canvas(600,700);
GraphicsContext gc = canvas.getGraphicsContext2D();
#Override
public void start(Stage primaryStage) throws Exception{
this.primaryStage = primaryStage;
primaryStage.setTitle("Test");
BorderPane root = new BorderPane();
// container for image layers
ScrollPane scrollPane = new ScrollPane();
// use scrollpane for image view in case the image is large
scrollPane.setContent(canvas);
gc.drawImage(image,0,0,canvas.getWidth(),canvas.getHeight());
changeBrightness(canvas,image);// function to add effect on canvas
// put scrollpane in scene
root.setCenter(scrollPane);
primaryStage.setScene(new Scene(root, 780.0, 620.0));
primaryStage.show();
}
private void changeBrightness(Canvas canvas, Image image) {
//clearing canvas before changing brightness working
gc.clearRect(0,0,canvas.getWidth(),canvas.getHeight());
ColorAdjust colorAdjust = new ColorAdjust();
colorAdjust.setBrightness(0.3);
gc.setEffect(colorAdjust);
gc.drawImage(image,0,0,canvas.getWidth(),canvas.getHeight());
// Remove the effect
gc.setEffect(null);
//trying to clear canvas after changing brightness
gc.clearRect(0,0,canvas.getWidth(),canvas.getHeight());
// Now apply again
gc.setEffect(colorAdjust);
// Redraw
gc.drawImage(image,0,0,canvas.getWidth()/2,canvas.getHeight()/2);
// Remove the effect again
gc.setEffect(null);
}
public static void main(String[] args) {
launch(args);
}
}
You may look at this for further information:
JavaFX GraphicsContext clearRect doesn't work with clip mask
Why won't gc.clearRect clear the canvas?
Btw, cat is cute.
I create a button and I want to set up its width and height through css. I tried this but it didn't work.
Other properties( such as font-size and text fill) are fine and work correctly. But the width of Button seems to be fixed and doesn't change.
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
VBox root = new VBox();
Button button = new Button("Button");
root.getChildren().add(button);
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("Style.css").toExternalForm());
button.setId("button");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
And in "Style.css":
#button{
-fx-width: 300;
-fx-font-size:20;
-fx-text-fill: yellow;
}
What I want to do:
Have a slider, from 0 - 10, have it move only by a full tick(1), and show the value of the tick above the tick.
What happens:
It shows all values from 0-10 except for 9 which is mysteriously missing..
I wanted to upload an image here but I lack the reputation it seems ;)
https://pasteboard.co/HdPtzVp.png
Code:
Main Class:
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.stage.Screen;
import javafx.stage.Stage;
public class StartGui extends Application
{
#Override
public void start(Stage stage)
{
HBox box = new HBox();
PionnenSlider slider = new PionnenSlider();
box.getChildren().add(slider);
Scene scene = new Scene(box, 500, 300);
stage.setScene(scene);
stage.setTitle("SliderMinimalCode");
Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
Slider class:
import javafx.scene.control.Slider;
public class PionnenSlider extends Slider
{
public PionnenSlider()
{
setMinorTickCount(0);
setBlockIncrement(1);
setMajorTickUnit(1);
setValue(0);
setMax(10);
setShowTickMarks(true);
setShowTickLabels(true);
setSnapToTicks(true);
}
}
apparently it is due to lack of space for the slider, found a workaround to automaticly size it with HBox.setHgrow(sliderPionnen, Priority.ALWAYS);, although that just grows it to fill the entire HBOX.. so I guess I'l have to find a workaround for that
It doesn't matter in the following code whether i pass the value 50 or 300 to the function. all my buttons are always in a row unless i resize the window. According to theory after the row of my nodes reaches 50 pixels (suppose i pass that value to the setPrefWrapLength() method) my next node should be in the next row. But that is not happening. Can someone explain me why?
package test2;
import com.sun.java.swing.plaf.gtk.GTKConstants;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Orientation;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Test2 extends Application {
#Override
public void start(Stage primaryStage) {
Button btn1 = new Button("one");
Button btn2 = new Button("two");
Button btn3 = new Button("three");
Button btn4 = new Button("four");
FlowPane fpane = new FlowPane(Orientation.HORIZONTAL, 10, 10, btn1, btn2, btn3, btn4);
fpane.setPrefWrapLength(50);
Scene scene = new Scene(fpane, 300, 250);
primaryStage.setTitle("Flow Pane");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
update :
I found out the following line in the javaFX documentation and i think this is in context with this question
Note that prefWrapLength is used only for calculating the preferred size and may not reflect the actual wrapping dimension, which tracks the actual size of the flowpane.