2

I am a beginner at programming. I am creating a keyboard which looks like a traditional T9 Keyboard. I am able to print the letters on display but I have few bugs right now. I cannot print a specific character with the number of times the key has been pressed

here is my code, any kind of help is appreciated. Thanks

    public int count = 0;

    @FXML
    private void handleButtonActionPQ(ActionEvent event) {
        count = count + 1;

        //key.btn1f();
        System.out.println(count);
        if(count <= 5 ){

            switch(count){
                case 1: display.appendText("p");
                        break;
                case 2: display.appendText("q");
                        break;
                case 3: display.appendText("x");
                        break;
                case 4: display.appendText("y");
                        break;
                case 5: display.appendText("z");
                        break;
                default: display.appendText("p");
            }
        }
        else if(count > 5){
            count = 0;
        }
    }
Krish94
  • 21
  • 3
  • 3
    You are trying to tackle to many issues in one thread. – SedJ601 Feb 16 '18 at 20:59
  • https://stackoverflow.com/questions/9966136/javafx-periodic-background-task – SedJ601 Feb 16 '18 at 21:00
  • Please one focused question per post as @Sedrick commented. Also don't post too much code, see [mcve]. For example to demonstrate key press count, one button is enough. – c0der Feb 17 '18 at 06:46
  • okay got you, I have edited a per your requirements can you please help me now. @c0der – Krish94 Feb 17 '18 at 21:53

1 Answers1

0

One way to identify which button was pressed is demonstrated in the following mcve:

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class FxMain extends Application {

    private Button aButton, bButton;

    @Override
    public void start(Stage stage) {

        final GridPane cp = new GridPane();
        aButton = new Button("A");
        bButton = new Button("B");

        aButton.setOnAction(e ->  buttonPressed(e));
        bButton.setOnAction(e ->  buttonPressed(e));        
        cp.add(aButton, 0, 0); cp.add(bButton, 1, 0);

        stage.setScene(new Scene(cp));
        stage.show();
    }

    //you could also have buttonPressed(Button b) 
    //and use it like: aButton.setOnAction(e ->  buttonPressed(aButton))
    private void buttonPressed(ActionEvent e) {

        if(! (e.getSource() instanceof Button) ) { return;}
        Button button = (Button) e.getSource();

        if(button.equals(aButton)) { System.out.println("A pressed"); }
        else if(button.equals(bButton)) { System.out.println("B pressed"); }
    }

    public static void main(String[] args) { launch(args);}
}
c0der
  • 18,467
  • 6
  • 33
  • 65
  • Thanks for the help, is there a way to detect, let's say I have 10 buttons and I want to know if the user is clicking the buttons randomly. I want to detect whether the user has clicked the same button repeatedly or different button. – Krish94 Feb 20 '18 at 02:54