• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.net.URL;
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>(){


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

String guessed=txt.getText();
boolean result;
result=MyCode.check(guessed);
if(result)
{

ball1.setTranslateX(600);
ball1.setTranslateY(250-container.getRadius());
//ball2.setTranslateX(600);
// ball2.setTranslateY(250-container.getRadius());
}
else
System.out.println("wrong");
}

});

}
}

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;
//counter++;
//System.out.println("turns"+counter);
for(count=0;count<5;count++)
{

if(guessed.equals(y[count]))

{
result=true;
break;
}

}
if(result)
System.out.println("Right");
else
System.out.println("Wrong");

return result;

}
}
 
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]....one 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...

if(count==1)
{
ball1.setTranslateX(600);
ball1.setTranslateY(250-container.getRadius());
}
else if(count==2)
{
ball2.setTranslateX(600);
ball2.setTranslateY(250-container.getRadius());
}

else if(count==3)
{
ball3.setTranslateX(600);
ball3.setTranslateY(250-container.getRadius());
}

else if(count==4)
{
ball4.setTranslateX(600);
ball4.setTranslateY(250-container.getRadius());
}



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

}
 
Ranch Hand
Posts: 177
Hibernate Python Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Clara, did you have a probklem understanding what Ranganathan told you about using the code tags? Again, this is a link, click it --> UseCodeTags
 
Clara Johnson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 slowly...how to do that...plus the "if else" part is not a good approach...how to do it in some other way...i want someone to help me with the movement of the ball first...many thanks...

 
Clara Johnson
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is the main class

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic