changing my textfield to upper/lower case is not working as intended - javafx

After creating my GUI my next step was to have the buttons be able to change the textfield to their respective case when pressed. However none of the ways I have tried seem to demonstrate that.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class UpperLowerClass extends Application {
private Button upperButton;
private Button lowerButton;
private String userText;
private TextField userInput;
private Stage window;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
window = primaryStage;
window.setTitle("Uppercase to Lowercase");
//GRID
GridPane grid = new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));
grid.setVgap(10);
grid.setHgap(10);
//UPPERCASE BUTTON
upperButton = new Button("Uppercase");
upperButton.setOnAction(this::changeTextButton);
GridPane.setConstraints(upperButton, 0, 0);
//LOWERCASE BUTTON
lowerButton = new Button("Lowercase");
lowerButton.setOnAction(this::changeTextButton);
GridPane.setConstraints(lowerButton, 0, 1);
//TEXTFIELD
TextField userInput = new TextField();
userInput.getText();
userInput.setText(String.valueOf(userText));
GridPane.setConstraints(userInput, 0, 2);
grid.getChildren().addAll(upperButton, lowerButton, userInput);
//SCENE
Scene myScene = new Scene(grid, 300, 250);
window.setScene(myScene);
window.show();
primaryStage.setScene(myScene);
primaryStage.show();
}
//EVENT
public void changeTextButton(ActionEvent event)
{
if (event.getSource() ==upperButton) {
userText = userText.toUpperCase();
}
if (event.getSource() ==lowerButton) {
userInput.setText(String.valueOf(userText.toLowerCase()));
}
}
}
There must be a problem with how I am retrieving the text or is it entirely because of the manipulation I am trying to do in the If statements? Each button is the two versions I have tried which leads me to think that I am retrieving the text wrong or manipulating wrong. Thank you in advance!

Is the String userText needed elsewhere? Getting and setting the text of userInput directly through getText() and setText() would be better imo. Additionally, there is no need to create a local variable for userInput when you have it as a global variable already.
This is your code tested working: (I removed the changeTextButton method and just made the changes from the buttons directly, but this could be split out easily enough.)
public class UpperLowerClass extends Application {
private Button upperButton;
private Button lowerButton;
private TextField userInput;
private Stage window;
#Override
public void start(Stage primaryStage) {
try {
window = primaryStage;
//GRID
GridPane grid = new GridPane();
grid.setPadding(new Insets(10, 10, 10, 10));
grid.setVgap(10);
grid.setHgap(10);
//UPPERCASE BUTTON
upperButton = new Button("Uppercase");
upperButton.setOnAction(event -> {
if(userInput.getText()!=null) {
userInput.setText(userInput.getText().toUpperCase());
}
});
// upperButton.setOnAction(this::changeTextButton);
GridPane.setConstraints(upperButton, 0, 0);
//LOWERCASE BUTTON
lowerButton = new Button("Lowercase");
//lowerButton.setOnAction(this::changeTextButton);
lowerButton.setOnAction(event -> {
if(userInput.getText()!=null) {
userInput.setText(userInput.getText().toLowerCase());
}
});
GridPane.setConstraints(lowerButton, 0, 1);
//TEXTFIELD
userInput = new TextField(); //don't need to create new Textfield here
GridPane.setConstraints(userInput, 0, 2);
grid.getChildren().addAll(upperButton, lowerButton, userInput);
//SCENE
Scene myScene = new Scene(grid, 300, 250);
window.setScene(myScene);
window.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}

Related

JavaFX Dynamic Form Field UI

Does anyone know how to imitate the functionality from the UI components shown below? I want to replicate adding form fields when text is entered into the TextField box. I don't need the dropdown button, just the dynamic adding of the forms.
You could modify the children of a GridPane adding a new TextField & Button every time one of the buttons is activated. Listen to the text properties to enable/disable the Button and save the results.
private static void insertRow(GridPane grid, List<String> values, int index) {
// increment index of children with rowIndex >= index
for (Node n : grid.getChildren()) {
int row = GridPane.getRowIndex(n);
if (row >= index) {
GridPane.setRowIndex(n, row + 1);
}
}
TextField text = new TextField();
Button add = new Button("+");
add.setDisable(true);
add.setOnAction(evt -> {
insertRow(grid, values, GridPane.getRowIndex(add) + 1);
});
values.add(index, "");
text.textProperty().addListener((a, oldValue, newValue) -> {
add.setDisable(newValue.isEmpty());
values.set(GridPane.getRowIndex(add), newValue);
});
grid.addRow(index, text, add);
}
#Override
public void start(Stage primaryStage) throws Exception {
GridPane grid = new GridPane();
List<String> list = new ArrayList<>();
insertRow(grid, list, 0);
Button print = new Button("print");
print.setOnAction(evt -> {
System.out.println(list);
});
grid.add(print, 0, 1);
Scene scene = new Scene(grid, 300, 500);
primaryStage.setScene(scene);
primaryStage.show();
}
This may not be exactly what you're looking for and may not be the best way to do this, but should be easy to adapt it to your needs.
Basically, you will need a list of HBox objects to be added to a VBox in your application. You could create the list yourself and bind it to the children of your VBox, or just add/remove the HBoxes to/from the VBox using the getChildren().add() and getChildren().remove() methods.
Here is a complete little application to demonstrate the concept. I created an internal class to handle the HBox with the fields you need. This could be adapted to be more felixable:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
private static VBox mainPane;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
mainPane = new VBox(5);
mainPane.setPadding(new Insets(10));
mainPane.setAlignment(Pos.TOP_CENTER);
mainPane.getChildren().add(new UIForms());
primaryStage.setScene(new Scene(mainPane));
primaryStage.show();
}
static void addField() {
mainPane.getChildren().add(new UIForms());
}
static void removeField(UIForms field) {
if (mainPane.getChildren().size() > 1) {
mainPane.getChildren().remove(field);
}
}
}
class UIForms extends HBox {
private TextField textField1;
private TextField textField2;
private Button btnAddField;
private Button btnRemoveField;
public UIForms() {
// Setup the HBox layout
setAlignment(Pos.CENTER_LEFT);
setSpacing(5);
// Create the UI controls
textField1 = new TextField();
textField2 = new TextField();
btnAddField = new Button("+");
btnRemoveField = new Button("-");
// Setup button actions
btnAddField.setOnAction(e -> Main.addField());
btnRemoveField.setOnAction(e -> Main.removeField(this));
// Add the UI controls
getChildren().addAll(
textField1, textField2, btnAddField, btnRemoveField
);
}
}

How to control the overlapping expansion of a JavaFX titledpane

Would anyone know how to configure a JavaFX titledPane in such a way that the expansion always takes place in the foreground? As is the case with the expansion of a ComboBox button.
When the region of the panel containing the titledPane is smaller than the expansion region, the expansion occurs behind the invaded area. How to do that in front of the invaded region.
The sample code shows this scenario. The functionality I wish could be implemented with the use of a comboBox button containing buttons, but I like the effect created in the titledPane expansion and would like to use it alternately.
Thank you in advance.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
TitledPane titledPane = new TitledPane();
titledPane.setPrefWidth(180);
titledPane.setBorder(new Border(new BorderStroke(Color.MAGENTA, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, new BorderWidths(1))));
Button btnSave = new Button();
btnSave.setPrefWidth(180);
btnSave.setText("Save");
btnSave.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
titledPane.setExpanded(false);
}
});
Button btnLeftSend = new Button();
btnLeftSend.setPrefWidth(180);
btnLeftSend.setText("Sent to Left");
btnLeftSend.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
titledPane.setExpanded(false);
}
});
Button btnRightSend = new Button();
btnRightSend.setPrefWidth(180);
btnRightSend.setText("Sent to right");
btnRightSend.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
titledPane.setExpanded(false);
}
});
VBox buttons = new VBox(3);
buttons.setPrefWidth(180);
buttons.getChildren().addAll(btnSave, btnLeftSend, btnRightSend);
titledPane.setAnimated(true);
titledPane.setText("Options");
titledPane.setContent(buttons);
titledPane.setExpanded(false);
Button btnLine1 = new Button();
btnLine1.setPrefWidth(180);
btnLine1.setText("Force expand");
btnLine1.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
titledPane.setExpanded(true);
}
});
HBox line1 = new HBox(5);
line1.setPadding(new Insets(5, 0, 0, 0));
line1.setAlignment(Pos.TOP_CENTER);
line1.setMaxSize(380, 40);
line1.setMinSize(380, 40);
line1.setPrefSize(380, 40);
line1.setBorder(new Border(new BorderStroke(Color.BLUE, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, new BorderWidths(1))));
line1.getChildren().addAll(btnLine1, titledPane);
Button btnLine2 = new Button();
btnLine2.setPrefWidth(180);
btnLine2.setText("Force shrink");
btnLine2.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
titledPane.setExpanded(false);
}
});
HBox line2 = new HBox(5);
line2.setPadding(new Insets(5, 0, 0, 0));
line2.setAlignment(Pos.TOP_CENTER);
line2.setMaxSize(380, 40);
line2.setMinSize(380, 40);
line2.setPrefSize(380, 40);
line2.setBorder(new Border(new BorderStroke(Color.RED, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, new BorderWidths(1))));
line2.getChildren().add(btnLine2);
VBox layout = new VBox(5);
layout.setPadding(new Insets(5));
layout.getChildren().addAll(line1, line2);
Scene scene = new Scene(layout, 390, 160);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
scene.setFill(Color.GHOSTWHITE);
primaryStage.setTitle("Sample code for StackOverFlow");
primaryStage.setScene(scene);
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
The z-order of nodes is controlled by the order in which they are added to their parent. If you use a VBox, of course, this order also determines the position of the node within the VBox. Consider instead using, for example, a grid pane, so you can control the position independently of the order in which the child nodes are added. Then just add line1 after adding line2:
// VBox layout = new VBox(5);
// layout.setPadding(new Insets(5));
// layout.getChildren().addAll(line1, line2);
GridPane layout = new GridPane();
layout.setVgap(5);
layout.setPadding(new Insets(5));
layout.add(line2, 0, 1);
layout.add(line1, 0, 0);
Many thanks for the reply. I replaced the VBox with GridPane in my application and it worked as I wanted it to.
For the purpose of helping other people with the same problem, follow the modified code at the suggestion of James_D.
package application;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
try {
TitledPane titledPane = new TitledPane();
titledPane.setPrefWidth(180);
Button btnSave = new Button();
btnSave.setPrefWidth(180);
btnSave.setText("Save");
btnSave.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
titledPane.setExpanded(false);
}
});
Button btnLeftSend = new Button();
btnLeftSend.setPrefWidth(180);
btnLeftSend.setText("Sent to Left");
btnLeftSend.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
titledPane.setExpanded(false);
}
});
Button btnRightSend = new Button();
btnRightSend.setPrefWidth(180);
btnRightSend.setText("Sent to right");
btnRightSend.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
titledPane.setExpanded(false);
}
});
VBox buttons = new VBox(3);
buttons.setPrefWidth(180);
buttons.getChildren().addAll(btnSave, btnLeftSend, btnRightSend);
titledPane.setAnimated(true);
titledPane.setText("Options");
titledPane.setContent(buttons);
titledPane.setExpanded(false);
Button btnLine1 = new Button();
btnLine1.setPrefWidth(180);
btnLine1.setText("Force expand");
btnLine1.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
titledPane.setExpanded(true);
}
});
HBox line1 = new HBox(5);
line1.setPadding(new Insets(5, 0, 0, 0));
line1.setAlignment(Pos.TOP_CENTER);
line1.setMaxSize(380, 40);
line1.setMinSize(380, 40);
line1.setPrefSize(380, 40);
line1.getChildren().addAll(btnLine1, titledPane);
Button btnDummy = new Button();
btnDummy.setPrefWidth(365);
btnDummy.setText("Anything here. Just an example");
HBox line2 = new HBox(5);
line2.setPadding(new Insets(5, 0, 0, 0));
line2.setAlignment(Pos.TOP_CENTER);
line2.setMaxSize(380, 40);
line2.setMinSize(380, 40);
line2.setPrefSize(380, 40);
line2.getChildren().add(btnDummy);
GridPane layout = new GridPane();
layout.setVgap(5);
layout.setPadding(new Insets(5));
layout.add(line2, 0, 1);
layout.add(line1, 0, 0);
Scene scene = new Scene(layout, 390, 160);
primaryStage.setTitle("Sample code for StackOverFlow");
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}

Q: Confused about switching screens JavaFX

I dont understand how to switch between the current screen and the register screen:
package Computing;
import javafx.scene.layout.*;import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.stage.Window;
public class Computing extends Application {
Stage stage;
#Override
public void start(Stage primaryStage) throws Exception {
stage = primaryStage;
primaryStage.setTitle("Computing Quiz");
GridPane gridPane = createComputingQuizPane();
// ui controls to gridpane
addUIControls(gridPane);
// scene
Scene scene = new Scene(gridPane, 800, 500);
// primary stage scene set
primaryStage.setScene(scene);
primaryStage.show();
}
private GridPane createComputingQuizPane() {
// Instantiate a new Grid Pane
GridPane gridPane = new GridPane();
gridPane.setAlignment(Pos.CENTER);
gridPane.setPadding(new Insets(40, 40, 40, 40));
gridPane.setHgap(10);
gridPane.setVgap(10);
return gridPane;
}
private void addUIControls(GridPane gridPane) {
// HEADER
Label headerLabel = new Label("Welcome to the Computing Quiz!");
headerLabel.setFont(Font.font("Arial", FontWeight.BOLD, 24));
gridPane.add(headerLabel, 0,0,2,1);
GridPane.setHalignment(headerLabel, HPos.CENTER);
GridPane.setMargin(headerLabel, new Insets(20, 0,20,0));
// LOGIN BUTTON
Button loginButton = new Button("Login");
loginButton.setPrefHeight(50);
loginButton.setDefaultButton(true);
loginButton.setPrefWidth(125);
gridPane.add(loginButton, 0, 4, 2, 1);
GridPane.setHalignment(loginButton, HPos.CENTER);
GridPane.setMargin(loginButton, new Insets(20, 0,20,0));
// REGISTER BUTTON
Button registerButton = new Button ("Register");
registerButton.setOnAction(e-> buttonClick());
registerButton.setPrefHeight(50);
registerButton.setDefaultButton(true);
registerButton.setPrefWidth(125);
gridPane.add(registerButton,0,5,2,1);
GridPane.setHalignment(registerButton,HPos.CENTER);
GridPane.setMargin(registerButton, new Insets(20,0,20,0));
//registerButton.setOnAction(new EventHandler<ActionEvent>() {
//#Override
//public void handle(ActionEvent event) {
//Register nc = new Register(stage);
//}
//});
}
public void buttonClick(){
Register nc = new Register(stage);
}
public static void main(String[] args) {
launch(args);
}
}
This is the register screen:
package Computing;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import javafx.stage.Window;
public class Register {
Stage stage;
public Register (Stage s){
stage = s;
}
public void start(Stage primaryStage){
primaryStage.setTitle("Registration");
GridPane gridPane = createRegisterPane();
addUIControls(gridPane);
Scene scene = new Scene(gridPane, 800, 500);
primaryStage.setScene(scene);
primaryStage.show();
}
private GridPane createRegisterPane() {
GridPane gridPane = new GridPane();
gridPane.setAlignment(Pos.CENTER);
gridPane.setPadding(new Insets(40, 40, 40, 40));
gridPane.setHgap(10);
gridPane.setVgap(10);
return gridPane;
}
private void addUIControls(GridPane gridPane) {
// Add Header
Label headerLabel = new Label("Registration Form");
headerLabel.setFont(Font.font("Arial", FontWeight.BOLD, 24));
gridPane.add(headerLabel, 0,0,2,1);
GridPane.setHalignment(headerLabel, HPos.CENTER);
GridPane.setMargin(headerLabel, new Insets(20, 0,20,0));
// FULL NAME
Label nameLabel = new Label("Full Name : ");
gridPane.add(nameLabel, 0,1);
// FULL NAME TEXT
TextField nameField = new TextField();
nameField.setPrefHeight(40);
gridPane.add(nameField, 1,1);
// USERNAME
Label emailLabel = new Label("Username : ");
gridPane.add(emailLabel, 0, 2);
// EMAIL TEXT
TextField emailField = new TextField();
emailField.setPrefHeight(40);
gridPane.add(emailField, 1, 2);
// PASSWORD
Label passwordLabel = new Label("Password : ");
gridPane.add(passwordLabel, 0, 3);
// PASSWORD TEXT
PasswordField passwordField = new PasswordField();
passwordField.setPrefHeight(40);
gridPane.add(passwordField, 1, 3);
// SUBMIT BUTTON
Button submitButton = new Button("Submit");
submitButton.setPrefHeight(40);
submitButton.setDefaultButton(true);
submitButton.setPrefWidth(100);
gridPane.add(submitButton, 0, 4, 2, 1);
GridPane.setHalignment(submitButton, HPos.CENTER);
GridPane.setMargin(submitButton, new Insets(20, 0,20,0));
submitButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
if(nameField.getText().isEmpty()) {
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "Error", "Please enter your name.");
return;
}
if(emailField.getText().isEmpty()) {
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "Error", "Please enter a Username");
return;
}
if(passwordField.getText().isEmpty()) {
showAlert(Alert.AlertType.ERROR, gridPane.getScene().getWindow(), "Error!", "Please enter a password");
return;
}
showAlert(Alert.AlertType.CONFIRMATION, gridPane.getScene().getWindow(), "Registration Successful!", "Welcome " + nameField.getText());
}
});
}
private void showAlert(Alert.AlertType alertType, Window owner, String title, String message) {
Alert alert = new Alert(alertType);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(message);
alert.initOwner(owner);
alert.show();
}
public static void main(String[] args) {
launch(args);
}
}
I've tried using this instead of the buttonClick method also I'm slightly confused as to why it won't work as before I used the same method using buttonClick to send the scene to the next screen
submitButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {

Textarea scrollpane hold in text append

It is my test code of textarea append text,
public class TextAreaScrollHold extends Application {
TextArea area = new TextArea();
#Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
root.getChildren().add(area);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
addTextInTextArea();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public void addTextInTextArea() {
for (int i = 0; i < 15; i++) {
area.appendText("Hello World " + i + "\n");
}
Task<Void> task = new Task() {
#Override
protected Void call() throws Exception {
for (int i = 15; i < 100; i++) {
area.appendText("Hello World " + i + "\n");
Thread.sleep(1000);
}
return null;
}
};
new Thread(task).start();
}
}
It my code data will update in thread. i need how to hold in scroll bar when data update in textarea. I have ref JavaFX TextArea and autoscroll and Access to TextArea's Scroll Pane or Scroll Bars but how solve this problems.
I need
When data update in textarea, i will scroll the text area scrollbar the bar will hold.
textArea.scrollTopProperty().addListener(new ChangeListener<Number>() {
#Override
public void changed(ObservableValue<? extends Number> observable, Number oldValue, Number newValue) {
textArea.setScrollTop(100);
}
});
I have used this code but scroll bar in not moved bar will fixed in pixel 100 positions
You can use getCaretPostion and postionCaret (yes, that setter's method name is awkward for Java).
I quickly drafted up some code for you, use the scroll lock button to enable/disable scrolling:
import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class ConsoleDemo extends Application {
Console console = new Console();
#Override
public void start(Stage primaryStage) {
StackPane root = new StackPane();
root.getChildren().add(console);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Console Demo");
primaryStage.setScene(scene);
primaryStage.show();
addTextInTextArea();
}
/**
* #param args
* the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public void addTextInTextArea() {
for (int i = 0; i < 15; i++) {
console.log("Hello World " + i);
}
Task<Void> task = new Task() {
#Override
protected Void call() throws Exception {
for (int i = 15; i < 100; i++) {
console.log("Hello World " + i);
Thread.sleep(1000);
}
return null;
}
};
new Thread(task).start();
}
public class Console extends BorderPane {
TextArea textArea = new TextArea();
int scrollLockPos = -1;
public Console() {
HBox toolbar = new HBox();
ToggleButton scrollLockButton = new ToggleButton("Scroll Lock");
scrollLockButton.setOnAction(e -> {
if (scrollLockButton.isSelected()) {
scrollLockPos = textArea.getCaretPosition();
} else {
scrollLockPos = -1;
}
});
HBox.setMargin(scrollLockButton, new Insets(5, 5, 5, 5));
toolbar.getChildren().add(scrollLockButton);
setCenter(textArea);
setTop(toolbar);
}
public void log(String text) {
textArea.appendText(text + "\n");
if (scrollLockPos != -1) {
textArea.positionCaret(scrollLockPos);
}
}
}
}
Not the nicest solution, but unless you want to use selection in the textarea while it's scrolling is locked, this one works. For a proper solution you'd need access to the skin / scrollpane / scrollbars and with the upcoming Java 9 version and its modularization you don't know what you will have access to since access to them is currently flagged as "restricted".
Edit:
Here's an alternate solution which uses the Range, console component only. With this version you can select text and keep the selection while the Scroll Lock button is down:
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.IndexRange;
import javafx.scene.control.TextArea;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
/**
* Console which provides a mechanism to lock scrolling. Selecting text and copying it works while scrolling is locked.
*/
public class Console extends BorderPane {
TextArea textArea = new TextArea();
ToggleButton scrollLockButton;
IndexRange range;
public Console() {
initComponents();
}
private void initComponents() {
// toolbar
HBox toolbar = new HBox();
toolbar.setAlignment(Pos.CENTER_RIGHT);
// clear
Button clearButton = new Button("Clear");
clearButton.setOnAction(e -> {
textArea.clear();
});
// scroll lock
scrollLockButton = new ToggleButton("Scroll Lock");
// button positions & layout
Insets insets = new Insets(5, 5, 5, 5);
HBox.setMargin(clearButton, insets);
HBox.setMargin(scrollLockButton, insets);
toolbar.getChildren().addAll(clearButton,scrollLockButton);
// component layout
setCenter(textArea);
setTop(toolbar);
}
public void log(String text) {
if (scrollLockButton.isSelected()) {
range = textArea.getSelection();
}
textArea.appendText(text + "\n");
if (scrollLockButton.isSelected()) {
textArea.selectRange(range.getStart(), range.getEnd());
}
}
}

javafx multiple buttons to same handler

I try to make a simple calculator with 20 buttons and one handler. In java I can use 'if' statement with event.getSource() in ActionPerformed to check which button is pressed, but it doesn't work with handler in javafx. Is it possible in javafx that all buttons has one handler? (I don't want to use java 8 Lambdas.)
Last time I tried with setId/getId but it same not work (to me).
public class Calculator extends Application {
public Button b0, b1;
#Override
public void start(Stage primaryStage) {
GridPane grid = new GridPane();
b0 = new Button("0");
b0.setId("0");
b0.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
grid.add(b0, 0, 1);
b0.setOnAction(myHandler);
b1 = new Button("1");
b1.setId("1");
b1.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
grid.add(b1, 0, 0);
b1.setOnAction(myHandler);
Scene scene = new Scene(grid, 365, 300);
scene.getStylesheets().add
(Calculator.class.getResource("calculator.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}
final EventHandler<ActionEvent> myHandler = new EventHandler<ActionEvent>(){
#Override
public void handle(final ActionEvent event) {
Button x = (Button) event.getSource();
if (x.getId().equals(b0.getId()))
System.out.println("0");
else if(x.getId().equals(b1.getId()))
System.out.println("1");
}
};
public static void main(String[] args) {
launch(args);
}
}
I tested your code and it seems to work just fine.
There's no real reason to test the ids of the buttons, though. If you really want to use the same handler (which I don't advise), just test for equality between each button and the source of the event:
final EventHandler<ActionEvent> myHandler = new EventHandler<ActionEvent>(){
#Override
public void handle(final ActionEvent event) {
if (event.getSource() == b0)
System.out.println("0");
else if(event.getSource() == b1)
System.out.println("1");
}
};
But it's (almost?) always better to use a different handler for each action. It keeps the code free of all the if/else constructs, which both makes it cleaner and better in terms of performance. Here, since your buttons do almost the same thing, you can use a single implementation but multiple objects.
Here's a complete example:
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class Calculator extends Application {
private final IntegerProperty value = new SimpleIntegerProperty();
class NumberButtonHandler implements EventHandler<ActionEvent> {
private final int number ;
NumberButtonHandler(int number) {
this.number = number ;
}
#Override
public void handle(ActionEvent event) {
value.set(value.get() * 10 + number);
}
}
#Override
public void start(Stage primaryStage) {
GridPane grid = createGrid();
for (int n = 1; n<10; n++) {
Button button = createNumberButton(n);
int row = (n-1) / 3;
int col = (n-1) % 3 ;
grid.add(button, col, 2 - row);
}
Button zeroButton = createNumberButton(0);
grid.add(zeroButton, 1, 3);
Button clearButton = createButton("C");
// without lambdas:
// clearButton.setOnAction(
// new EventHandler<ActionEvent>() {
// #Override
// public void handle(ActionEvent event) {
// value.set(0);
// }
// }
// );
// with lambdas:
clearButton.setOnAction(event -> value.set(0));
grid.add(clearButton, 2, 3);
TextField displayField = createDisplayField();
BorderPane root = new BorderPane();
root.setPadding(new Insets(10));
root.setTop(displayField);
root.setCenter(grid);
Scene scene = new Scene(root, 365, 300);
primaryStage.setScene(scene);
primaryStage.setResizable(false);
primaryStage.show();
}
private Button createNumberButton(int number) {
Button button = createButton(Integer.toString(number));
button.setOnAction(new NumberButtonHandler(number));
return button ;
}
private Button createButton(String text) {
Button button = new Button(text);
button.setMaxSize(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
GridPane.setFillHeight(button, true);
GridPane.setFillWidth(button, true);
GridPane.setHgrow(button, Priority.ALWAYS);
GridPane.setVgrow(button, Priority.ALWAYS);
return button ;
}
private GridPane createGrid() {
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(5);
grid.setVgap(5);
grid.setPadding(new Insets(10));
return grid;
}
private TextField createDisplayField() {
TextField displayField = new TextField();
displayField.textProperty().bind(Bindings.format("%d", value));
displayField.setEditable(false);
displayField.setAlignment(Pos.CENTER_RIGHT);
return displayField;
}
public static void main(String[] args) {
launch(args);
}
}

Resources