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);
}
Related
I have a question. I need to make a GridPane with a directory choose that will then lead me to a modal dialog showing photos. I cannot figure how to do the modal dialog that also has to be a GridPane or a HBox...so the question is , how do I get to show a Modal Dialog after selecting the Folder and pressing the "Show" Button... Thanks a lot!
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
public class FotoView extends Application {
#Override
public void start(Stage primaryStage) {
TextField tf = new TextField();
Button b1 = new Button("Search");
Button b2 = new Button("Show");
DirectoryChooser dc = new DirectoryChooser();
GridPane gp = new GridPane();
gp.add(tf, 0 , 0);
gp.add(b1, 1, 0);
gp.add(b2, 0, 1);
b1.setOnAction(e-> dc.showDialog(primaryStage));
primaryStage.setScene(new Scene(gp)) ;
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
} ```
Below is a quick example where a first window has a button that opens up a DirectoryChooser. Once a directory has been selected a second smaller window opens up with the Modality set to APPLICATION_MODAL. In this second window you could add the image(s) that you load and add them to the GridPane.
import java.io.File;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.Modality;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(final String[] args) {
launch(args);
}
#Override
public void start(final Stage aStage) throws Exception {
final HBox root = new HBox();
final Button browseBtn = new Button("Click to open a Directory chooser");
root.getChildren().add(browseBtn);
browseBtn.setOnAction(e -> {
final DirectoryChooser chooser = new DirectoryChooser();
final File dir = chooser.showDialog(aStage);
openNewModalStage(aStage, dir);
});
final Scene scene = new Scene(root, 500, 500);
aStage.setScene(scene);
aStage.show();
}
private void openNewModalStage(final Stage aStage, final File aDirectory) {
final Stage stage = new Stage();
final GridPane grid = new GridPane();
final Scene scene = new Scene(grid);
grid.setStyle("-fx-background-color:black");
grid.setPrefWidth(400);
grid.setPrefHeight(400);
// get your images from 'aDirectory' and add them to your grid pane.
stage.setScene(scene);
// set the new windows Modality.
stage.initModality(Modality.APPLICATION_MODAL);
stage.show();
}
}
This way you would only need the one button and the dialog would show as soon as you've selected a directory. However, if you would still want a Search and Show button then just store the directory as a variable and add a listener on the 'show' button and move the openNewModalStage call to that one and remove the second argument.
Edit:
Also, depending on how many images and exactly what you want to display in the modal window, you might want to reconsider the GridPane and use a TilePane, or an hbox/vbox inside of a scroll pane. It's just a thought but I don't know what you will be doing with the GridPane.
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);
}
}
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);
}
}
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);
}
}
Am in attempt to design a pop up window. I designed it and it works, but with a small problem.
This is a part of code of the pop up window :
public class Warning extends BorderPane {
public Warning() {
setCenter(addVBox2());
}
private VBox addVBox2() {
VBox vbox = new VBox();
vbox.setPadding(new Insets(15,10,15,10));
vbox.setSpacing(10);
Label l1 = new Label("WARNING");
l1.setFont(Font.font("Calibri", FontWeight.BOLD, 20));
l1.setTextFill(Color.BLACK);
l1.setUnderline(true);
Label l2 = new Label("Try other User Name..!");
l2.setFont(Font.font("Calibri", FontWeight.BOLD, 18));
l2.setTextFill(Color.RED);
vbox.getChildren().addAll(l1, l2);
return vbox;
}
And this is how I call it :
setEffect(new BoxBlur(5, 10, 10));
Stage usrpagestage = new Stage();
usrpagestage.setMaxHeight(100);
usrpagestage.setMaxWidth(300);
usrpagestage.initStyle(StageStyle.UTILITY);
usrpagestage.setScene(new Scene(new Warning()));
usrpagestage.show();
usrpagestage.setOnCloseRequest(new EventHandler<WindowEvent>() {
#Override
public void handle(WindowEvent t) {
setEffect(new BoxBlur(0, 0, 0));
}
});
The pop up windows works when it is supposed to. But the contents in it is not fully displayed. This is the screen shot :
How to solve this issue ?
Remove the two lines:
usrpagestage.setMaxHeight(100);
usrpagestage.setMaxWidth(300);
After that, the stage will revert to its default behavior of automatically sizing to fit the initial scene content.
No, it didn't worked.
Yep, it didn't worked at all :-)
It should have worked (letting the stage autosize itself should be the correct solution).
It didn't work because of a bug in the JavaFX layout libraries:
RT-31665 Size of stage with style UTILITY is not correctly calculated.
You can workaround the bug by manually calling stage.sizeToScene().
Sample Code
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.*;
import javafx.scene.web.WebView;
import javafx.stage.*;
public class WarningSample extends Application {
#Override public void start(Stage stage) throws Exception {
final WebView view = new WebView();
view.getEngine().load("http://www.google.com");
stage.setScene(new Scene(view));
stage.show();
Stage warningStage = new Stage();
warningStage.initStyle(StageStyle.UTILITY);
warningStage.setScene(new Scene(new Warning()));
// this workaround allows the stage to be sized correctly.
warningStage.sizeToScene();
warningStage.show();
}
public static void main(String[] args) { launch(); }
private class Warning extends VBox {
public Warning() {
setPadding(new Insets(15, 10, 15, 10));
setSpacing(10);
Label heading = new Label("WARNING");
heading.setFont(Font.font("Calibri", FontWeight.BOLD, 20));
heading.setTextFill(Color.BLACK);
heading.setUnderline(true);
Label body = new Label("Try other User Name..!");
body.setFont(Font.font("Calibri", FontWeight.BOLD, 18));
body.setTextFill(Color.RED);
getChildren().addAll(heading, body);
}
}
}