views:

65

answers:

2
public class JFrameWithPanel extends JFrame implements ActionListener, ItemListener
{
    int packageIndex;
    double price;
    double[] prices = {49.99, 39.99, 34.99, 99.99};

    DecimalFormat money = new DecimalFormat("$0.00");
    JLabel priceLabel = new JLabel("Total Price: "+price);
    JButton button = new JButton("Check Price");
    JComboBox packageChoice = new JComboBox();
    JPanel pane = new JPanel();
    TextField text = new TextField(5);
    JButton accept = new JButton("Accept");
    JButton decline = new JButton("Decline");
    JCheckBox serviceTerms = new JCheckBox("I Agree to the Terms of Service.", false);
    JTextArea termsOfService = new JTextArea("This is a text area", 5, 10);

    public JFrameWithPanel()
    {
        super("JFrame with Panel");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pane.add(packageChoice);
        setContentPane(pane);
        setSize(250,250);
        setVisible(true);

        packageChoice.addItem("A+ Certification");
        packageChoice.addItem("Network+ Certification ");
        packageChoice.addItem("Security+ Certifictation");
        packageChoice.addItem("CIT Full Test Package");

        pane.add(button);
        button.addActionListener(this);

        pane.add(text);
        text.setEditable(false);
        text.setBackground(Color.WHITE);
        text.addActionListener(this);

        pane.add(termsOfService);
        termsOfService.setEditable(false);
        termsOfService.setBackground(Color.lightGray);

        pane.add(serviceTerms);
        serviceTerms.addItemListener(this);

        pane.add(accept);
        accept.addActionListener(this);

        pane.add(decline);
        decline.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e)
    {
        packageIndex = packageChoice.getSelectedIndex();
        price = prices[packageIndex];
        text.setText("$"+price);

        Object source = e.getSource();

        if(source == accept)
        {
            if(serviceTerms.isSelected() == false)
            {
                JOptionPane.showMessageDialog(null,"Please accept the terms of service.", "Terms of Service", JOptionPane.ERROR_MESSAGE);
            }
            else
            {
                JOptionPane.showMessageDialog(null,"Thank you. We will now move on to registering your product.");
                pane.dispose();
            }
        }
        else if(source == decline)
        {
            System.exit(0);
        }
    }

    public void itemStateChanged(ItemEvent e)
    {
        int select = e.getStateChange();
    }

    public static void main(String[] args)
    {
        String value1;
        int constant = 1, invalidNum = 0, answerParse, packNum, packPrice;

        JOptionPane.showMessageDialog(null,"Hello!"+"\nWelcome to the CIT Test Program.");

        JOptionPane.showMessageDialog(null,"IT WORKS!");
    }



}//class

How do I get this frame to close so that my JOptionPane Message Dialogs can continue in the program without me exiting the program completely.

EDIT: I tried .dispose() but I get this:

cannot find symbol
symbol  : method dispose()
location: class javax.swing.JPanel
                pane.dispose();
                    ^
A: 

Try: this.dispose() instead.

JPanel doesn't have that method but JFrame does

edit

In your main you're not calling your Frame:

public static void main(String[] args)  {
    String value1;
    int constant = 1, invalidNum = 0, answerParse, packNum, packPrice;

    JOptionPane.showMessageDialog(null,"Hello!"+"\nWelcome to the CIT Test Program.");

    JOptionPane.showMessageDialog(null,"IT WORKS!");
    }

Try adding it and see the difference:

public static void main(String[] args)  {
    String value1;
    int constant = 1, invalidNum = 0, answerParse, packNum, packPrice;

    JOptionPane.showMessageDialog(null,"Hello!"+"\nWelcome to the CIT Test Program.");

    JOptionPane.showMessageDialog(null,"IT WORKS!");
    new JFrameWithPanel(); //<-- creating a JFrameWithPanel
}

Also in the action performed method, you're showing the dialog and then disposing, probably you want to do it the other way around.

if(serviceTerms.isSelected() == false) {
    JOptionPane.showMessageDialog(null,"Please accept the terms of service.", "Terms of Service", JOptionPane.ERROR_MESSAGE);
} else {
    this.dispose();
    JOptionPane.showMessageDialog(null,"Thank you. We will now move on to registering your product.");
}

Result in:

main

Followed by

result

edit 2

Try the following code, it should show a frame, and when you click the "close" button it should show a dialog, is that what you're looking for?

import javax.swing.*;
import java.awt.event.*;

class FrameDemo {
    public static void main( String [] args ) {
        final JFrame frame = new JFrame("Main frame");
        frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        frame.add( new JPanel(){{
            add( new JLabel("This is the main content"));
            add( new JButton("Close"){{
                addActionListener( new ActionListener(){
                    public void actionPerformed(ActionEvent e ) {
                        frame.dispose();
                        JOptionPane.showMessageDialog(frame,"IT WORKS!");

                    }
                });
            }});
        }});
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );

    }
}
OscarRyz
That's something completely different. In your main method you're not even calling your `JFrameWithPanel` Try adding: `new JFrameWithPanel();` in the last line of the `main` method and see the difference
OscarRyz
Yes, that's exactly what is doing, see my update. Clicking on accept shows the "Thank you" dialog.
OscarRyz
The first two? Welcome to CIT and IT WORKS? Probably you have just delete them.
OscarRyz
Well I want the program to continue to show JOptionPane's after the Frame is disposed, how would I go about doing that, that was my main question, to get the frame to close and the program to continue opening JOptionPanes.
Nick Gibson
uh? Do you want to dispose the frame and then show a dialog? Or do you want to show a dialog, then show a frame and keep the dialog visible? I don't understand you what is what you're trying to do. Look at the ìf/else` part in the "accept" logic. You're showing the dialog first and then disposing the frame, probably you want the other way around ( see my answer above )
OscarRyz
I changed it to your way. I want the Frame to dispose. Its doing that. Now that the frame is disposed I want my JOptionPane's to actually popup and let me interact with them. They arent doing that. Instead the program is closing.
Nick Gibson
Yes, i want it to keep displaying Dialogs even after the frame closes when it gives the Dialog "OK now time to register your product!" But it keeps closing. Now how do I do it to MY code without having to rewrite everything.
Nick Gibson
Did the short code I posted work?
OscarRyz
Yeah it did, how would I put it into my code now?
Nick Gibson
I told you that already: http://img267.imageshack.us/img267/7038/capturadepantalla201006x.png Swap the call to `dispose` you're creating the dialog first and then disposing. What you need is to dispose first and then show the dialog.
OscarRyz
A: 

I think this could be your problem:

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

This causes the application to exit when you close the frame.

You could try:

setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

and replace the dispose() call with:

setVisible(false)

Aaron