-3

I am a Java programmer and new to JavaFx.I want to create a virtual keyborad. I can make everything like buttons,layout,stage,scene everything.I also know using setText() method which can write text on the same java applicatton,but the question is that how do i make understand computer or program(in javafx or java not in swings) that on button click(ie on setOnAction()),it has to write a character on any 'another' java application (such as notepad,wordpad,etc). Is there is any class or interface that i have to extends or implements respectively or is there is any method which can help? I had explored the internet but was unable to find something helpful.

enter image description here

Weirdoooo
  • 243
  • 4
  • 10

1 Answers1

0

If you have set all the buttons in your controller you can do like this :

//I supposed you named you 'button_a' your  
@FXML
Button button_a;
@Override
public void initialize(URL location, ResourceBundle resources) {
    button_a.setOnAction(event->{
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter("file.txt")); //or new File("c:/users/.../.../file.txt");
            writer.write(button_a.getText());       //will give the letter you write on the button : the letter of the keyboard
        } catch (IOException e) {
            System.err.println("IOError on write");
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    System.err.println("IOError on close");
                    e.printStackTrace();
                }
            }
        }
    });
}

An easier way could be that : You have surely put all your buttons in a container, a GridPane would be good because you can put all into ONE container (and put ONLY the buttons or you'll need to check it's a button each time in the loop), and then iterate over the children of the GridPane (the buttons) :

@FXML
GridPane grid;
@Override
public void initialize(URL location, ResourceBundle resources) {
    String fileName = "test.txt";
    for(Node button : grid.getChildren()){
        ((Button)button).setOnAction(event->{
            BufferedWriter writer = null;
            try {
                writer = new BufferedWriter(new FileWriter(fileName)); //or new File("c:/users/.../.../file.txt");
                writer.write(((Button)button).getText());       //will give the letter you write on the button : the letter of the keyboard
            } catch (IOException e) {
                System.err.println("IOError on write");
                e.printStackTrace();
            } finally {
                if (writer != null) {
                    try {
                        writer.close();
                    } catch (IOException e) {
                        System.err.println("IOError on close");
                        e.printStackTrace();
                    }
                }
            }
        });
    }
}

Hope this helps

azro
  • 53,056
  • 7
  • 34
  • 70