need a little help with dialog/message boxes


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8

Thread: need a little help with dialog/message boxes

  1. #1
    Join Date
    Nov 2004
    Location
    Virginia
    Posts
    4

    Question need a little help with dialog/message boxes

    I'm pretty new to writing programs in java and i have a school assignment. First, i need to know how to change the text in the title bar of an input dialog box, a message dialog box and a confirm dialog box. Also, the way i have it set up is that in the first input box, i need to be able to know if the user hits 'cancel' and send the program to a different part. right now, if the user hits cancel, the whole program goes away and i get error messages that say 'number format exception: null' i'm so confused someone please help!
    i've attached the panel i created...maybe that will clear up what it is i'm trying to do.
    Attached Files Attached Files

  2. #2
    Join Date
    May 2004
    Location
    Durham, UK
    Posts
    174
    Hi,

    When you are parsing the integer if the number is invalid it will pass back a number format exception rather than passing back null, this is why you are getting an incompatible type on that line.

    change that code to

    Code:
                    try
                    {
                        guess = Integer.parseInt(numStr);
                    } 
                    catch(NumberFormatException nfe)
                    {
                        guess = -1;
                    }
    and see what difference it makes.
    Hope this helps.
    Graham

  3. #3
    Join Date
    Nov 2004
    Location
    Virginia
    Posts
    4
    this still doesn't work...
    after the user hits cancel, i want the program to go on and open a new box asking if they want to play again...
    your new code compiles fine, but it doesn't do what i'm working for. thanks anyways, any futher help would also be appreciated.

  4. #4
    Join Date
    May 2004
    Location
    Durham, UK
    Posts
    174
    Hi,

    It is the controller part of the program which should be deciding the flow of the program.

    Can you post the whole program not just the panel.

    Cheers
    Graham

  5. #5
    Join Date
    Nov 2004
    Location
    Virginia
    Posts
    4
    here's the main program...
    Attached Files Attached Files

  6. #6
    Join Date
    Nov 2004
    Location
    Virginia
    Posts
    4
    also, i still need to know how to change the text in the title bar, if anyone knows...

  7. #7
    Join Date
    Oct 2004
    Posts
    25

    Question

    http://java.sun.com/j2se/1.4.2/docs/...howInputDialog(java.awt.Component,%20java.lang.Object,%20java.lang.String,%20int)

    title parameter is the dialog title.

    floaty

  8. #8
    Join Date
    May 2004
    Location
    Durham, UK
    Posts
    174
    Hi,

    The key is the the dialog will return null if cancel is pressed.

    I have rewritten some of the loops and I think it works now.

    here we go

    Code:
    // File: HiLoPanel.java
    // Author: Amy Jackson
    // Class: CMSC 255, Section 001, Fall 2004
    // Description: Seperate class file for Proj5.java
    
    import javax.swing.*;
    import java.awt.*;
    import java.util.Random;
    
    public class HiLoPanel extends JPanel
    {
        
        Random generator = new Random();
        int num, guess, numGuesses, correct, again, redo;
        String numStr, result, error;
        
        public HiLoPanel()
        {
            JLabel saying = new JLabel("Amy Jackson, Project 5");
            
            do
            {
                num = generator.nextInt(100) + 1;
                numGuesses = 0;
                correct = 0;
                
                do
                {
                    numStr = JOptionPane.showInputDialog("Guess a number from 1 to 100.");
                    
                    // If the user has pressed cancel numstr will be null
                    
                    if(numStr!= null)
                    {
                        try
                        {
                            guess = Integer.parseInt(numStr);
                            
                            while (guess < 2 || guess > 99 && guess != -1)
                            {
                                error = JOptionPane.showInputDialog("That number is not between " +
                                "1 and 100. Please try again.");
                                guess = Integer.parseInt(numStr);
                            }
                            
                            result = "That number is too " + ((guess < num) ? "low. " : "high.") +
                            " Try again.";
                            numGuesses = numGuesses + 1;
                            
                            if (guess == num)
                            {
                                result = "CORRECT!  Great job.  It took you " + numGuesses
                                + " guesses.";
                                correct = 1;
                            }
                            
                            JOptionPane.showMessageDialog(null, result);
                        }catch(NumberFormatException nfe)
                        {
                            guess = -1;
                            correct = 1;
                        }
                    }
                    else
                    {
                        correct = 1;
                    }
                } while (correct != 1);
                
                again = JOptionPane.showConfirmDialog(null, "Play Again?");
                
            }  while (again == JOptionPane.YES_OPTION);
            
        }
    }
    hope this helps.
    Graham

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center