0

I have a certain set of buttons registered so that they can be executed using letters in a keyboard or by mouse click. But I want to hide the text in the button from the user so that it looks more natural.
Heres part of the code:

ToggleButton btn1 = new ToggleButton("");
btn1.setMnemonicParsing(true); // instruction to parse mnemonic
btn1.setText("_7"); 

Now instead of the button showing a 7 in the middle I want it to be hidden but still function the same. I have a feeling it has something to do with the setVisible() method but I'm not sure how to use it for just the text inside the button instead of the entire button itself.

  • After hiding 7, what you want to display text on button? – Santosh Dec 07 '17 at 06:00
  • I don't the number 7 to show up at all, just look like a blank button but still function @Santosh – Devon Thomas Dec 07 '17 at 06:01
  • Use `btn1.setText("");`. If you code looks exactly like it does above, just delete the`btn1.set("_7);`. `ToggleButton btn1 = new ToggleButton("");` sets the text to empty string already. – SedJ601 Dec 07 '17 at 06:04
  • tried that, it wont set the 7 key to the button if its cleared. basically I just want that key to be assigned to the button which is why I set the text to "_7" but i dont want to see the number 7.@SedrickJefferson – Devon Thomas Dec 07 '17 at 06:07
  • Firstly its not good design to display empty button, secondly ToggleButton is to display toggle like ON or OFF. If you still want to achieve this then i think you can do btn1.setText(" "); and add a click listener for btn1 there consider the actual value you want. – Santosh Dec 07 '17 at 06:07
  • You have bound the button's text to a keyboard key. There is no other way to set the button's text to null. – SedJ601 Dec 07 '17 at 06:10
  • If you don't have a large number of buttons, try this approach to fire the buttons when a key is pressed. https://stackoverflow.com/questions/43199342/javafx-bind-key-to-button – SedJ601 Dec 07 '17 at 06:12
  • Hey, how does the code look where you bind the keys using the button's text? I just thought of something new. – SedJ601 Dec 07 '17 at 17:55
  • Hey sorry for the late reply but heres where I bind the keys: btn1.setMnemonicParsing(true); // instruction to parse mnemonic btn1.setText("_7"); – Devon Thomas Dec 08 '17 at 07:58

1 Answers1

0

Most probably you can't do both at the same time. If you want to trigger the button using MneomonicParsing, then you have set the text and it will be visible in the button.

Why don't you use a onKeyPressed method of the container of those buttons instead? Then you don't have to set any text on the button. Like this:

container.setOnKeyPressed(k->{
    switch(k.getCode()){
        case NUMPAD7:
        case DIGIT7: btn1.fire();
            break;
        //implement other cases
    }
});
Muzib
  • 2,412
  • 3
  • 21
  • 32