File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JavaFX and the fly likes javafax, button action..NEED HELP PLEASE!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JavaFX
Bookmark "javafax, button action..NEED HELP PLEASE!!!" Watch "javafax, button action..NEED HELP PLEASE!!!" New topic

javafax, button action..NEED HELP PLEASE!!!

Clara Johnson

Joined: Dec 05, 2012
Posts: 10
i have made a word guessing game, when i click myButton to check if the guessed word is right or wrong, ball1 is moved into the "container" if its right, i want that when i click the button again and if the typed word is right, the 2nd ball should move into the container too...

controller class coding

* To change this template, choose Tools | Templates
* and open the template in the editor.
package project3;

import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Circle;

* FXML Controller class
* @xxx */
public class MyFxmlController implements Initializable {

@FXML // fx:id="ball1"
private Circle ball1; // Value injected by FXMLLoader

@FXML // fx:id="ball2"
private Circle ball2; // Value injected by FXMLLoader

@FXML // fx:id="ball3"
private Circle ball3; // Value injected by FXMLLoader

@FXML // fx:id="ball4"
private Circle ball4; // Value injected by FXMLLoader

@FXML // fx:id="container"
private Circle container; // Value injected by FXMLLoader

@FXML // fx:id="myButton"
private Button myButton; // Value injected by FXMLLoader

@FXML // fx:id="myLabel1"
private Label myLabel1; // Value injected by FXMLLoader

@FXML // fx:id="myLabel2"
private Label myLabel2; // Value injected by FXMLLoader

@FXML // fx:id="pane"
private StackPane pane; // Value injected by FXMLLoader

@FXML // fx:id="txt"
private TextField txt; // Value injected by FXMLLoader

@Override // This method is called by the FXMLLoader when initialization is complete
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
assert ball1 != null : "fx:id=\"ball1\" was not injected: check your FXML file 'MyFxml.fxml'.";
assert ball2 != null : "fx:id=\"ball2\" was not injected: check your FXML file 'MyFxml.fxml'.";
assert ball3 != null : "fx:id=\"ball3\" was not injected: check your FXML file 'MyFxml.fxml'.";
assert ball4 != null : "fx:id=\"ball4\" was not injected: check your FXML file 'MyFxml.fxml'.";
assert container != null : "fx:id=\"container\" was not injected: check your FXML file 'MyFxml.fxml'.";
assert myButton != null : "fx:id=\"myButton\" was not injected: check your FXML file 'MyFxml.fxml'.";
assert myLabel1 != null : "fx:id=\"myLabel1\" was not injected: check your FXML file 'MyFxml.fxml'.";
assert myLabel2 != null : "fx:id=\"myLabel2\" was not injected: check your FXML file 'MyFxml.fxml'.";
assert pane != null : "fx:id=\"pane\" was not injected: check your FXML file 'MyFxml.fxml'.";
assert txt != null : "fx:id=\"txt\" was not injected: check your FXML file 'MyFxml.fxml'.";

// initialize your logic here: all @FXML variables will have been injected

myButton.setOnAction(new EventHandler<ActionEvent>(){

public void handle(ActionEvent event) {
int count = 0;

String guessed=txt.getText();
boolean result;

// ball2.setTranslateY(250-container.getRadius());



word guessing logic

public class MyCode {

static String x="Netbeans";
static String y[]={"net","beans","neat","beat","bet"};
//static int counter;

// public MyCode() {
// counter++;

static boolean check(String guessed)

int count=0;

boolean result=false;




return result;

Ranganathan Kaliyur Mannar

Joined: Oct 16, 2003
Posts: 1101

Hi Clara,
Welcome to the Ranch!

Please UseCodeTags when posting code (click on the link to understand how to do that).

And, you have not mentioned what happens when you run the program. What happens and what you expect will help everyone to understand what needs to be done.

Clara Johnson

Joined: Dec 05, 2012
Posts: 10
thankyou so much for your reply

i have worked on it and now what the code does is that there are about 5 balls, and its a word guessing game...the user has to make words out from "netbeans", like "beans", "net" and so on...on each correct answer a ball moves into a container...i have used the following technique to do so, but this is not a nice approach in programming, i want this to happen from some array...if someone can please help by keeping the balls in an array or by making an array in the "word guessing" code that stores the guessed words so that they are not repeated... [sorry the next time i'll write i'll use the code tags] thing more that i want to ask is that my balls get into the container suddenly, i want them to move slowly with a delay...what should i do for that....and how can i change the topic's name?thanks for any help...

else if(count==2)

else if(count==3)

else if(count==4)

JOptionPane.showMessageDialog(null,"Sorry, wrong guess!");

Manuel Petermann
Ranch Hand

Joined: Jul 19, 2011
Posts: 177

First things first. You should not use Java Swing stuff in Java FX so you need to get rid of your JOptionPane.
The Interface Initializable is deprecated and should not be used anymore. The FXML loader takes care of that now.
I really want to encourage you to use code tags. Its very frustrating to go through your originally posted code.
I don't know how anyone could help you if you are just posting small code snippets and never something actually runnable.

Please correct my English.
Darryl Burke

Joined: May 03, 2008
Posts: 5115

Clara, did you have a probklem understanding what Ranganathan told you about using the code tags? Again, this is a link, click it --> UseCodeTags

luck, db
There are no new questions, but there may be new answers.
Clara Johnson

Joined: Dec 05, 2012
Posts: 10
word guessing code, tell me what to do if i want an array to store the words guessed by the user, so that when the user types "the already guessed word" it should give a message that its been already guessed and no ball should move in...

Clara Johnson

Joined: Dec 05, 2012
Posts: 10
herez the part where the javafx scenebuilder is involved, where the ball is being moved....i want to move the ball with some delay...i mean the ball just moves from source to destination like suddenly...i want it to move to do the "if else" part is not a good to do it in some other way...i want someone to help me with the movement of the ball first...many thanks...

Clara Johnson

Joined: Dec 05, 2012
Posts: 10
this is the main class

I agree. Here's the link:
subject: javafax, button action..NEED HELP PLEASE!!!
It's not a secret anymore!