Results 1 to 3 of 3
  1. #1
    Klahking is offline Member
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    0

    Default Unable To Stop JOptionPane Warning In A While Statement

    I have tried everything I know to create a else inside of a while loop to take a false return value and put out a warning message pops up, lets you click ok, resets focus, clears field and allows for the correct entry to be made. I have included the section I am referring to as well as the entire GUI. It's 585 lines.

    The instructions are: If the title does not exist, you will alert them with a JOptionPane box that the product does not exist, clear all the fields, and set the focus in the name field. You will not create a product for a non-existing title.

    Java Code:
    // Music Method 
    	// Still need logic for returning warning message for title not found // might be able to create in Submit Handler
    	public void createMusic()
    	{
    		if(music.isSelected() && combo1.getSelectedIndex() != 0 && titleText != null)
    		{
    			Scanner input;
    			String fileScan = null;
    			
    			try
    			{
    				input = new Scanner(new File("C:/Users/Kerry Hawkins/workspace/HW2/src/music.txt"));
    				
    				
    				while(input.hasNext())
    				{
    					
    					fileScan = input.nextLine();
    					
    					if(titleText.getText().equals(fileScan))
    					{
    						if(input.hasNext() == true)
    						{
    							 artist = input.nextLine();
    							 noOfSongs = Integer.parseInt(input.nextLine());
    							 price = input.nextDouble();
    						}
    						
    					}// I tried an else satement for here, all I got was a repeating JOptionPane because I can't break out of the loop.
    					
    				}
    				
    				musicCreated = new Music(titleText.getText(), price, new Date(month, day, year), Music.GenreType.CLASSICAL, artist, noOfSongs);
    				
    				JOptionPane.showMessageDialog(null, "You have just purchased: " + "\n" + musicCreated, "Music Purchased!", JOptionPane.PLAIN_MESSAGE);
    				
    			}catch(IOException ex)
    			{
    				ex.printStackTrace();
    			}
    		}
    	}

    Java Code:
    // Kerry Hawkins
    // HW2
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    
    @SuppressWarnings("serial")
    public class SalesGUI extends JFrame 
    {
    	private JTextField nameText, streetText, cityText, stateText, zipText,
    			titleText, datePurchasedText, accountNumberText;
    	private JLabel nameLabel, streetLabel, cityLabel, stateLabel, zipLabel,
    			chooseOneLabel, musicTypeLabel, appTypeLabel, titleLabel,
    			datePurchasedLabel, accountNumberLabel;
    	private JButton submit, finish;
    	private ButtonGroup group;
    	private JRadioButton gameButton, productivityButton, educationButton;
    	private JCheckBox app, music;
    	private JComboBox combo1;
    	private JPanel musicTypePanel, softwareTypePanel;
    	private String artist;
    	private int noOfSongs;
    	private double price;
    	private String developer;
    	private App appCreated;
    	private Music musicCreated;
    
    	boolean input = true;
    
    	String[] names = { "CHOOSE ONE", "CLASSICAL", "COUNTRY", "ROCK" };
    
    	String statement = "";
    	
    	ArrayList<Customer> customerArray = new ArrayList<Customer>();
    
    	public SalesGUI() 
    	{
    		super("Mav Tunes                                                             SalesGUI.java");
    		setLayout(new GridLayout(12, 2));
    		
    		// Music JPanel
    		musicTypePanel = new JPanel();
    		musicTypePanel.setLayout(new GridLayout(1, 2));
    		add(musicTypePanel);
    
    		// APP JPanel
    		softwareTypePanel = new JPanel();
    		softwareTypePanel.setLayout(new GridLayout(1, 3));
    		add(softwareTypePanel);
    
    		// JLabels
    		nameLabel = new JLabel("Name");
    
    		streetLabel = new JLabel("Street");
    
    		cityLabel = new JLabel("City");
    
    		stateLabel = new JLabel("State");
    
    		zipLabel = new JLabel("Zip");
    
    		chooseOneLabel = new JLabel("Choose One");
    
    		musicTypeLabel = new JLabel("Type of Music");
    
    		appTypeLabel = new JLabel("Type of App");
    
    		titleLabel = new JLabel("Enter Title");
    
    		datePurchasedLabel = new JLabel("Date Purchased");
    
    		accountNumberLabel = new JLabel("Account Number");
    
    		// JTextFields
    		nameText = new JTextField(15);
    
    		streetText = new JTextField(15);
    
    		cityText = new JTextField(15);
    
    		stateText = new JTextField(15);
    
    		zipText = new JTextField(15);
    
    		titleText = new JTextField(15);
    
    		datePurchasedText = new JTextField(15);
    		datePurchasedText.setToolTipText("mm/dd/yyyy");
    
    		accountNumberText = new JTextField(15);
    
    		// JCheckBox with ItemListener
    		app = new JCheckBox("APP", false);
    		app.addItemListener(new ItemListener() 
    		{
    			public void itemStateChanged(ItemEvent ie) 
    			{
    				if (ie.getItem() == app) 
    				{
    					gameButton.setEnabled(true);
    					productivityButton.setEnabled(true);
    					educationButton.setEnabled(true);
    
    					if (app.isSelected()) 
    					{
    						combo1.setEnabled(false);
    						combo1.setSelectedIndex(0);
    						music.setSelected(false);
    					}
    
    					if (app.isSelected() == false) 
    					{
    						combo1.setEnabled(true);
    						music.setEnabled(true);
    						group.clearSelection();
    					}
    
    				}
    			}
    		});
    
    		// JCheckBox with ItemListener
    		music = new JCheckBox("MUSIC", false);
    		music.addItemListener(new ItemListener() 
    		{
    			public void itemStateChanged(ItemEvent ie) 
    			{
    				if (ie.getItem() == music) 
    				{
    					if (music.isSelected()) 
    					{
    						app.setSelected(false);
    						combo1.setEnabled(true);
    						group.clearSelection();
    						gameButton.setEnabled(false);
    						productivityButton.setEnabled(false);
    						educationButton.setEnabled(false);
    					}
    
    					if (music.isSelected() == false) 
    					{
    						gameButton.setEnabled(true);
    						productivityButton.setEnabled(true);
    						educationButton.setEnabled(true);
    						combo1.setSelectedIndex(0);
    					}
    
    				}
    			}
    		});
    
    		// JComboBox with no listeners
    		combo1 = new JComboBox(names);
    		combo1.setSelectedIndex(0);
    		combo1.setMaximumRowCount(3);
    
    		// JRadioButtons with no listeners
    		gameButton = new JRadioButton("GAME", false);
    
    		// JRadioButtons with no listeners
    		productivityButton = new JRadioButton("PRODUCTIVITY", false);
    
    		// JRadioButtons with no listeners
    		educationButton = new JRadioButton("EDUCATION", false);
    
    		// JButtonGroup 
    		group = new ButtonGroup();
    		group.add(gameButton);
    		group.add(productivityButton);
    		group.add(educationButton);
    
    		// JButton with inner class listeners below
    		submit = new JButton("SUBMIT");
    		finish = new JButton("FINISH");
    
    		// Submit Handler 
    		class SubmitButtonHandler implements ActionListener 
    		{
    			public void actionPerformed(ActionEvent ae) 
    			{
    				if (ae.getSource() == submit) 
    				{
    					checkInput();
    					
    					parseDate(); 
    					
    					Customer c = new Customer(nameText.getText(), new Address(streetText.getText(), 
    							cityText.getText(), stateText.getText(), Integer.parseInt(zipText.getText())), 
    							Integer.parseInt(accountNumberText.getText()));
    
    					if(music.isSelected())// need an error message to handle if title does not exist and also stops creation of MUSIC
    					{
    						createMusic();
    						c.addToProductList(musicCreated);
    					}
    					else // need an error message to handle if title does not exist and also stops creation of APP
    					{
    						createApp();
    						c.addToProductList(appCreated);
    					}
    					
    					customerArray.add(c);
    
    					JOptionPane.showMessageDialog(null, c, "Customer Created",	JOptionPane.PLAIN_MESSAGE);
    
    					if (c != null) 
    					{
    						clearAll();
    						nameText.requestFocus();
    					}
    				}
    
    			}
    		}
    
    		SubmitButtonHandler submitHandler = new SubmitButtonHandler();
    		submit.addActionListener(submitHandler);
    
    		// Finish Handler prints customer record
    		class FinishButtonHandler implements ActionListener 
    		{
    			public void actionPerformed(ActionEvent ae) 
    			{
    				if (ae.getSource() == finish) 
    				{
    					for (Customer c : customerArray) 
    					{
    						statement += c.createHistory();
    					}
    
    					JOptionPane.showMessageDialog(null, statement,"Purchase History", JOptionPane.PLAIN_MESSAGE);
    					
    					statement = "";
    				}
    
    				clearAll();
    				nameText.requestFocus();
    			}
    
    		}
    
    		FinishButtonHandler finishHandler = new FinishButtonHandler();
    		finish.addActionListener(finishHandler);
    
    		// ADD Label and Text Components
    		add(nameLabel);
    		add(nameText);
    		add(streetLabel);
    		add(streetText);
    		add(cityLabel);
    		add(cityText);
    		add(stateLabel);
    		add(stateText);
    		add(zipLabel);
    		add(zipText);
    
    		// ADD APP and MUSIC JPanel
    		add(chooseOneLabel);
    		add(musicTypePanel);
    		musicTypePanel.add(app);
    		musicTypePanel.add(music);
    
    		// ADD JCombo Box
    		add(musicTypeLabel);
    		add(combo1);
    
    		// ADD Software JPanel
    		add(appTypeLabel);
    		add(softwareTypePanel);
    		softwareTypePanel.add(gameButton);
    		softwareTypePanel.add(productivityButton);
    		softwareTypePanel.add(educationButton);
    
    		// ADD Label and Text Components
    		add(titleLabel);
    		add(titleText);
    		add(datePurchasedLabel);
    		add(datePurchasedText);
    		add(accountNumberLabel);
    		add(accountNumberText);
    
    		// Add JButtons
    		add(submit);
    		add(finish);
    
    	}
    
    	// Clear all fields, Check boxes, combination box and radio buttons 
    	public void clearAll() 
    	{
    		if (nameText.getText() != null) 
    		{
    			nameText.setText(null);
    		}
    
    		if (streetText.getText() != null) 
    		{
    			streetText.setText(null);
    		}
    
    		if (cityText.getText() != null) 
    		{
    			cityText.setText(null);
    		}
    
    		if (stateText.getText() != null) 
    		{
    			stateText.setText(null);
    		}
    
    		if (zipText.getText() != null) 
    		{
    			zipText.setText(null);
    		}
    
    		if (app.isSelected()) 
    		{
    			app.setSelected(false);
    		}
    
    		if (music.isSelected()) 
    		{
    			music.setSelected(false);
    		}
    
    		if (combo1.getSelectedIndex() != 0) 
    		{
    			combo1.setSelectedIndex(0);
    		}
    
    		group.clearSelection();
    
    		if (titleText.getText() != null) 
    		{
    			titleText.setText(null);
    		}
    
    		if (datePurchasedText.getText() != null) 
    		{
    			datePurchasedText.setText(null);
    		}
    
    		if (accountNumberText.getText() != null) 
    		{
    			accountNumberText.setText(null);
    		}
    
    	}
    
    	// Music Method 
    	// Still need logic for returning warning message for title not found // might be able to create in Submit Handler
    	public void createMusic()
    	{
    		if(music.isSelected() && combo1.getSelectedIndex() != 0 && titleText != null)
    		{
    			Scanner input;
    			String fileScan = null;
    			
    			try
    			{
    				input = new Scanner(new File("C:/Users/Kerry Hawkins/workspace/HW2/src/music.txt"));
    				
    				
    				while(input.hasNext())
    				{
    					
    					fileScan = input.nextLine();
    					
    					if(titleText.getText().equals(fileScan))
    					{
    						if(input.hasNext() == true)
    						{
    							 artist = input.nextLine();
    							 noOfSongs = Integer.parseInt(input.nextLine());
    							 price = input.nextDouble();
    						}
    						
    					}
    					
    				}
    				
    				musicCreated = new Music(titleText.getText(), price, new Date(month, day, year), Music.GenreType.CLASSICAL, artist, noOfSongs);
    				
    				JOptionPane.showMessageDialog(null, "You have just purchased: " + "\n" + musicCreated, "Music Purchased!", JOptionPane.PLAIN_MESSAGE);
    				
    			}catch(IOException ex)
    			{
    				ex.printStackTrace();
    			}
    		}
    	}
    
    	// APP method 
    	// Still need logic for returning warning message for title not found // might be able to create in Submit Handler
    	public void createApp()
    	{
    		if(app.isSelected() && group.getSelection() != null && titleText != null)
    		{
    			Scanner input;
    			String fileScan;
    			
    			try
    			{
    				input = new Scanner(new File("C:/Users/Kerry Hawkins/workspace/HW2/src/app.txt"));
    				
    				while(input.hasNext())
    				{
    					fileScan = input.nextLine();
    					
    					if(titleText.getText().equals(fileScan))
    					{
    						if(input.hasNext() == true)
    						{
    							developer = input.nextLine();
    							price = input.nextDouble();
    						}
    						
    					}
    				}
    					
    				
    				appCreated = new App(titleText.getText(), price, new Date(month, day, year),App.Type.GAME, developer);
    				
    				JOptionPane.showMessageDialog(null, "You have just purchased: " + "\n" + appCreated, "Application Purchased!", JOptionPane.PLAIN_MESSAGE);
    				
    				
    			}catch(IOException ex)
    			{
    				ex.printStackTrace();
    			}
    		}
    	}
    
    	// Variables for parseDate Method()
    	int index1;
    	int index2;
    	int month;
    	int day;
    	int year;
    
    	// Date Method
    	public void parseDate() 
    	{
    		String dP = datePurchasedText.getText();
    
    		index1 = dP.indexOf("/");
    		month = Integer.parseInt(dP.substring(0, index1));
    		index1++;
    		index2 = dP.lastIndexOf("/");
    		day = Integer.parseInt(dP.substring(index1, index2));
    		index2++;
    		year = Integer.parseInt(dP.substring(index2, dP.length()));
    		
    	}
    	
    	// CheckInput Method
    	public boolean checkInput() 
    	{
    
    		String myText = nameText.getText();
    		String myText1 = streetText.getText();
    		String myText2 = cityText.getText();
    		String myText3 = stateText.getText();
    		String myText4 = zipText.getText();
    		String myText5 = titleText.getText();
    		String myText6 = datePurchasedText.getText();
    		String myText7 = accountNumberText.getText();
    
    		if (myText == null || myText.length() == 0) 
    		{
    			JOptionPane.showMessageDialog(null, "Please enter your name", "Mav Tunes", JOptionPane.WARNING_MESSAGE);
    
    			input = false;
    
    			nameText.requestFocus();
    
    		}
    
    		else if (myText1 == null || myText1.length() == 0) 
    		{
    
    			JOptionPane.showMessageDialog(null, "Please enter your street",	"Mav Tunes", JOptionPane.WARNING_MESSAGE);
    
    			input = false;
    
    			streetText.requestFocus();
    
    		}
    
    		else if (myText2 == null || myText2.length() == 0) 
    		{
    
    			JOptionPane.showMessageDialog(null, "Please enter your city", "Mav Tunes", JOptionPane.WARNING_MESSAGE);
    
    			input = false;
    
    			cityText.requestFocus();
    
    		}
    
    		else if (myText3 == null || myText3.length() == 0) 
    		{
    
    			JOptionPane.showMessageDialog(null, "Please enter your state", "Mav Tunes", JOptionPane.WARNING_MESSAGE);
    
    			input = false;
    
    			stateText.requestFocus();
    
    		}
    
    		else if (myText4 == null || myText4.length() == 0) 
    		{
    
    			JOptionPane.showMessageDialog(null, "Please enter your zip", "Mav Tunes", JOptionPane.WARNING_MESSAGE);
    
    			input = false;
    
    			zipText.requestFocus();
    
    		}
    
    		else if (app.isSelected() == false && (music.isSelected() == false)) 
    		{
    			JOptionPane.showMessageDialog(null,	"Please select the APP or MUSIC type", "Mav Tunes",	JOptionPane.WARNING_MESSAGE);
    
    			input = false;
    		}
    		
    		else if (app.isSelected() == true && group.getSelection() == null) 
    		{
    			JOptionPane.showMessageDialog(null,	"Please select the application type");
    
    			input = false;
    
    			app.requestFocus();
    		}
    
    		else if (music.isSelected() == true && combo1.getSelectedIndex() == 0) 
    		{
    			JOptionPane.showMessageDialog(null, "Please select the music genre");
    
    			input = false;
    
    			combo1.requestFocus();
    		}
    
    		else if (myText5 == null || myText5.length() == 0) 
    		{
    
    			JOptionPane.showMessageDialog(null, "Please enter title", "Mav Tunes", JOptionPane.WARNING_MESSAGE);
    
    			input = false;
    
    			titleText.requestFocus();
    
    		}
    
    		else if (myText6 == null || myText6.length() == 0) 
    		{
    
    			JOptionPane.showMessageDialog(null, "Please enter the date", "Mav Tunes", JOptionPane.WARNING_MESSAGE);
    
    			input = false;
    
    			datePurchasedText.requestFocus();
    
    		}
    
    		else if (myText7 == null || myText7.length() == 0) 
    		{
    
    			JOptionPane.showMessageDialog(null, "Please enter your account number", "Mav Tunes", JOptionPane.WARNING_MESSAGE);
    
    			input = false;
    
    			accountNumberText.requestFocus();
    
    		}
    
    		return true;
    	}
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    Too much code.
    Can you make a small program to demo your problem.


    Did you try using a break statement to exit the loop?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

Similar Threads

  1. Use stop button to stop moving (stop timers) on JPanel
    By mneskovic in forum New To Java
    Replies: 3
    Last Post: 07-23-2010, 12:50 PM
  2. help with JOptionPane and if/else statement
    By surg3y3 in forum New To Java
    Replies: 3
    Last Post: 04-01-2010, 01:51 AM
  3. Giving Warning
    By tanmoy.b81 in forum New To Java
    Replies: 1
    Last Post: 12-24-2008, 09:21 AM
  4. Replies: 2
    Last Post: 12-22-2008, 03:05 AM
  5. Replies: 2
    Last Post: 12-06-2008, 06:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •