I'm currently working on a little game with JavaFX, and i had some problems to catch keyEvents.
Now i can catch them but the program throw a java.lang.StackOverflowError and it didn't do what I expected when a key is pressed.
Here is the main class:
public class WarbladeFX extends Application {
Game root;
public void start(Stage primaryStage) {
root = new Game();
Scene scene = new Scene(root, 800, 600);
scene.setFill(new Color(0,0,0,1));
scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
public void handle(KeyEvent event) {
Event.fireEvent(root, event);
}
});
scene.setOnKeyReleased(new EventHandler<KeyEvent>() {
public void handle(KeyEvent event) {
Event.fireEvent(root, event);
}
});
primaryStage.setTitle("WarbladeFX");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
And the Game class:
public class Game extends Group {
Entity ship;
long delta;
HashSet<String> input = new HashSet<>();
public Game() {
File f = new File("src/ressources/ship.gif");
ship = new Entity(new Image("file:///" + f.getAbsolutePath().replace("\\", "/")) ,300, 300);
getChildren().add(ship);
setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
//System.out.println(".handle()");
String code = event.getCode().toString();
if(!input.contains(code))
input.add(code);
}
});
setOnKeyReleased(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
String code = event.getCode().toString();
input.remove(code);
}
});
new AnimationTimer(){
#Override
public void handle(long now) {
if(input.contains("LEFT"))
ship.setVelX(-1);
if(input.contains("RIGHT"))
ship.setVelX(1);
ship.move(now);
getChildren().clear();
getChildren().add(ship);
}
}.start();
}
}
Some help would be very great.
Events fired on nodes in the scene graph will "bubble up" to the parent, and then the parent of the parent, and eventually all the way up to the scene. So the event handlers that you defined on the scene "refire" the event to the root, which will then bubble up to the scene and get handled again, being refired again to the root, and so on...
If you want to catch the events anywhere in the scene, and then handle them in the Game class, define some methods in Game to process the events and invoke those methods. Don't "refire" the events.
For example:
public class Game extends Group {
Entity ship;
long delta;
HashSet<String> input = new HashSet<>();
public Game() {
File f = new File("src/ressources/ship.gif");
ship = new Entity(new Image("file:///" + f.getAbsolutePath().replace("\\", "/")) ,300, 300);
getChildren().add(ship);
new AnimationTimer(){
#Override
public void handle(long now) {
if(input.contains("LEFT"))
ship.setVelX(-1);
if(input.contains("RIGHT"))
ship.setVelX(1);
ship.move(now);
getChildren().clear();
getChildren().add(ship);
}
}.start();
}
public void keyDown(String key) {
if(!input.contains(key))
input.add(key);
}
public void keyUp(String key) {
input.remove(code);
}
}
and then you can do
public class WarbladeFX extends Application {
Game root;
public void start(Stage primaryStage) {
root = new Game();
Scene scene = new Scene(root, 800, 600);
scene.setFill(new Color(0,0,0,1));
scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
public void handle(KeyEvent event) {
game.keyDown(event.getCode().toString());
}
});
scene.setOnKeyReleased(new EventHandler<KeyEvent>() {
public void handle(KeyEvent event) {
game.keyUp(event.getCode().toString());
}
});
primaryStage.setTitle("WarbladeFX");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
you are generating an infinite loop when you fire the event again in the event-handle method. Try to handle the event in this method, i.e. react to the users input.
Related
I'm writing JavaFX application in order to send from one controller to other controller. I use EventBus which was written by developer. I download it from github.But When I try to recall from one controller to other controller. First time it works once. Second time it works twice. Third time it works three times and so on. What might be reason of behaving this eventbus?
MainController
here Event bus was registered like static
public class Main extends Application
{
public static EventBus eventBus = new FxEventBus();
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
This controller class which was fired event
Controller
private AddClickedEvent addClickedEvent;
#Override
public void initialize(URL location, ResourceBundle resources)
{
id.setOnAction(event ->
{
try
{
FXMLLoader loader = new FXMLLoader();
Parent parent = loader.load(getClass().getResource("ask.fxml").openStream());
Stage stage = new Stage();
stage.setScene(new Scene(parent));
if(addClickedEvent == null){
addClickedEvent = new AddClickedEvent(AddClickedEvent.ANY);
}
Main.eventBus.fireEvent(addClickedEvent);
stage.showAndWait();
} catch (IOException e) {
e.printStackTrace();
}
});
}
Here is Controller other Controller that should show up something after fire
#Override
public void initialize(URL location, ResourceBundle resources)
{
Main.eventBus.addEventHandler(AddClickedEvent.ANY,event -> {
System.out.println("uyondan bosilib galdi");
System.out.println(yes);
yes = true;
});
id1.setOnAction(event -> {
System.out.println(yes);
});
id2.setOnAction(event -> {
Stage stage = (Stage)((Node) (event).getSource()).getScene().getWindow();
stage.close();
});
}
AddClicked Event class
public class AddClickedEvent extends Event
{
public static final EventType<AddClickedEvent> ANY =
new EventType<>(Event.ANY, "ADD_CLIENT_EVENT");
public AddClickedEvent(#NamedArg("eventType") EventType<? extends Event> eventType) {
super(eventType);
}
}
EventHandler should be created only once in process of whole application then it will react only once rather than multiple times. I come up with solutions to it declare for every controller class with static int variable which helps me to register events only once when the value of int is zero.
private static int check;
#Override
public void initialize(URL location, ResourceBundle resources)
{
if(check == 0)
{
Main.eventBus.addEventHandler(AddClickedEvent.ANY,event -> {
System.out.println("uyondan bosilib galdi");
System.out.println(yes);
yes = true;
});
check ++;
// Then it will react only once We registered event here
}
id1.setOnAction(event -> {
System.out.println(yes);
});
id2.setOnAction(event -> {
Stage stage = (Stage)((Node) (event).getSource()).getScene().getWindow();
stage.close();
});
}
I want to close my JavaFX application if the user is inactive for a period of time. I have this code in Swing and I want to do the same in JavaFX. This class redirect the user to the login panel if no event happens during the time indicated.
import javax.swing.Timer;
public class AutoClose {
private Timer timer;
public AutoClose(JFrame centralpanel) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
#Override
public void eventDispatched(AWTEvent event) {
Object source = event.getSource();
if (source instanceof Component) {
Component comp = (Component) source;
Window win = null;
if (comp instanceof Window) {
win = (Window) comp;
} else {
win = SwingUtilities.windowForComponent(comp);
}
if (win == centralpanel) {
timer.restart();
}
}
}
}, AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.MOUSE_WHEEL_EVENT_MASK);
timer = new Timer(3600000, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
centralpanel.dispose();
//Redirect to the login panel.
Login login = new Login();
login.setVisible(true);
timer.stop();
JOptionPane.showMessageDialog(null,"Connection closed due to inactivity");
}
});
timer.start();
}
});
}
}
Create a PauseTransition to trigger a delayed exit and add a event filter for InputEvents to all your scenes that restart the transition:
#Override
public void start(Stage primaryStage) throws IOException {
Button button = new Button("abcde");
StackPane root = new StackPane(button);
Scene scene = new Scene(root, 400, 400);
// create transition for logout
Duration delay = Duration.seconds(10);
PauseTransition transition = new PauseTransition(delay);
transition.setOnFinished(evt -> logout());
// restart transition on user interaction
scene.addEventFilter(InputEvent.ANY, evt -> transition.playFromStart());
primaryStage.setScene(scene);
primaryStage.show();
transition.play();
}
private void logout() {
// TODO: replace with logout code
Platform.exit();
}
Note: It's important to use an event filter since events can be consumed before they reach an event handler.
I have done this code and now it works correctly as I wanted.
public class AutoClose {
private Timeline timer;
public AutoClose(VBox mainPanel) {
timer = new Timeline(new KeyFrame(Duration.seconds(3600), new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent evt) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Inactivity");
alert.setHeaderText("Connection closed due to inactivity");
alert.show();
try {
Stage mainWindowStage = Login.getPrimaryStage();
Parent root = FXMLLoader.load(getClass().getResource("/view/Login.fxml"));
Scene scene = new Scene(root);
mainWindowStage.setScene(scene);
mainWindowStage.show();
timer.stop();
} catch (IOException ex) {
}
}
}));
timer.setCycleCount(Timeline.INDEFINITE);
timer.play();
mainPanel.addEventFilter(MouseEvent.ANY, new EventHandler<Event>() {
#Override
public void handle(Event event) {
timer.playFromStart();
}
});
}
}
well i am new to JavaFx and i haven't been using java for a really long time, so i am having many problems. And the biggest is how to change the bg of the damn pane.
Below is the Controller class
//Styling prePane
public class Controller {
//Declaring elements
public Pane prePane;
public Button generate;
public TextArea info;
#FXML
ProgressBar progressBar;
public void onGenerate() throws IOException {
//Styling prePane
prePane=new Pane();
prePane.getStyleClass().add("prePane");
//Creating and embedding progressBar
generate.setDisable(true);
progressBar.setProgress(0);
//Creating task object
Task copyWorker = createWorker();
progressBar.progressProperty().unbind();
progressBar.progressProperty().bind(copyWorker.progressProperty());
copyWorker.messageProperty().addListener(new ChangeListener<String>() {
#Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
System.out.println(newValue);
}
});
//Starting task thread
new Thread(copyWorker).start();
//QR Code generation
String details;
info.getParagraphs();
details=String.valueOf(info.getText());
ByteArrayOutputStream out= net.glxn.qrgen.QRCode.from(details).to(ImageType.GIF).stream();
File file=new File("D:\\JavaFXQRGenerator-master\\QrGenerator\\QrCode\\details.jpg");
FileOutputStream fos=new FileOutputStream(file);
fos.write(out.toByteArray());
fos.flush();
}
//Defining the task
public Task createWorker() {
return new Task() {
#Override
protected Object call() throws Exception {
for (int i = 0; i < 10; i++) {
updateProgress(i + 1, 10);
}
return true;
}
};
}
}
Main Class
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("QR Generator");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Below the actual Style sheet
.prePane{
-fx-background-image: url("D:\JavaFXQRGenerator-master\QrGenerator\resources\genPane.jpg");
}
Any kind of help is appreciated.
Assuming the resources folder is part of your build path, genPane.jpg will be in the root of the classpath. So the correct path, according to the CSS documentation is just
.prePane{
-fx-background-image: url("/genPane.jpg");
}
here is my code:
#Override
void start(Stage stage) throws Exception {
def root = new VBox() {
{
children.add(new TextArea() {
{
setId("ta1")
}
})
children.add(new TextArea() {
{
setId("ta2")
}
})
}
}
root.setOnFocus(new OnFocus() {
void onFocus(Node focusedTarget) {
// handle focusedTarget
}
})
def scene = new Scene(root, 800, 600)
stage.setScene(scene)
stage.show()
}
I hope implement following code to handle focusing child events
root.setOnFocus(new OnFocus() {
void onFocus(Node focusedTarget) {
// handle focusedTarget
}
})
if i set #ta1 and #ta2's focusedProperty, if child are large, it hard to do it, so I hope directly listen the parent, how to do it?
The standard event dispatching can be used to fire a custom event on the Scene. A listener atached to the focusOwner property of the Scene can be used to trigger the event.
Example (java)
public class FocusEvent extends Event {
public static final EventType FOCUS_EVENT_TYPE = new EventType(EventType.ROOT);
public FocusEvent(Object source, EventTarget target) {
super(source, target, FOCUS_EVENT_TYPE);
}
}
#Override
public void start(Stage primaryStage) {
TextArea ta1 = new TextArea();
ta1.setId("ta1");
TextArea ta2 = new TextArea();
ta2.setId("ta2");
VBox root = new VBox(ta1, ta2);
root.addEventHandler(FocusEvent.FOCUS_EVENT_TYPE, evt -> {
System.out.println("focused "+ evt.getTarget());
});
ta1.addEventHandler(FocusEvent.FOCUS_EVENT_TYPE, evt -> {
System.out.println("You focused the first TextArea");
evt.consume();
});
Scene scene = new Scene(root);
scene.focusOwnerProperty().addListener((o, old, newValue) -> {
if (newValue != null) {
FocusEvent event = new FocusEvent(scene, newValue);
Event.fireEvent(newValue, event);
}
});
primaryStage.setScene(scene);
primaryStage.show();
}
I have JavaFX project in which I have to listen to keyboard-input as our barcode scanner is configured that way.
Are there any libraries in JavaFX where I can keep a listener active and perform suitable action upon reception of a String by barcode-scanner.
I searched on net, but didn't find any good solution unfortunately.
Here is my code :
public class Main extends Application {
private Scene scene;
MyBrowser myBrowser;
#Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("Our Application");
java.net.CookieManager manager = new java.net.CookieManager();
java.net.CookieHandler.setDefault(manager);
myBrowser = new MyBrowser();
scene = new Scene(myBrowser, 1080, 1920);
primaryStage.setScene(scene);
primaryStage.setFullScreen(true);
primaryStage.show();
// # being the escape character
scene.setOnKeyTyped(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event) {
String text = event.getCharacter();
if (text.equals("#")) {
String tempText = completeText;
completeText = "";
processText(tempText);
}else {
completeText = completeText+text;
}
}
});
}
private void processText(String text){
System.out.println("I will process "+text);
}
public static void main(String[] args) {
launch(args);
}
public class MyBrowser extends Region {
final String hellohtml = "index.html";
WebView webView = new WebView();
WebEngine webEngine = webView.getEngine();
public MyBrowser() {
webEngine.getLoadWorker().stateProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == Worker.State.SUCCEEDED) {
JSObject window = (JSObject) webEngine.executeScript("window");
window.setMember("app", this);
}
});
URL urlHello = getClass().getResource(hellohtml);
webEngine.load(urlHello.toExternalForm());
webView.setPrefSize(1080, 1920);
webView.setContextMenuEnabled(false);
getChildren().add(webView);
}
Kindly let me know. Thank you.
So I think I understand what your problem is, you have a barcode scanner that sends in barcodes as keyevents to your application, And then you need to respond to the whole bar code when you're done receiving the code?
If that's the case you can use a KeyListener to intercept the key press events. Then you just need to implement the logic to put the individual key events together.
class MyListener implements KeyListener{
#Override
public void keyPressed(KeyEvent e) {
// Logic goes here
}
#Override
public void keyReleased(KeyEvent e) {
// Logic goes here
}
#Override
public void keyTyped(KeyEvent e) {
// Logic goes here
}
}