AnemonePoppy
AnemonePoppy

Reputation: 51

Java Nested JPanel with checkboxes

I am working on a menu ordering form. The program runs until the end where I have my last "if statement" that is to create a new Panel with check box options, that is then to open up to perform new work (display message or continue order details).

My first problem is that my check boxes are not being added to the new panel. I tried wrapping it in a new FlowLayout but only the header is showing.

I'm new to Java, only been programming a few months. This is a nested if-else statement. Not sure what to do. Here is the end part of the code. Any help pointing out my probably obvious mistakes would be appreciated. I want to learn.

This is inside the actionPerformed method of my ClickAction:

if (event.getSource()== order) {
    deliveryPanel = new JPanel();
    deliveryPanel.setLayout(new FlowLayout());

    headerLabel.setText("This Order will be for:");
    JCheckBox dv = new JCheckBox("Delivery"); 
    JCheckBox pu = new JCheckBox("Pick Up");
    deliveryPanel.add(dv);
    deliveryPanel.add(pu);

    CheckBoxHandler checkHandler = new CheckBoxHandler();

    pu.addItemListener(checkHandler); 
    dv.addItemListener(checkHandler);


    /*Options for delivery choice*/
    if (JCheckBox == pu) {
        System.out.println("Your Order"+ orderOutput+ "for" +name+ "will be ready for pickup");
    }
    else if (JCheckBox == dv){
        JOptionPane.showInputDialog(null,"Please Enter your Delivery Address");  //input address

        JOptionPane.showMessageDialog(null,"**Order Details**"+"\n"+"Name: "+name+"\n"+"Address: "+
            address+"\n"+"Order: "+orderOutput+"\n");
    }

    nameOutputMsg = "Welcome " + name + ".\n";
    greetingOutputMsg = "\nThank you for visiting Famous Favorite's Subs!" + "\n";

    //create output string
    outputMsg = nameOutputMsg
                + "**Order Details**"
                + "\nOrder: " + orderOutput
                + "\nQuantity : " + quantity
                + "\nDelivery Address: " + address
                + greetingOutputMsg;


    /*
     * output the order information to a file. Finally,
     * you will read this file to display the order confirmation information.
     */
    FileWriter fw = null; 
    try {
        fw = new FileWriter(new File("output.txt"));
    }
    catch (IOException ex) {
        JOptionPane.showMessageDialog(null, "File could not be accessed/Created. ");
        System.exit(1);
    }
    try {
        fw.write(outputMsg);
    }
    catch (IOException ex) {
        JOptionPane.showMessageDialog(null, "File could not be written.");
        System.exit(1);
    }
    try {
        fw.close();
    }
    catch (IOException ex) {
        JOptionPane.showMessageDialog(null, "File could not be saved. ");
        System.exit(1);
    }

    Scanner fileScanner = null;
    try {
        fileScanner = new Scanner(new FileReader("output.txt"));
    }
    catch (FileNotFoundException ex) {
        JOptionPane.showMessageDialog(null, "File could not be read or file not exists. ");
        System.exit(1);
    }
    String fileText="";

    while(fileScanner.hasNext()) {
        fileText += fileScanner.nextLine()+"\n";
    }
    fileScanner.close();
    // display output message
    JOptionPane.showMessageDialog(null, fileText);
    System.exit(0);


}

if (event.getSource() == exit) {
    JOptionPane.showMessageDialog(null, "Thank you for your Business.", "Goodbye", JOptionPane.INFORMATION_MESSAGE);
     System.exit(1); // terminate application
}

Upvotes: 0

Views: 177

Answers (1)

chrissukhram
chrissukhram

Reputation: 2967

Currently you are not displaying your deliveryPanel, you are just creating it. You need to add it to a frame in order for it to become visible.

If you want it to show up as a popup and not in the main window, you can add your deliveryPanel to a JOptionPane that shows up once the "order" event is pressed.

For example:

JOptionPane.showMessageDialog(null,deliveryPanel,"Order",JOptionPane.INFORMATION_MESSAGE);

Upvotes: 1

Related Questions