TextField right alignment issue javafx - java

I have textfield that containing a value and that value should be Right alignment.
When i run the application it display the text left alignment but i set the Right alignment.
and problem with 3rd textfield.
After clicking the update button it works fine.
So may i know why it is behave different.
Code:
import java.io.File;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
public class TextFieldAlignment extends Application {
TextField rText;
File file;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
rText = new TextField("updated right1 updated right2 updated right3 updated right4");
rText.setAlignment(Pos.CENTER_RIGHT);
Button btn = new Button("update");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
rText.setText("updated right1 updated right2 updated right3 updated right4");
// applyWorkaround();
}
});
final Label labelFile = new Label();
Button btn2 = new Button();
btn2.setText("Open FileChooser'");
btn2.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("AVI files (*.exml)", "*.exml");
fileChooser.getExtensionFilters().add(extFilter);
//Show open file dialog
file = fileChooser.showOpenDialog(null);
// rText.setText(file.getPath());
}
});
VBox root = new VBox();
root.getChildren().addAll(rText, btn, btn2);
stage.setScene(new Scene(root, 200, 300));
stage.show();
}
}

Put the setalignment and settext after you add the TextField to the Scene.
Ref: Java API docs for Node
Node objects may be constructed and modified on any thread as long
they are not yet attached to a Scene. An application must attach nodes
to a Scene, and modify nodes that are already attached to a Scene, on
the JavaFX Application Thread.

Related

How to properly produce a label when a button is clicked [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
In the code below I have a project that creates 3 buttons called sinister, medium, and dexter. The buttons are properly displayed but are not functional. How do I get the button called sinister in the program to display a label saying “left” when clicked?
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.control.Label;
import javafx.scene.control.Button;
import javafx.geometry.Pos;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
public class latinTranslator extends Application
{
private Label myLabel;
public static void main(String[] args)
{
launch(args);
}
#Override
public void start(Stage primaryStage) {
Button leftButton = new Button("Sinister");
Button centerButton = new Button("Medium");
Button rightButton = new Button("Dexter");
//align centerbutton
centerButton.setAlignment(Pos.CENTER);
//event handler
leftButton.setOnAction(new ButttonClickHandler());
centerButton.setOnAction(new ButttonClickHandler());
rightButton.setOnAction(new ButttonClickHandler());
//label, button and its spacing;
VBox Vbox = new VBox(20,leftButton, centerButton,rightButton );
//create scene
Scene scenebox = new Scene(Vbox,300,500);
//alignment
Vbox.setAlignment(Pos.CENTER);
primaryStage.setScene(scenebox);
//set scene to stage
primaryStage.setTitle("Latin Translator");
//window
primaryStage.show();
}
class ButttonClickHandler implements EventHandler<ActionEvent>
{
#Override
public void handle(ActionEvent event)
{
}
}
}
First of all, you need to add myLabel to the scene graph. Then, in the event handler, you can set the text of myLabel.
In the below code, I have indicated changes I made in method start with comments. Look for the following comments:
ADDED THIS LINE
CHANGE HERE
I have also added the code for method handle in class ButttonClickHandler
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.control.Label;
import javafx.scene.control.Button;
import javafx.geometry.Pos;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
public class LatinTranslator extends Application {
private Label myLabel;
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
myLabel = new Label(); // ADDED THIS LINE
Button leftButton = new Button("Sinister");
Button centerButton = new Button("Medium");
Button rightButton = new Button("Dexter");
// align centerbutton
centerButton.setAlignment(Pos.CENTER);
// event handler
leftButton.setOnAction(new ButttonClickHandler());
centerButton.setOnAction(new ButttonClickHandler());
rightButton.setOnAction(new ButttonClickHandler());
// label, button and its spacing;
VBox Vbox = new VBox(20, myLabel, leftButton, centerButton, rightButton); // CHANGE HERE
// create scene
Scene scenebox = new Scene(Vbox, 300, 500);
// alignment
Vbox.setAlignment(Pos.CENTER);
primaryStage.setScene(scenebox);
// set scene to stage
primaryStage.setTitle("Latin Translator");
// window
primaryStage.show();
}
class ButttonClickHandler implements EventHandler<ActionEvent> {
#Override
public void handle(ActionEvent event) {
Object source = event.getSource();
if (source instanceof Button) {
Button button = (Button) source;
String text = button.getText();
if ("Sinister".equals(text)) {
myLabel.setText("left");
}
}
}
}
}
Run the code and click on button Sinister and the word left will appear above the button.
This is how it looks before clicking button Sinister
And this is how it looks after clicking button Sinister

JFXtras MonologFX - How to detect which button was pressed

I was testing the MonologFX from JFXtras (v8.0-r5), but I got stuck with it!
Can anyone tell me how to check what was the button in the dialog that was pressed by the user? I tried in many ways, but no luck at all.
package javafx_jfxtras_monologfx;
import javafx.application.Application;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
import jfxtras.labs.dialogs.MonologFX;
import jfxtras.labs.dialogs.MonologFXButton;
import jfxtras.labs.dialogs.MonologFX.Type;
public class JavaFX_JFXtras_MonologFX extends Application
{
#Override
public void start(Stage stage)
{
MonologFX m = new MonologFX();
m.setModal(true);
m.setType(Type.QUESTION);
m.setTitleText("JFXtras MonologFX");
m.setMessage("Do you want to continue?");
m.setPos(698, 450);
MonologFXButton mb1 = new MonologFXButton();
mb1.setType(MonologFXButton.Type.YES);
mb1.setLabel("Continue");
m.addButton(mb1);
MonologFXButton mb2 = new MonologFXButton();
mb2.setType(MonologFXButton.Type.NO);
mb2.setLabel("Exit");
m.addButton(mb2);
Button btn = new Button();
btn.setText("Click the Button");
btn.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
System.out.println("Hello :)");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
stage.setTitle("JavaFX - JFXtras MonologFX");
stage.setScene(scene);
stage.show();
m.show();
}
public static void main(String[] args)
{
launch(args);
}
}
But the controls in labs are experimental and Mark has not worked on this one for a long time. We don't take them out because someone may use them, but as of version 8u40 JavaFX has a dialog itself. https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Dialog.html|

TextField - getText() wont work

I have this code sample that implement input mask to TextField:
package com.example;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage stage) {
TextField field = new TextField() {
#Override public void replaceText(int start, int end, String text) {
super.replaceText(start, end, "#");
}
};
Button button = new Button("Show Text");
button.setOnAction(new EventHandler<ActionEvent>(){
#Override
public void handle(ActionEvent event) {
System.out.println("TEXT: " + field.getText());
}
});
VBox root = new VBox(20);
root.setAlignment(Pos.CENTER);
root.getChildren().addAll(field, button);
Scene scene = new Scene(root, 500, 500);
stage.setScene(scene);
stage.show();
}
}
Whenever I type the word "LIFE" in the TextField and press the button, the output always returns TEXT: ####, I want the output to return TEXT: LIFE. It seems like getText is not working. How to fix this?
Your approach doesn't work, because the replaceText method calls setText; so the implementation in your subclass causes the text field's textProperty to contain only '#' characters. Hence when you call getText(), you get a string with all '#' characters.
If you want to use a subclass of TextField like that, you would have to keep track of the "real" text elsewhere, which would get quite difficult. (Also, I think your implementation doesn't behave properly with copy and paste, and fixing that would be a bit tricky too.)
Probably the way to do this is to use a PasswordField, and replace it with a TextField when you want to show the text.
Here's a simple example:
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class RevealPasswordExample extends Application {
#Override
public void start(Stage primaryStage) {
PasswordField passwordField = new PasswordField();
TextField textField = new TextField();
passwordField.textProperty().bindBidirectional(textField.textProperty());
StackPane textContainer = new StackPane(passwordField);
CheckBox showText = new CheckBox("Show text");
showText.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {
if (isNowSelected) {
textContainer.getChildren().setAll(textField);
} else {
textContainer.getChildren().setAll(passwordField);
}
});
VBox root = new VBox(5, textContainer, showText);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(24));
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Actually getText() is working properly, that's why you got #### at the output.
The thing is you have used replaceText() method, which will replace all the characters of your text field to '#' from the start to end. I donno why u have used that. If you don't wanna show the string typed in the field try it as a password field like,
PasswordField field = new PasswordField();
field.getText();
by using this also you can get the field text but it won't be visible to the user.

Make TextField/TextArea caret visible in JavaFX GUI

I was wondering how to make the caret visible and/or flash whilst using a TextField or TextArea. The ones I have created in my GUI work as the letters appear when typed but there is no visible caret.
I've looked through the TextField documentation but none are about making it visible or not. I expected to find something along the lines "setCaretVisible(Boolean);"
Do I have to make it visible via CSS? If so, any suggestions would be most welcome!
Please see code that I've quickly put together to illustrate the problem:
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCombination;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class Test extends Application {
public static void main(String[] arguments) { launch(arguments); }
#Override public void start(Stage stage) {
Scene scene = new Scene(new Group());
scene.setRoot(new BuildLayout(stage));
stage.setTitle("Application Name");
stage.setScene(scene);
stage.initStyle(StageStyle.UNDECORATED);
stage.initStyle(StageStyle.TRANSPARENT);
stage.setFullScreen(true);
stage.setFullScreenExitHint("");
stage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
stage.show();
}
}
final class BuildLayout extends BorderPane {
protected BuildLayout(Stage stage) {
TabPane tabpane = new TabPane();
Tab tab = new Tab();
tab.setGraphic(new Text("Video Browser"));
tab.setClosable(false);
tab.setContent(new Input(1));
tabpane.getTabs().addAll(tab);
setTop(new Toolbar(stage));
setCenter(tabpane);
}
}
final class Input extends VBox {
Input(int id) {
HBox hbox = new HBox();
TextField videoTitle = new TextField("video_title");
TextArea description = new TextArea( "video_description");
Button share = new Button("share");
Button unshare = new Button("unshare");
setPadding(new Insets(5,10,10,5));
setAlignment(Pos.TOP_CENTER);
hbox.getChildren().addAll(videoTitle, new Text(" Creator: " + " Date: " + " Views: " + " "));
if(true) {
videoTitle.setEditable(true);
description.setEditable(true);
if(true) {
hbox.getChildren().add(share);
} else { hbox.getChildren().add(unshare); }
}
getChildren().addAll(hbox, description);
}
}
final class Toolbar extends HBox {
protected Toolbar(Stage stage) {
Button close = new Button("close");
close.setOnAction((ActionEvent e) -> { stage.close(); });
setAlignment(Pos.CENTER_LEFT);
getChildren().addAll(close);
}
}
Many thanks,
This problem only occurs on Mac and not Windows (untested Linux). The fix is to downgrade to a JavaFX 2.2 compliant maximised window as the setMaximized() from JavaFX 8 also isn't Mac compatible.
Modifying the start method with some code found here:
#Override public void start(Stage stage) {
Scene scene = new Scene(new Group());
scene.setRoot(new BuildLayout(stage));
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
stage.setTitle("Application Name");
stage.setScene(scene);
stage.setX(bounds.getMinX());
stage.setY(bounds.getMinY());
stage.setWidth(bounds.getWidth());
stage.setHeight(bounds.getHeight());
stage.show();
}
Produces a fullscreen application with visible and flashing caret.

JavaFx :Default Message for Empty ListView

When there is no record in any table it shows a message 'No content in table', which is by default functionality of TableView in JavaFx.
So here my question is, does the same can be possible with ListView in JavaFx ? Like, if there is no item in any ListView then it will show a message same as TableView, instead of a blank/empty fields.
You have to try this:-
listView.setPlaceholder(new Label("No Content In List"));
its 100% working....
JavaFX8 has a setPlaceholder(...) method for ListView.
In earlier versions, you need to roll your own somehow. This is a bit of a hack: it wraps the ListView in a stack pane, with a white rectangle and the placeholder displayed over the top of the list view. The placeholder and rectangle have their visible property bound, so they are only visible if the list is empty.
There may be easier ways that I'm not seeing right away...
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.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class ListViewPlaceholderTest extends Application {
#Override
public void start(Stage primaryStage) {
final ListView<String> listView = new ListView<>();
final IntegerProperty counter = new SimpleIntegerProperty();
final Button addButton = new Button("Add item");
addButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
counter.set(counter.get()+1);
listView.getItems().add("Item "+counter.get());
}
});
final Button removeButton = new Button("Remove");
removeButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
listView.getItems().remove(listView.getSelectionModel().getSelectedIndex());
}
});
removeButton.disableProperty().bind(Bindings.equal(listView.getSelectionModel().selectedIndexProperty(), -1));
final HBox buttons = new HBox(5);
buttons.setPadding(new Insets(10));
buttons.getChildren().addAll(addButton, removeButton);
final BorderPane root = new BorderPane();
root.setCenter(createPlaceholderForListView(listView, new Label("No content in List")));
root.setBottom(buttons);
final Scene scene = new Scene(root, 600, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
private Node createPlaceholderForListView(ListView<?> listView, Node placeholder) {
final StackPane pane = new StackPane();
final Rectangle rect = new Rectangle(0, 0, Color.WHITE);
rect.widthProperty().bind(listView.widthProperty());
rect.heightProperty().bind(listView.heightProperty());
pane.getChildren().addAll(listView, rect, placeholder);
placeholder.visibleProperty().bind(Bindings.isEmpty(listView.getItems()));
rect.visibleProperty().bind(placeholder.visibleProperty());
rect.setMouseTransparent(true);
return pane ;
}
}
With fxml:
<ListView fx:id="foundContentList">
<placeholder>
<Label text="Nothing found" />
</placeholder>
</ListView>
Not entirely sure but I don't think there is a setPlaceholder method(to set the default message when no content in table) for ListView.
The workaround that I use is to create an Object in the list that indicate "No content" and show that on the listview and also disable it.
For example:
ObservableList noContent= FXCollections.observableArrayList("No content found");
ListView listView = new ListView(noContent);
listView.setDisable(true);

Categories