Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 864981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:34:06+00:00 2026-05-15T09:34:06+00:00

public class JFrameWithPanel extends JFrame implements ActionListener, ItemListener { int packageIndex; double price; double[]

  • 0
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();
                    ^
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:34:06+00:00Added an answer on May 15, 2026 at 9:34 am

    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 );
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class SaveData extends Activity implements OnClickListener { private DataManipulator dh; static final int
public class MCQSample extends Activity implements OnClickListener{ TextView title; String gotBread; RadioGroup AnswerRG; int
public class MainActivity extends Activity implements Runnable{ private int progressBarStatus = 0; private Handler
public class CheckedTextView extends TextView implements Checkable { private boolean mChecked; private int mCheckMarkResource;
public class abc<X extends Z> implements Iterable<X> { protected ArrayList<X> list; public Iterator<X> iterator()
public class saveButtonListener implements ActionListener{ public void actionPerformed(ActionEvent ev){ JFileChooser chooser= new JFileChooser(); String
public class A extends B implements C { } Class B and interface C
public class upload extends Activity { private static final int CAMERA_REQUEST = 1888; private
public class A { protected int b = 16; } public class B extends
public class MyActivity extends MapActivity implements LocationListener, OnClickListener { private MapView mapView; private MyItemizedOverlay

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.