0

I am taking a GridPane example from an book. The book is not the most recent. When I try to use the Internet I keep getting Scene Builder which is not what I want to use. I am using NetBeans IDE 8.2, and I have 2 problems with this program. Within the btnOk_Click() I am trying to choose Small, Medium, Large AND Thin, Thick. The program puts them in the same group. Also, this MessageBox.show(msg, "Order Details"); //????? may be out dated. The btnCancel_Click() works, but the btnOk_Click() does not show any work. Here is the program:

package gridpane;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
//import javafx.event.ActionEvent;
//import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
//import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;

public class GridPane_Pizza extends Application {

//TextField
TextField txtName;
TextField txtPhone;
TextField txtAddress;
//RadioButton
RadioButton rdoSmall;
RadioButton rdoMedium;
RadioButton rdoLarge;
//RadioButton
RadioButton rdoThin;
RadioButton rdoThick;
//CheckBox
CheckBox chkPepperoni;
CheckBox chkMushrooms;
CheckBox chkAnchovies;
//private Stage state;
Stage stage;

@Override
public void start(Stage primaryStage) {

    stage = primaryStage;

    //Create Label
    Label lblName = new Label("Enter the name:\t");
    txtName = new TextField();
    txtName.setMinWidth(100);
    txtName.setPrefWidth(200);
    txtName.setMaxWidth(300);
    txtName.setPromptText("Enter the name here:\t");

    //Create phone for label and text
    Label lblPhone = new Label("Enter the phone:\t");
    txtPhone = new TextField();
    txtPhone.setMinWidth(60);
    txtPhone.setPrefWidth(120);
    txtPhone.setMaxWidth(180);
    txtPhone.setPromptText("Enter the phone here:\t");

    //Create address for label and text
    Label lblAddress = new Label("Enter the Address:\t");
    txtAddress = new TextField();
    txtAddress.setMinWidth(100);
    txtAddress.setPrefWidth(200);
    txtAddress.setMaxWidth(300);
    txtAddress.setPromptText("Enter the address here:\t");

    //Create the size pane  ??
    Label lblSize = new Label("Size:\t");  //??  \t
    rdoSmall = new RadioButton("Small");
    rdoMedium = new RadioButton("Medium");
    rdoLarge = new RadioButton("Large");

    rdoMedium.setSelected(true);

    ToggleGroup groupSize = new ToggleGroup();
    rdoSmall.setToggleGroup(groupSize);
    rdoMedium.setToggleGroup(groupSize);
    rdoLarge.setToggleGroup(groupSize);

    VBox paneSize = new VBox(lblSize, rdoSmall, rdoMedium, rdoLarge);

    paneSize.setSpacing(10);

    //Create the crust pane
    Label lblCrust = new Label("Crust");
    rdoThin = new RadioButton("Thin");
    rdoThick = new RadioButton("Thick");

    rdoThin.setSelected(true);

    ToggleGroup groupCrust = new ToggleGroup();
    rdoThin.setToggleGroup(groupSize);
    rdoThick.setToggleGroup(groupSize);


    VBox paneCrust = new VBox(lblCrust, rdoThin, rdoThick);

    paneCrust.setSpacing(10);

    Label lblToppings = new Label("Labels");
    chkPepperoni = new CheckBox("Pepperoni");
    chkMushrooms = new CheckBox("Mushrooms");
    chkAnchovies = new CheckBox("Anchovies");


    VBox paneToppings =  new VBox(lblToppings, chkPepperoni, chkMushrooms, chkAnchovies);

    paneToppings.setSpacing(10);

    Button btnOk = new Button("OK!!");
    btnOk.setPrefWidth(80);
    btnOk.setOnAction(e -> btnOk_Click());  //change to public

    Button btnCancel = new Button("Cancel!!");
    btnCancel.setPrefWidth(80);
    btnCancel.setOnAction(e -> btnCancel_Click());  //change to public

    HBox paneButtons = new HBox(10, btnOk, btnCancel);


    GridPane grid = new GridPane();

    grid.setPadding(new Insets(10));
    grid.setHgap(10);
    grid.setVgap(10);
    grid.setMinWidth(500);
    grid.setPrefWidth(500);
    grid.setMaxWidth(800);


    grid.addRow(0, lblName, txtName);
    grid.addRow(1, lblPhone, txtPhone);
    grid.addRow(2, lblAddress, txtAddress);
    grid.addRow(3, paneSize, paneCrust, paneToppings);

    grid.add(paneButtons, 2, 15);

    GridPane.setHalignment(lblName, HPos.RIGHT);  //grid or GridPane
    //grid.setHalignment(lblPhone, HPos.RIGHT);
    GridPane.setHalignment(lblPhone, HPos.RIGHT);
    GridPane.setHalignment(lblAddress, HPos.RIGHT);

    //grid.setColumnSpan(txtName, 2);
    GridPane.setColumnSpan(txtName, 2);  //grid or GridPane
    GridPane.setColumnSpan(txtPhone, 2);
    GridPane.setColumnSpan(txtAddress, 2);

    ColumnConstraints col1 = new ColumnConstraints();  //??
    col1.setPercentWidth(33);
    ColumnConstraints col2 = new ColumnConstraints();
    col2.setPercentWidth(33);
    ColumnConstraints col3 = new ColumnConstraints();
    col3.setPercentWidth(33);
    grid.getColumnConstraints().addAll(col1, col2, col3);  //??



    //
    Scene scene = new Scene(grid);
    primaryStage.setTitle("Pizza Order");
    primaryStage.setScene(scene);
    primaryStage.setMinWidth(500);
    primaryStage.setMaxWidth(1200); //900
    //primaryStage.setMaxWidth(900); //900
    primaryStage.setMaxHeight(800);
    primaryStage.show();
}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

//private void btnOk_Click() {  //public
public void btnOk_Click() {  //public    
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    String msg = "Customer:\n\n";

    msg += "\t" + txtName.getText() + "\n";
    msg += "\t" + txtPhone.getText() + "\n\n";
    msg += "\t" + txtAddress.getText() + "\n";
    msg += "You have ordered a ";

    if (rdoSmall.isSelected())   //Problem here
        msg += "small ";
    if (rdoMedium.isSelected())
        msg += "medium ";
    if (rdoLarge.isSelected())
        msg += "large ";
    //problem here
    if (rdoThin.isSelected())
        msg += "thin crust pizza with ";
    if (rdoThick.isSelected())
        msg += "thick crust pizza with ";

    String toppings = "";

    toppings = buildToppings(chkPepperoni, toppings);
    toppings = buildToppings(chkMushrooms, toppings);
    toppings = buildToppings(chkAnchovies, toppings);

    if (toppings.equals(""))
        msg += "no toppings.";
    else
        msg += "the following toppings:\n" + toppings;

    MessageBox.show(msg, "Order Details");  //?????
    //msg.show(msg, "Order Details");
    //msg
    //stage.show(msg, "Order Details");  //??????????
}

public String buildToppings(CheckBox chk, String msg)
{
    if (chk.isSelected())
    {
        if (!msg.equals(""))
        {
            msg += ", ";
        }
        msg += chk.getText();
    }
    return msg;
}

public void btnCancel_Click() {

    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    stage.close();
}
Reed
  • 5
  • 6
  • I don't know what `MessageBox` is, there is no such class in the JavaFX API. Perhaps you could use an [`Alert`](https://openjfx.io/javadoc/13/javafx.controls/javafx/scene/control/Alert.html) with [`AlertType.INFORMATION`](https://openjfx.io/javadoc/13/javafx.controls/javafx/scene/control/Alert.AlertType.html#INFORMATION) instead. – jewelsea Jan 06 '20 at 22:11
  • I think you are right, but I am just learning javafx, and I don't know how to use Alert or AlertType.INFORMATION. Can you give me some help there? – Reed Jan 07 '20 at 18:50
  • 1
    [Makery provide a tutorial on standard JavaFX dialogs](https://code.makery.ch/blog/javafx-dialogs-official/), I advise you take a look at it. – jewelsea Jan 07 '20 at 18:52
  • That was helpfully, but what I would really like to do is find a why to use Alert in the btnOk_Click(). This is just an example where I am trying to put it all together with gridpane. I have a combo box that I wrote, and it is more important, yet it does not go into gridpane/button. That's why I am trying to figure this program out so that it runs. If it runs, I can use it on my other program. – Reed Jan 08 '20 at 02:33
  • Ok then, sorry I don’t understand what you are asking. Good luck solving your problem. – jewelsea Jan 08 '20 at 09:10

1 Answers1

0

So I can see in your code that you put the wrong group variable here...

ToggleGroup groupCrust = new ToggleGroup();
rdoThin.setToggleGroup(groupSize);
rdoThick.setToggleGroup(groupSize);

It should be groupCrust instead of groupSize if you want them to be in different groups.

In terms of the event handlers for the buttons, I have never seen doing that in the way you shared...

I would add the click event handlers like this...

btnOk.addEventHandler(MouseEvent. MOUSE_CLICKED, 
  new EventHandler<MouseEvent>() {
      @Override public void handle(MouseEvent e) {
          // some code
      }
});

In terms of the MessageBox, looking at this seems like you might want to try to Alert depending on the version you are using. You should refer to this code https://stackoverflow.com/a/36137669/11547889 if you want to implement it on your code

  • Setting the event handler as done in the question is preferred to your proposed solution. The question uses a [lambda event handler](https://code.makery.ch/blog/javafx-8-event-handling-examples/), which is more concise. It uses `setOnAction`, rather than a click handler so can be triggered by programmatically firing the button or via the keyboard (which the click handler won't). Also, there is better type safety as setOnAction it will take only ActionEvent as a parameter and not an arbitrary event, as addEventHandler does. – jewelsea Jan 06 '20 at 22:18
  • Nice!! thanks for the info :). It has been a while since I have done anything with JavaFX – Adrian Beloqui Jan 06 '20 at 22:32
  • Thanks for ToggleGroup groupCrust error. I corrected it, and that part works. I am still confused about what I should do with the MessageBox. I did look at that other place - https://stackoverflow.com/a/36137669/11547889, and the Alert, but I am very new at this type of writing so javafx is a bit hard for me to understand, and the book I use does not go over Alert. – Reed Jan 07 '20 at 18:42
  • //MessageBox.show(msg, "Order Details"); //????? alert.setTitle("Message Here..."); alert.setHeaderText("Look, an Information Dialog"); //alert.setContentText("I have a great message for you!"); alert.setContentText(msg, "Order Details"); alert.showAndWait(); – Reed Jan 08 '20 at 02:48
  • Alert alert = new Alert(Alert.AlertType.INFORMATION); – Reed Jan 08 '20 at 02:49