Whatever I try, the slider refuses to lengthen/stretch horizontally. My theory is that the GridPane cell sizes are overriding the slider's x coordinate. Any solutions?
here's my entire class:
public class MoodMeter extends GridPane {
public MoodMeter() {
GridPane grid = new GridPane();
grid.setAlignment(Pos.TOP_CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
Slider slider = new Slider(-100, 100, 0);
slider.setOrientation(Orientation.HORIZONTAL);
slider.setMaxWidth(100);
slider.setBlockIncrement(100);
setHgrow(slider, Priority.ALWAYS);
grid.add(slider, 1, 0);
Scene scene = new Scene(grid, 700, 100);
Stage stage = new Stage();
stage.setTitle("Diary Mood Meter");
stage.setScene(scene);
stage.setResizable(false);
stage.show();
Rectangle2D desktop = Screen.getPrimary().getVisualBounds();
stage.setY(((desktop.getMaxY() + desktop.getMinY()) / 2) + 200);
}
}
And this is the result:
(not enough population)
http://i.imgur.com/cfvdKIM.png
Solved.
Changed
slider.setMaxWidth(100);
slider.setBlockIncrement(100);
to
slider.setMaxWidth(1000);
slider.setBlockIncrement(1000);
Without knowing the rest of your GridPane code setup, there are several ways to achieve that:
set slider.setMaxWidth(Double.MAX_VALUE)
add ColumnConstraints for horizontal resizing to the GridPane
set the Hgrow of your slider accordingly.
Just set the hgrow property for the slider to ALWAYS:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class SliderInGridPane extends Application {
#Override
public void start(Stage primaryStage) {
GridPane root = new GridPane();
root.add(new Label("Value:"), 0, 0);
Slider slider = new Slider(0, 100, 50);
GridPane.setHgrow(slider, Priority.ALWAYS);
root.add(slider, 1, 0);
primaryStage.setScene(new Scene(root, 250, 75));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Related
my name is Omar, I made this code in javafx in order to switch between scene 0 and scene 1.
When an input scanner laser barcode number is introduced (6590055780)
it changes from scene 0 to scene 1, once in scene 1, if user pressed a button will change to scene 0.
But when i compile this code, it gives me the following error
"Exception in thread "JavaFX Application Thread" java.lang.illegalArgumentException: Grid hgap=10.0 alignment=CENTERis already set as root of another scene"
I do not know why i get this error, it is like the setOnKeyPressed does not allow me to leave from it, when i want to switch from scene 0 to scene 1, i got this error message, anyhelp is appreciated in order to solve this problem. thank you very much
and this is the code
import javafx.application.Application;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
import javafx.event.EventHandler;
import javafx.scene.control.TextArea;
import javafx.scene.input.KeyEvent;
public class scannerinputfx2 extends Application {
////////////* public class*///////
public static String salida;
#Override
public void start(Stage primaryStage){
primaryStage.setTitle("what's up");
//scene 0
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
Scene scene = new Scene(grid, 300, 275);
TextArea area = new TextArea();
TextField userTextField = new TextField();
//grid.add(userTextField, 0, 0);
//grid.add(area, 0,1);
GridPane.setConstraints(userTextField, 0,0);
GridPane.setConstraints(area,0,1);
grid.getChildren().addAll(area, userTextField);
//scene 1
GridPane grid1 = new GridPane();
grid1.setAlignment(Pos.CENTER);
grid1.setHgap(10);
grid1.setVgap(10);
grid1.setPadding(new Insets(25, 25, 25, 25));
Scene scene1 = new Scene(grid1, 300, 275);
Button btnfinal = new Button("Press button to get back");
GridPane.setConstraints(btnfinal, 0, 0);
grid1.getChildren().addAll(btnfinal);
btnfinal.setOnAction(actionEvent -> {
scene1.setRoot(grid);
});
primaryStage.setScene(scene);
primaryStage.show();
userTextField.setOnKeyPressed(new EventHandler<KeyEvent>() {
public void handle(KeyEvent ke) {
area.setText( userTextField.getText());
if (userTextField.getText().equals("6590055780"))
{
System.out.println("The number is " + userTextField.getText());
userTextField.setText("");
scene.setRoot(grid1);
}
else
{
System.out.println("The number is not the indicated");
}
}
});
}
////////*Here finish primary stage/////////
public static void main(String[] args) {
launch(args);
}
}
Im a beginner working on a simple Chat with javafx, i have already searched for similar problems and didnt find a fitting solution. The first thing i need to do is the Graphics. My first Problem is, that my MousEvent Listener for the Button isnt working, i simply cant click the Button somehow. My second problem is, once the application is running and i click somewhere outside of the TextField, i cant return to it and enter new Text. Like the Listeners of the TextField, which listen for KeyStroke events dont run anymore. Code:
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class ChatView extends Application {
String s;
#Override
public void start(Stage stage){
s = "";
StackPane rootPane = new StackPane();
TextField enterMessageField = new TextField();
enterMessageField.setEditable(true);
TextArea displayAllMessages = new TextArea();
displayAllMessages.setPrefHeight(500);
displayAllMessages.setEditable(false);
ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(displayAllMessages);
scrollPane.setVbarPolicy(ScrollBarPolicy.ALWAYS);
displayAllMessages.setPrefWidth(650);
Button button = new Button("Send Message");
VBox vBoxChat = new VBox();
vBoxChat.setPadding(new Insets(650, 200, 20, 20));
vBoxChat.getChildren().addAll( enterMessageField);
VBox vBoxChatIncoming = new VBox();
vBoxChatIncoming.setPadding(new Insets(20, 20, 20, 20));
vBoxChatIncoming.getChildren().addAll( scrollPane);
VBox vBoxEnter = new VBox();
vBoxEnter.setPadding(new Insets(650, 20, 20, 550));
vBoxEnter.getChildren().add(button);
rootPane.getChildren().addAll(vBoxChat, vBoxEnter, vBoxChatIncoming);
Scene scene = new Scene(rootPane, 700, 700, Color.WHITE);
stage.setScene(scene);
stage.setTitle("Chat");
stage.show();
enterMessageField.setOnKeyPressed(new EventHandler <KeyEvent> () {
#Override
public void handle(KeyEvent event){
if (event.getCode() == KeyCode.ENTER){
s = enterMessageField.getText() + "\n";
enterMessageField.setText("");
displayAllMessages.appendText(s);
}
}
});
button.setOnAction((event) -> {
s = enterMessageField.getText() + "\n";
enterMessageField.setText("");
System.out.println(s);
});
}
public static void main(String[] args){
launch(args);
}
}
Thank you for your help!
I changed the rootPane to GridPane and this fact fixed the problems.
Actually the StackPane is not suitable type for multicomponent interface. See the similar problem here: Mouse Events get Ignored on the Underlying Layer
See a bit improved code below:
public class ChatView extends Application {
String s = "";
#Override
public void start(Stage stage) {
TextField enterMessageField = new TextField();
enterMessageField.setEditable(true);
TextArea displayAllMessages = new TextArea();
displayAllMessages.setPrefHeight(500);
displayAllMessages.setEditable(false);
ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(displayAllMessages);
scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);
displayAllMessages.setPrefWidth(650);
Button button = new Button("Send Message");
button.setDefaultButton(true);
VBox vBoxChat = new VBox(enterMessageField);
vBoxChat.setPadding(new Insets(10, 10, 10, 10));
VBox vBoxChatIncoming = new VBox(displayAllMessages);
vBoxChatIncoming.setPadding(new Insets(10, 10, 10, 10));
VBox vBoxEnter = new VBox(button);
vBoxEnter.setPadding(new Insets(10, 10, 10, 10));
GridPane rootPane = new GridPane();
rootPane.add(vBoxChatIncoming, 0, 0);
rootPane.add(vBoxChat, 0, 1);
rootPane.add(vBoxEnter, 1, 1);
Scene scene = new Scene(rootPane, 800, 700, Color.WHITE);
stage.setScene(scene);
stage.setTitle("Chat");
stage.show();
enterMessageField.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ENTER) {
s = enterMessageField.getText() + "\n";
enterMessageField.setText("");
displayAllMessages.appendText(s);
}
});
button.setOnAction((event) -> {
s = enterMessageField.getText() + "\n";
enterMessageField.setText("");
System.out.println(s);
});
}
public static void main(String[] args) {
launch(args);
}
}
quick question,
why are my multiple windows not appearing from this code?
also any tips on how to make the circle's diameter increase only by the stage's resolution when the window is increased in size; anyway of doing this internally through "circ3."
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
public class Circles extends Application {
#Override
public void start(Stage primaryStage){
Pane pane = new Pane();
Circle circ = new Circle();
circ.setStroke(Color.DARKBLUE);
circ.setFill(Color.YELLOW);
circ.centerXProperty().bind(pane.widthProperty().divide(10));
circ.centerYProperty().bind(pane.heightProperty().subtract(20));
circ.centerXProperty().bind(pane.widthProperty().subtract(20));
circ.setRadius(20);
pane.getChildren().add(circ);
Scene scene = new Scene(pane, 500, 200);
primaryStage.setTitle("Bottom Right");
primaryStage.setScene(scene);
primaryStage.show();
Pane pane2 = new Pane();
Circle circ2 = new Circle();
circ2.setStroke(Color.PEACHPUFF);
circ2.setFill(Color.YELLOWGREEN);
circ2.centerXProperty().bind(pane2.widthProperty().divide(2));
circ2.centerYProperty().bind(pane2.heightProperty().subtract(20));
circ2.setRadius(20);
pane2.getChildren().add(circ2);
Scene scene2 = new Scene(pane2, 200, 500);
primaryStage.setTitle("Bottom Centered");
primaryStage.setScene(scene2);
primaryStage.show();
Pane pane3 = new Pane();
Circle circ3 = new Circle();
circ3.setStroke(Color.PEACHPUFF);
circ3.setFill(Color.YELLOWGREEN);
circ3.centerXProperty().bind(pane3.widthProperty().subtract(150));
circ3.centerYProperty().bind(pane3.heightProperty().divide(2));
circ3.setRadius(150);
//size (circle diameter) needs to scale with width resolution
pane3.getChildren().add(circ3);
//boilerplate
Scene scene3 = new Scene(pane3, 300, 500);
primaryStage.setTitle("Radius / Width ");
primaryStage.setScene(scene3);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
You can make the circle change size with the window size using bindings:
Pane pane3 = new Pane();
Circle circ3 = new Circle();
circ3.setStroke(Color.PEACHPUFF);
circ3.setFill(Color.YELLOWGREEN);
circ3.centerXProperty().bind(pane3.widthProperty().divide(2));
circ3.centerYProperty().bind(pane3.heightProperty().divide(2));
circ3.radiusProperty().bind(pane3.widthProperty().divide(2));
Im trying to realize a special FadeTransition effect. But I have no idea how I can manage it. For some node I would like to increase the opacity from left to right (for example in Powerpoint, you can change the slides with such an effect). Here is an easy example for rectangles. But the second one should fadeIn from left to right (the opacity should increase on the left side earlier as on the right side). With timeline and KeyValues/KeyFrames I found also no solution.
Thanks in advance.
Rectangle rec2;
#Override
public void start(Stage stage) {
Group root = new Group();
Scene scene = new Scene(root, 400, 300, Color.BLACK);
stage.setTitle("JavaFX Scene Graph Demo");
Pane pane = new Pane();
Button btnForward = new Button();
btnForward.setText(">");
btnForward.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
FadeTransition ft = new FadeTransition(Duration.millis(2000), rec2);
ft.setFromValue(0.);
ft.setToValue(1.);
ft.play();
}
});
Rectangle rec1 = new Rectangle(0, 0, 300,200);
rec1.setFill(Color.RED);
rec2 = new Rectangle(100, 50, 100,100);
rec2.setFill(Color.GREEN);
rec2.setOpacity(0.);
pane.getChildren().addAll(rec1,rec2);
root.getChildren().add(pane);
root.getChildren().add(btnForward);
stage.setScene(scene);
stage.show();
}
Define the fill of the rectangle using css with a linear gradient which references looked-up colors for the left and right edges of the rectangle. (This can be inline or in an external style sheet.)
Define a couple of DoublePropertys representing the opacities of the left and right edge.
Define the looked-up colors on the rectangle or one of its parents using an inline style bound to the two double properties.
Use a timeline to change the values of the opacity properties.
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;
public class FadeInRectangle extends Application {
#Override
public void start(Stage primaryStage) {
Group root = new Group();
Scene scene = new Scene(root, 400, 300, Color.BLACK);
primaryStage.setTitle("JavaFX Scene Graph Demo");
Pane pane = new Pane();
Rectangle rec1 = new Rectangle(0, 0, 300,200);
rec1.setFill(Color.RED);
Rectangle rec2 = new Rectangle(100, 50, 100,100);
rec2.setStyle("-fx-fill: linear-gradient(to right, left-col, right-col);");
final DoubleProperty leftEdgeOpacity = new SimpleDoubleProperty(0);
final DoubleProperty rightEdgeOpacity = new SimpleDoubleProperty(0);
root.styleProperty().bind(
Bindings.format("left-col: rgba(0,128,0,%f); right-col: rgba(0,128,0,%f);", leftEdgeOpacity, rightEdgeOpacity)
);
Button btnForward = new Button();
btnForward.setText(">");
btnForward.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Timeline timeline = new Timeline(
new KeyFrame(Duration.ZERO, new KeyValue(leftEdgeOpacity, 0)),
new KeyFrame(Duration.ZERO, new KeyValue(rightEdgeOpacity, 0)),
new KeyFrame(Duration.millis(500), new KeyValue(rightEdgeOpacity, 0)),
new KeyFrame(Duration.millis(1500), new KeyValue(leftEdgeOpacity, 1)),
new KeyFrame(Duration.millis(2000), new KeyValue(rightEdgeOpacity, 1)),
new KeyFrame(Duration.millis(2000), new KeyValue(leftEdgeOpacity, 1))
);
timeline.play();
}
});
pane.getChildren().addAll(rec1,rec2);
root.getChildren().add(pane);
root.getChildren().add(btnForward);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I created this simple example of TextArea
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class MainApp extends Application
{
#Override
public void start(Stage stage) throws Exception
{
HBox hbox = new HBox();
// Text Area
TextArea dataPane = new TextArea();
dataPane.setScrollTop(0);
dataPane.setEditable(false);
dataPane.prefWidthProperty().bind(hbox.widthProperty());
dataPane.setWrapText(true); // New line of the text exceeds the text area
dataPane.setPrefRowCount(10);
dataPane.appendText("Computer software, or simply software, also known as computer programs");
dataPane.appendText("\nis the non-tangible component of computers.");
dataPane.appendText("\nComputer software contrasts with computer hardware, which");
dataPane.appendText("\nis the physical component of computers.");
dataPane.appendText("Computer hardware and software require each");
dataPane.appendText("\nother and neither can be");
dataPane.appendText("\nrealistically used without the other.");
dataPane.appendText("\nComputer software");
hbox.setAlignment(Pos.CENTER);
hbox.setSpacing(1);
hbox.setPadding(new Insets(10, 0, 10, 0));
hbox.getChildren().add(dataPane);
Scene scene = new Scene(hbox, 800, 90);
stage.setTitle("JavaFX and Maven");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
I want to set by default the the position of the slider to be at the top of the TextArea.
Can you help me to fix this?
If you set dataPane.setScrollTop(0); after your stage is shown, it will work :)
So like this:
#Override
public void start(Stage stage) {
HBox hbox = new HBox();
// Text Area
TextArea dataPane = new TextArea();
dataPane.setEditable(false);
dataPane.prefWidthProperty().bind(hbox.widthProperty());
dataPane.setWrapText(true); // New line of the text exceeds the text area
dataPane.setPrefRowCount(10);
dataPane.appendText("Computer software, or simply software, also known as computer programs");
dataPane.appendText("\nis the non-tangible component of computers.");
dataPane.appendText("\nComputer software contrasts with computer hardware, which");
dataPane.appendText("\nis the physical component of computers.");
dataPane.appendText("Computer hardware and software require each");
dataPane.appendText("\nother and neither can be");
dataPane.appendText("\nrealistically used without the other.");
dataPane.appendText("\nComputer software");
hbox.setAlignment(Pos.CENTER);
hbox.setSpacing(1);
hbox.setPadding(new Insets(10, 0, 10, 0));
hbox.getChildren().add(dataPane);
Scene scene = new Scene(hbox, 800, 90);
stage.setTitle("JavaFX and Maven");
stage.setScene(scene);
stage.show();
dataPane.setScrollTop(0.0);
}