how know pressed key is character only in javafx - javafx

how know pressed key from Key Board is character only in javafx.
i want to handle following condition
if(event.isControlDown() && event.getCode().ISCHARACTERKEY()){
// some Code
}
ISCHARACTERKEY() include A-Z or a-z only.
is Javafx provide ISCHARACTERKEY() type of inbuilt method?

Yes, it does:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package keycodetester;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* #author ottp
*/
public class KeyCodeTester extends Application {
#Override
public void start(Stage primaryStage) {
TextField tf = new TextField();
tf.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
if(event.isAltDown() && event.getCode().isLetterKey()) {
System.out.println("Character");
}
}
});
StackPane root = new StackPane();
root.getChildren().add(tf);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
event.getCode().isLetterKey() is your method..
Patrick

Related

JavaFX ComboBox check input and change it

I am trying to build a Combobox in JavaFX that should work as followed.
The user should only type numbers in, but the typed numbers have to be formattet.
ex. 111-111-1111.
So if the user types in three numbers a - should be added automatically.
I figured out how to do that.
I blocked everything but numbers with a TexFormatter.
Now the main Part of the problem comes the part after this.
I added a keyevent.key_released place the caret at the end when a key is released.
But if the user is typing too fast it won't work.
Most of the time it works just fine like that, but when is the user every doing something only the way I expect?
I could not find another way to get the actual value of the combobox, because it seems to refresh after hitting enter or so.
Adding the - in the Textformatter resultet in the programm listening to itself and I wasn't able to place the caret at the end position.
import java.util.function.UnaryOperator;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextFormatter;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
/**
*
* #author
*/
public class tester extends Application{
ComboBox<String> combo = new ComboBox<String>();
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
System.out.println("test");
HBox root = new HBox();
//
UnaryOperator<TextFormatter.Change> filter = new UnaryOperator<TextFormatter.Change>() {
#Override
public TextFormatter.Change apply(TextFormatter.Change t) {
System.out.println(t.getText());
if (t.isReplaced())
if(t.getText().matches("[^0-9]")) {
t.setText(t.getControlText().substring(t.getRangeStart(), t.getRangeEnd()));}
if (t.isAdded()) {
// Add in Formatter //
if (t.getControlText().length() == 2 || t.getControlText().length() == 6 && t.getText().matches("[0-9]")){
t.setText(t.getText() + "-");
combo.getEditor().end();
}
//
if (t.getText().matches("[^0-9]")) {
t.setText("");}
}
return t;
}
};
combo.setEditable(true);
combo.getEditor().setTextFormatter(new TextFormatter<>(filter));
combo.getEditor().addEventFilter(KeyEvent.KEY_RELEASED, e -> {
combo.getEditor().end();
});
root.getChildren().add(combo);
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
}

Netbeans 9 0 Java FX 10

Try creating a JavaFX application.
Don't add or modify any code.
Run the Project. It does not find the MAIN.
If you run the File, then it works.
However, create JavaFX FXML application. As mentioned earlier, don't make any change to the code.
Run the Package.... it WORKS.
Does anyone know why? I tried to report the problem, but that looked more complicated than writing the code. :)
I am enclosing generated code for a JAVAFX APPLICATION
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package forsubmission;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
* #author Hornigold
*/
public class ForSubmission extends Application {
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
Thanks,
Gold

JavaFX - Turning off possible focus on TextArea

I have TextArea and TextField in my app. I have managed to give focus to TextField on the start and made TextArea impossible to edit. I want to also somehow turn off possibility to focus it with forexample mouse click or TAB cycling.
Is there any suitable way to do it?
You need to use:
textArea.setFocusTraversable(false);
textArea.setMouseTransparent(true);
Example demonstration :
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
/**
* #author StackOverFlow
*
*/
public class Sample2 extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
BorderPane pane = new BorderPane();
// Label
TextArea textArea1 = new TextArea("I am the focus owner");
textArea1.setPrefSize(100, 50);
// Area
TextArea textArea2 = new TextArea("Can't be focused ");
textArea2.setFocusTraversable(false);
textArea2.setMouseTransparent(true);
textArea2.setEditable(false);
// Add the items
pane.setLeft(textArea1);
pane.setRight(textArea2);
// Scene
Scene scene = new Scene(pane, 200, 200);
primaryStage.setScene(scene);
// Show stage
primaryStage.show();
}
/**
* Application Main Method
*
* #param args
*/
public static void main(String[] args) {
launch(args);
}

Error : Running array of objects in JavaFX with same method

Here's the code :
Main File :
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package testobjectarray;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
/**
*
*
*/
public class TestObjectArray extends Application {
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* The main() method is ignored in correctly deployed JavaFX application. main() serves only as fallback in case the application can not be launched through deployment artifacts, e.g., in IDEs
* with limited FX support. NetBeans ignores main().
*
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
myClass[] c = new myClass[5];
c[2].myMethod();
}
}
Class declared outside :
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package testobjectarray;
/**
*
*
*/
public class myClass {
public void myMethod(){
System.out.println("Inside myMethod");
}
}
Problem, I get errors when I compile i.e. initializing an array of objects and invoking the method. If I have just one object, it works great. Any help appreciated.
The problem with your code is you are initialising an array of size 5, but you never add values to it. Try the following code
myClass[] c = new myClass[5];
c[0] = new myClass();
c[1] = new myClass(); // and so on till c[4]
//now you can call the methods
c[1].myMethod();

using modality in a popup window javafx

I kept a popup dialog on a pane and it came at the top of the other components. Now I want to disable accessing all the other components of the program. How to do it?
The popup API does not have an initModality(Modality.APPLICATION_MODAL); method which is exactly what you want. In this case, you can make your popup window a stage and use the method mentioned above.
This is for #Xsleek in solution, example code:-
package popupexample;
import java.text.SimpleDateFormat;
import java.util.Date;
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.layout.StackPane;
import javafx.scene.layout.VBoxBuilder;
import javafx.scene.text.Text;
import javafx.stage.Modality;
import javafx.stage.Stage;
/**
*
* #author reegan
*/
public class PopUpExample extends Application {
#Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Say 'Hello World'");
btn.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
popupErrorMsg();
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public void popupErrorMsg() {
final Stage myDialog = new Stage();
myDialog.initModality(Modality.APPLICATION_MODAL);
Button okButton = new Button("Ok");
okButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
myDialog.close();
}
});
Date todayDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Scene myDialogScene = new Scene(VBoxBuilder.create()
.children(new Text("Please Enter Validate Date \n \t "+ dateFormat.format(todayDate)), okButton)
.spacing(30)
.alignment(Pos.CENTER)
.padding(new Insets(10))
.build());
myDialog.setScene(myDialogScene);
myDialog.show();
}
}

Resources