0

I have a button that adds its name to a TextArea. I want the button to hold a double that also gets displayed in the text area but is not displayed on the button itself.

Here is my MainMenuController where the TextArea is

@FXML TextArea txtReceipt;

@FXML
public void buttonHandler(ActionEvent event) {
    Button button = (Button) event.getSource();
    txtReceipt.appendText ((String) button.getText() + "\n");
}

At first, I extended the Button class to include a double. However, JavaFX does not recognize this as a control. I then made a class to hold a button to no avail.

import javafx.scene.control.Button;


/**
 *
 * @author David
 */
public class NumButton extends Button{

    double cost;



   private void setCost(double cost)
   {
       this.cost = cost;
   }

}

Can anyone provide guidance on how to achieve this? I am thinking that perhaps

Button button = (Button) event.getSource();
txtReceipt.appendText ((String) button.getText() + "\n");

should be

NumButton numButton = (Button) event.getSource();
txtReceipt.appendText ((String) numButton.getText() + numButton.cost + "\n");
Mazen Embaby
  • 1,255
  • 11
  • 18
David Frick
  • 641
  • 1
  • 9
  • 25
  • What do you mean by "At first, I extended the Button class to include a double. However, JavaFX does not recognize this as a control."? A subclass of `Button` will also be a subclass of `Control`, since `Button` is a subclass of `Control`. – James_D Jul 19 '17 at 00:49
  • JavaFx interface does not see NumButton when I try to add it. – David Frick Jul 19 '17 at 00:52
  • Sorry, I still don't understand what you mean by that. If you make `NumButton` a subclass of `Button`, you can use `NumButton` anywhere you can use a `Button`. What do you mean by "JavaFX interface does not see `NumButton`"? What interface? – James_D Jul 19 '17 at 00:53
  • Scenebuilder 2.0. Sorry for the confusion – David Frick Jul 19 '17 at 00:59
  • I didn't realize you were using SceneBuilder. See https://stackoverflow.com/questions/30063792/adding-a-custom-component-to-scenebuilder-2-0 (You should also consider upgrading to a more [recent version](http://gluonhq.com/products/scene-builder/).) – James_D Jul 19 '17 at 01:01
  • Thx James! Much appreciated – David Frick Jul 19 '17 at 01:23

0 Answers0