GUI window disposal and JOptionPane question

stupid GUI question that I cannot find the answer to

Page 1 of 1

4 Replies - 3269 Views - Last Post: 10 November 2009 - 06:48 PM Rate Topic: -----

#1 noctolater  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 72
  • Joined: 29-April 09

GUI window disposal and JOptionPane question

Posted 09 November 2009 - 08:48 PM

I am having a strange error when I try to create a new GUI window. I thought that frame.dispose() would basically destroy the frame and let me reuse the overall JFrame again, but it's not. Instead it's basically adding the same Frame objects to the frame again. What can I do to change this? I have never written a program using more than one window before, and it's a little confusing :P

Also, does anyone know how to add actionListeners to JOptionPane.showConfirmDialog buttons? That way I can tell what the input is when I ask the yes/no/cancel questions. Thanks so much for your help!

Edit: Sorry about not showing code, here it is.

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

/**
 * Write a description of class GUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class GUI
{
	private static Student student = new Student();
	private static JFrame frame = new JFrame ("EDSOFT");
	public static void main()
	{
		GUI.mainScreen(frame.getContentPane());
	}
	
	/**
	 * the default screen seen when the program starts.
	 */
	public static void mainScreen(Container pane)
	{
		pane.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		JLabel greeting = new JLabel ("Hello Elaine, what would you like to do?");
		JButton changeName = new JButton ("Change the student's name");
		JButton changeID = new JButton ("Change the student's ID#");
		JButton enterGrades = new JButton ("Enter the student's grades");
		JButton printReport = new JButton ("Print a grade report");
		
		c.gridwidth = 4;
		c.gridx = 0;
		c.gridy = 0;
		pane.add(greeting, c);
		
		c.fill = GridBagConstraints.BOTH;
		c.gridwidth = 1;
		c.gridx = 0;
		c.gridy = 1;
		pane.add(changeName, c);
		changeName.addActionListener (new ActionListener()
		{
			/**
			 * go into changing the student's name.
			 * 
			 * @param theEvent	  and ActionEvent
			 */
			public void actionPerformed (ActionEvent theEvent)
			{
				frame.setVisible(false);
				frame.dispose();
				GUI.changeStudentsName();
				GUI.main();
			}
		});
		
		c.gridx = 1;
		c.gridy = 1;
		pane.add(changeID, c);
		changeID.addActionListener (new ActionListener()
		{
			/**
			 * respond to a button press.
			 * 
			 * @param theEvent	  and ActionEvent
			 */
			public void actionPerformed (ActionEvent theEvent)
			{
				frame.setVisible(false);
				frame.dispose();
				GUI.changeStudentsID();
				GUI.main();
			}
		});
		
		c.gridx = 2;
		c.gridy = 1;
		pane.add(enterGrades, c);
		enterGrades.addActionListener (new ActionListener()
		{
			/**
			 * respond to a button press.
			 * 
			 * @param theEvent	  and ActionEvent
			 */
			public void actionPerformed (ActionEvent theEvent)
			{
				frame.setVisible(false);
				frame.dispose();
				GUI.changeGrades(frame.getContentPane());
				GUI.main();
			}
		});
		
		c.gridx = 3;
		c.gridy = 1;
		pane.add(printReport, c);
		printReport.addActionListener (new ActionListener()
		{
			/**
			 * respond to a button press.
			 * 
			 * @param theEvent	  and ActionEvent
			 */
			public void actionPerformed (ActionEvent theEvent)
			{
				System.out.println("This is a placeholder:");
				System.out.println("The student is named " + student.getLastName() + ", " + student.getFirstName() + ".");
				System.out.println("The student has the ID# of " + student.getID());
				System.out.println("The student has gotten " + student.findGPA() + "% in the class");
			}
		});
		
		frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
	
	/**
	 * changeStudentsName starts the window for changing the student's name.
	 */
	public static void changeStudentsName ()
	{
		String name = JOptionPane.showInputDialog (null, "Please enter the student's name as 'Last Name, First Name'.");

		try
		{
			name.indexOf(", ");
		}
		catch (Exception e)
		{
			name = JOptionPane.showInputDialog (null, "That is not a valid name, please enter the student's name as 'Last Name, First Name' again.");
		}
		
		int i = name.indexOf(", ");
		student.setLastName(name.substring(0, i));
		while (name.substring(i, i + 1).equals(",") || name.substring(i, i + 1).equals(" "))
		{
			i++;
		}
		student.setFirstName(name.substring(i));
		
		JOptionPane.showConfirmDialog(null, "The student's name is now " + student.getFirstName() + 
									  " " + student.getLastName() + ". Is this correct?");
	}
	
	/**
	 * changeStudentID starts the window for changing the student's ID.
	 */
	public static void changeStudentsID ()
	{
		String ID = JOptionPane.showInputDialog (null, "Please enter the student's ID#.");
		
		while (ID.length() != 8)
		{
			ID = JOptionPane.showInputDialog (null, "That is not a valid ID#, please enter the ID# again.");
		}
		
		try
		{
			Integer.parseInt(ID);
		}
		catch (Exception e)
		{
			ID = JOptionPane.showInputDialog (null, "That is not a valid ID#, please enter the ID# again.");
		}
		
		student.setID(ID);
		
		JOptionPane.showConfirmDialog(null, "The student's ID is now " + student.getID() + ". Is this correct?");
	}
	
	/**
	 * changeGrades starts the window for changing the student's grades.
	 */
	public static void changeGrades (Container pane)
	{
		pane.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		JLabel greeting = new JLabel ("Would you like to enter all the grades at once, or just a specific assignment?");
		JButton allGrades = new JButton ("Enter all the grades at once");
		JButton oneGrade = new JButton ("Just enter a specific assignment");
		
		c.gridwidth = 2;
		c.gridx = 0;
		c.gridy = 0;
		pane.add(greeting, c);
		
		c.fill = GridBagConstraints.BOTH;
		c.gridwidth = 1;
		c.gridx = 0;
		c.gridy = 1;
		pane.add(allGrades, c);
		allGrades.addActionListener (new ActionListener()
		{
			/**
			 * respond to a button press.
			 * 
			 * @param theEvent	  and ActionEvent
			 */
			public void actionPerformed (ActionEvent theEvent)
			{
				System.exit(0);
			}
		});
		
		c.gridx = 1;
		c.gridy = 1;
		pane.add(oneGrade, c);
		oneGrade.addActionListener (new ActionListener()
		{
			/**
			 * respond to a button press.
			 * 
			 * @param theEvent	  and ActionEvent
			 */
			public void actionPerformed (ActionEvent theEvent)
			{
				frame.setVisible(false);
				frame.dispose();
				GUI.setOneGrade();
				GUI.main();
			}
		});
	}
	
	/**
	 * setOneGrade asks the user to select what grade they want to set, and then what it should be set as.
	 */
	public static void setOneGrade ()
	{
		
	}
		
}


This post has been edited by noctolater: 09 November 2009 - 09:23 PM


Is This A Good Question/Topic? 0
  • +

Replies To: GUI window disposal and JOptionPane question

#2 theautokustomizer  Icon User is offline

  • D.I.C Regular

Reputation: 16
  • View blog
  • Posts: 250
  • Joined: 20-September 09

Re: GUI window disposal and JOptionPane question

Posted 09 November 2009 - 08:53 PM

We here at D.I.C. like to some signs of good faith on your part and show us what you have to this point in code. And don't forget to use the code tags...TY
Was This Post Helpful? 0
  • +
  • -

#3 noctolater  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 72
  • Joined: 29-April 09

Re: GUI window disposal and JOptionPane question

Posted 10 November 2009 - 08:55 AM

This is a much consolidated version of my previous post. Instead of boring you with all my code, I'll just show you two methods. I have built it so that every new GUI window is it's own method, essentially. Though, if there's a better way of doing that, can someone let me know? Most of the GUI tutorials I have found are for a single frame, and not multiple windows.

I am having a strange error when I try to create a new GUI window. I thought that frame.dispose() would basically destroy the frame and let me reuse the overall JFrame again, but it's not. Instead, when I go to reuse the window, it's basically adding the same Frame objects to the frame again. What can I do to change this? I have never written a program using more than one window before, and it's a little confusing :P

public class GUI
{
	private static Student student = new Student();
	private static JFrame frame = new JFrame ("EDSOFT");
	public static void main()
	{
		frame.setVisible(false);
		frame.dispose();
		GUI.mainScreen(frame.getContentPane());
	}


	/**
	 * changeGrades starts the window for changing the student's grades.
	 */
	public static void changeGrades (Container pane)
	{
		pane.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		JLabel greeting = new JLabel ("Would you like to enter all the grades at once, or just a specific assignment?");
		JButton allGrades = new JButton ("Enter all the grades at once");
		JButton oneGrade = new JButton ("Just enter a specific assignment");
		
		c.gridwidth = 2;
		c.gridx = 0;
		c.gridy = 0;
		pane.add(greeting, c);
		
		c.fill = GridBagConstraints.BOTH;
		c.gridwidth = 1;
		c.gridx = 0;
		c.gridy = 1;
		pane.add(allGrades, c);
		allGrades.addActionListener (new ActionListener()
		{
			/**
			 * respond to a button press.
			 * 
			 * @param theEvent	  and ActionEvent
			 */
			public void actionPerformed (ActionEvent theEvent)
			{
				frame.setVisible(false);
				frame.dispose();
				GUI.setAllGrades(frame.getContentPane());
				GUI.main();
			}
		});
		
		c.gridx = 1;
		c.gridy = 1;
		pane.add(oneGrade, c);
		oneGrade.addActionListener (new ActionListener()
		{
			/**
			 * respond to a button press.
			 * 
			 * @param theEvent	  and ActionEvent
			 */
			public void actionPerformed (ActionEvent theEvent)
			{
				frame.setVisible(false);
				frame.dispose();
				GUI.setOneGrade(frame.getContentPane());
				GUI.main();
			}
		});
	}


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: GUI window disposal and JOptionPane question

Posted 10 November 2009 - 06:06 PM

View Postnoctolater, on 9 Nov, 2009 - 07:48 PM, said:

I am having a strange error when I try to create a new GUI window. I thought that frame.dispose() would basically destroy the frame and let me reuse the overall JFrame again, but it's not. Instead it's basically adding the same Frame objects to the frame again. What can I do to change this? I have never written a program using more than one window before, and it's a little confusing :P

Also, does anyone know how to add actionListeners to JOptionPane.showConfirmDialog buttons? That way I can tell what the input is when I ask the yes/no/cancel questions. Thanks so much for your help!

Edit: Sorry about not showing code, here it is.

No it destroys the Frame so you cannot use it anymore...you will have to create another one
You do not need ActionListener of JOptionPane when you
int status = JOptionPane......
the method returns only after the user has clicked on one of the button and the button cliked is in the variable status
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: GUI window disposal and JOptionPane question

Posted 10 November 2009 - 06:48 PM

Duplicated topics merged
Please avoid double posts :angry:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1