PROG24178 Lecture Notes - Lecture 12: Radio Button, Pepperoni, Javafx

78 views6 pages
2 Aug 2016
Course
Professor
Page:
of 6
Button Example
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package buttonexample;
//these are my required imports
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextArea;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
*
*In today's lecture, we will cover button examples and radio buttons, and check boxes in
JavaFX.
*/
find more resources at oneclass.com
find more resources at oneclass.com
public class ButtonExample extends Application {
//here i set my field variables and controls.
private TextArea txtOrder= new TextArea();//text area with the text area assigned to it here
and called txtOrder
private CheckBox chkPepperoni; //checkbox with the variable name chkPepperoni
private CheckBox chkOnion=new CheckBox("Onion");//set the text of chkOnion to Onion
private CheckBox chkBacon=new CheckBox("Bacon");//set text box of checkbox to bacon
private RadioButton radSmall=new RadioButton("Small"); //radio button with var name
small and text small
private RadioButton radMedium=new RadioButton("Medium"); //radiobutton with var name
radMedium and text medium
private RadioButton radLarge=new RadioButton("Large");//radiobutton with var name
radlarge and text large
private ToggleButton tgbDelivery=new ToggleButton("Pick Up"); //toggle button with the
text pickup assigned to it in the constructor
private ToggleGroup group=new ToggleGroup(); //toggle group
@Override
public void start(Stage primaryStage) {
// Button btn = new Button();
chkPepperoni.setText("Pepperoni");//set the text of pepproni to Pepperoni, another way
to do it is in the constructor itself as above for the other examples
/* btn.setText("Say 'Hello World'");
btn.setOnAction((ActionEvent event) -> {
System.out.println("Hello World!");
}); */
find more resources at oneclass.com
find more resources at oneclass.com
VBox pToppings = new VBox(); //create a new vbox pane that has the name ptoppings and
has all the attributes we assign it to.
//another way to add the getchildren is to do it this way:
// VBox pToppings = new VBox(chkPepperoni,chkOnion,chkBacon);//the attributes are
added in the constructor tself, saving us 3 lines of code!
//a vbox is a vertical box so it goes from top down rather than horizontally.
pToppings.getChildren().add(chkPepperoni); //here is wherei add all the checkboxes
pToppings.getChildren().add(chkOnion); //here is wherei add all the checkboxes. the get
children method is one way of doing this, but you can do it in the
//constructor itself, as above.
pToppings.getChildren().add(chkBacon); //here is wherei add all the checkboxes,
remember pane.getChildren().add(the_variable);
//create a vbox object that has the name psize and has all the radio buttons. Let's add it to
our dropdown menu for the pizza.
VBox pSize = new VBox(radSmall, radMedium, radLarge, tgbDelivery); //create a new vbox
pane that has the name psize and has all the radio buttons and
//toggle button.
//we need to group the delivery size, either small, medium OR large.
radSmall.setToggleGroup(group);//we set the toggle group to group and assign radsmall to
it
radMedium.setToggleGroup(group);//we set the toggle group to group and assign radsmall
to it
radLarge.setToggleGroup(group);//we set the toggle group to group and assign radsmall to
it
//can also use a setter method to set the toggle group attributes using public
setToggleGroup(){ s m l}
//create a gridpane called pane that has pToppings in the first column, txtOrder in second
column and pSize in third column
tgbDelivery.setOnAction(new SetOrder());//set order is invokd on order
find more resources at oneclass.com
find more resources at oneclass.com