I would like to implement a text input using javafx. The cursor looks like a black rectangle (for people with low vision). It seems impossible with a text area or a text field. The mouse shape can be an arrow for example. Can you give ideas to implement this function ?
Here is full example
import com.sun.javafx.scene.control.skin.TextFieldSkin;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class CaretColorizer extends Application {
#Override
public void start(Stage stage) throws Exception {
TextField redCaretTextField = new TextField("Big black caret");
redCaretTextField.setSkin(
new TextFieldCaretControlSkin(
redCaretTextField,
Color.RED
)
);
VBox layout = new VBox(10, redCaretTextField);
layout.setPadding(new Insets(10));
stage.setScene(new Scene(layout));
stage.show();
}
public class TextFieldCaretControlSkin extends TextFieldSkin {
public TextFieldCaretControlSkin(TextField textField, Color caretColor) {
super(textField);
caretPath.strokeProperty().unbind();
caretPath.fillProperty().unbind();
caretPath.setStrokeWidth(10);
caretPath.setStroke(Color.BLACK);
caretPath.setFill(Color.BLACK);
}
}
public static void main(String[] args) {
launch(args);
}
}
Related
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
I'm beginner of javafx:
I am developing a graphical user interface using javafx and Scene builder that has a coordinate plane with x axis and y axis. It should be like this: https://postimg.cc/image/98k9mvnb3/
when someone do a mouse click anywhere on this coordinate plane it will show the coordinate point(x,y) of the pixel on the console and there will a mark(like point or some text will be written) on the same place where mouse clicked.
For implementing these things I have to use canvas & i'm able to get the coordinate point but i'm not getting how to draw the coordinate plane and how to write something on the pixel where mouse clicked.
Here is my code:
Controller Class
package application;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.canvas.Canvas;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
public class AxisController implements Initializable{
#FXML
private AnchorPane anchr;
#FXML
private Canvas canvas;
#Override
public void initialize(URL location, ResourceBundle resources) {
assert canvas != null : "fx:id=\"canvas\" was not injected: check your FXML file 'AxisFxml.fxml'.";
}
#FXML
private void handleMouse(MouseEvent event){
System.out.println(event.getX());
System.out.println(event.getY());
}
}
Main Class:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
public class AxisMain extends Application {
#Override
public void start(Stage primaryStage) {
try {
AnchorPane root = FXMLLoader.load(getClass().getResource("/application/AxisFxml.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Have you tried something as simple as putting some code like this into your handleMouse method?
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.BLUE);
gc.fillRect(event.getX()-5,event.getY()-5,10,10);
This assumes of course that you have also attached this method to the canvas so that you actually get the events in the right coordinate system.
I need either a Label or a Text for my project. I need the label so that ellipsis can be used. The problem though, is when I try to use a FadeTransition, and play it, the label gets slightly darker at the start. Here is some demo code:
package com.neonorb.test;
import javafx.animation.FadeTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.util.Duration;
import java.io.IOException;
/**
* Created by chris on 7/20/15.
*/
public class Test extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws IOException {
Label label = new Label("hello");
//Text label = new Text("hello);//Using Text instead of Label does not cause the weird behavior
FadeTransition fadeTransition = new FadeTransition(Duration.seconds(3), label);
fadeTransition.setFromValue(1.0);
fadeTransition.setToValue(0.0);
fadeTransition.setOnFinished(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
label.setOpacity(1.0);
}
});
Button button = new Button("play");
button.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
fadeTransition.play();
}
});
BorderPane borderPane = new BorderPane();
borderPane.setCenter(label);
borderPane.setBottom(button);
primaryStage.setScene(new Scene(borderPane));
primaryStage.show();
}
}
So I either need a fix to this problem, or a way to use ellipsis in Text. Any ideas?
Set the opacity of the label to 0.99 initially:
label.setOpacity(0.99);
Also change the code inside setOnFinished method in the same way. Then, set the starting value of the fade transition to 0.99:
fadeTransition.setFromValue(0.99);
I know this is not the solution you are looking for, but this solution prevents the label from abruptly getting darker at the start. That is because the label actually starts with that darker status.
I have a TextArea that doesn't scroll down when I add text in it. I thought using this answer, but my TextArea is connected to a StringProperty like this :
consoleTextArea.textProperty().bind(textRecu);
So the answer doesn't work for me, is there another way to make my TextArea scroll down every time I update it by the binding?
Here is fast demo of what i meant in comment about adding listener to the textRecu. Yep consoleTextArea.textProperty() can't be changed because of a binding. But textRecu has no binding => can be changed and we can add listener to it.
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
private StringProperty textRecu = new SimpleStringProperty();
private TextArea consoleTextArea = new TextArea();
#Override
public void start(Stage primaryStage) throws Exception{
VBox root = new VBox();
Button button = new Button("Add some text");
button.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
//here you change textRecu and not consoleTextArea.textProperty()
textRecu.setValue(textRecu.getValue() +"New Line\n");
}
});
root.getChildren().addAll(consoleTextArea, button);
consoleTextArea.textProperty().bind(textRecu);
//here you also add listener to the textRecu
textRecu.addListener(new ChangeListener<Object>() {
#Override
public void changed(ObservableValue<?> observable, Object oldValue,
Object newValue) {
// from stackoverflow.com/a/30264399/1032167
// for some reason setScrollTop will not scroll properly
//consoleTextArea.setScrollTop(Double.MAX_VALUE);
consoleTextArea.selectPositionCaret(consoleTextArea.getLength());
consoleTextArea.deselect();
}
});
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}