Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default JOptionPane Help

    Okay, so i've been trying to create a gui that will process my method. For the most part it works, however there are a few hiccups.
    I want the dialog box to pop back up after the person hits okay for the second time (i.e. after the method runs)
    Also, whenever I use the cancel button I get
    Java Code:
    Exception in thread "main" java.lang.NumberFormatException: null
    	at java.lang.Integer.parseInt(Unknown Source)
    	at java.lang.Integer.parseInt(Unknown Source)
    	at NumbersToWords.main(NumbersToWords.java:11)
    here's my code thus far:
    Java Code:
    public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		String entry;
    		entry = JOptionPane.showInputDialog(null, "Input numerical number:");
    		int number = Integer.parseInt(entry);
    		JOptionPane.showMessageDialog(null,input(number));
    		System.exit(0);
    	}

  2. #2
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: JOptionPane Help

    any help?

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: JOptionPane Help

    int number = Integer.parseInt(entry);
    Because when you hit cancel, entry is null, and how would one derive a number from a null using parseInt? You need to check for null, and not try to parse it if it is (null).

  4. #4
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: JOptionPane Help

    ahh, okay thanks. Now how about having the initial dialogue box pop back up prompting again for a number

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: JOptionPane Help

    Just put the popup statement in a while loop that loops until the input is valid! :D

  6. #6
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: JOptionPane Help

    not really seeing how to do that. I've tried a few ways, but not working for me

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: JOptionPane Help

    Java Code:
    import javax.swing.JOptionPane;
    public class Tester{
        public static void main(String[] args){
            new Tester();
        }
        
        public Tester(){
            String input;
            int num = -1;
            do{
                input = JOptionPane.showInputDialog("Input a number!");
                if(input != null){
                    try{
                        num = Integer.parseInt(input);
                    }catch(Exception e){
                        input = null;
                        continue;
                    }
                } else{
                    continue;
                }
            } while(input == null);
            System.out.println("The number was: "+num);
        }
    
    }

  8. #8
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: JOptionPane Help

    ahhh, thank you!, So now what about after the message pops up and you hit okay, the program stops. How do I get it to return to the initial prompt menu?

  9. #9
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: JOptionPane Help

    well, actually now everytime I hit cancel at the initial menu the box just pops back up. The only way out is to put in a proper number

  10. #10
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: JOptionPane Help

    Haha, yeah, I kinda wrote it like that. What you can do is on the check for null, replace the continue with a 'break'. That'll kill the loop! :D

  11. #11
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: JOptionPane Help

    How do I get it to return to the initial prompt menu?
    Well, it depends on how complex you're making this thing. For ease of expandability you could put all your important sections from your main 'menu' into separate methods. That makes repeating certain parts simple, and keeps your main loop small. The second thing is you can nest loops.

    For instance, this pseudocode might help describe:

    Java Code:
    while(program is running){
        Ask the user to make a choice
        while(user inputs nothing or crap){
            ask the user for a valid number
        }
        if (canceled){
            quit the app
        } else{
            pick correct action based on user input (hint: switch statement)
        }
        if we made it this far, user input good input, now lets loop again (back to top!)
    }

  12. #12
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: JOptionPane Help

    okay, didn't quite get that to work. What about creating a box and the output is printed right below AS you are typing.
    so...
    if I start typing 567 the progression would be: five -> fifty-six -> five hundred sixty-seven

  13. #13
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: JOptionPane Help

    To get it to print while typing, I think you'll need to use something other than a JOptionPane.showInputDialog(). The method I showed using this only gets the value after a button is clicked. You would have to make your own GUI window, and check the contents of the text field after each key press. This is doable, but it is more work. I've written apps that do this to do kind of a 'live search' style result set.

  14. #14
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: JOptionPane Help

    teach me? lol

  15. #15
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: JOptionPane Help

    ha alright, lemme see here

    Java Code:
    import javax.swing.*;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    public class LivePrint {
        public static void main(String[] args){
            new LivePrint();
        }
    
        JTextField input, output;
        
        public LivePrint(){
            JFrame window = new JFrame("Live Printer");
            JPanel livePanel = new JPanel();
            input = new JTextField("Type Here", 30);
            input.addKeyListener(new LiveListener());
            output = new JTextField("Output happens here live!", 30);
            
            livePanel.add(input);
            livePanel.add(output);
            
            window.add(livePanel);
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //enable close button
            window.pack(); //shrink window around components
            window.setLocationRelativeTo(null); //center window
            
            window.setVisible(true);
        }
        
        class LiveListener implements KeyListener{
            public void keyTyped(KeyEvent keyEvent) {}
    
            public void keyPressed(KeyEvent keyEvent) {}
    
            public void keyReleased(KeyEvent keyEvent) {
                output.setText(input.getText());
            }
        }
    }

  16. #16
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: JOptionPane Help

    I hate to be an annoyance, but would you mind explaining that to me a bit, and telling me how I could put that to use with my class

  17. #17
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: JOptionPane Help

    if it helps, the class name is "NumbersToWords" and the method that I am calling is 'input(int number)'

  18. #18
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: JOptionPane Help

    No problem, so I commented to code, hope that helps, and then more info below
    Java Code:
    public class LivePrint {
        //Our main method creates a new instance of LivePrint
        public static void main(String[] args){
            new LivePrint();
        }
        
        //Our text fields need to be at the class level so that they are
        //in scope for our keyListener
        JTextField input, output;
         
        //The constructor for our little app
        public LivePrint(){
            //Create a window on the screen
            JFrame window = new JFrame("Live Printer");
            //Create a panel to told all the gui components
            JPanel livePanel = new JPanel();
            //Instantiate the input field with size in columns
            input = new JTextField("Type Here", 30);
            //Add a key listener to the text field so we can do something when a key is pressed
            input.addKeyListener(new LiveListener());
            //Instantiate the output text field
            output = new JTextField("Output happens here live!", 30);
            //Add the input text field to the panel
            livePanel.add(input);
            //Add the output text field to the panel
            livePanel.add(output);
            //Add the panel to the window
            window.add(livePanel);
            window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //enable close button
            window.pack(); //shrink window around components
            window.setLocationRelativeTo(null); //center window
            //Show the window on the screen
            window.setVisible(true);
            //You could add a loop or other code here if needed for something
        }
         
        //An implementation of the KeyListener interface, 
        //this defines what happens when a key is pressed
        class LiveListener implements KeyListener{
            //Not implemented (don't care about typed events)
            public void keyTyped(KeyEvent keyEvent) {}
     
            //Not implemented (don't care about pressed events)
            public void keyPressed(KeyEvent keyEvent) {}
            
            //When use lets off a key on the keyboard, do something
            public void keyReleased(KeyEvent keyEvent) {
                //Set the text of our output text field to whatever the text currently is
                //in the input text field
                //This happens every 'release' giving us a live text feel
                output.setText(input.getText());
            }
        }
    }
    You would want to do more than just set the text of the output field. You would want to perform your program logic in the keyReleased() method, so that every time it is called, your program refreshes. In the case of a search, if you typed C-A-R-T
    it would first search for C then CA then CAR then CART and so forth. You would want to add other methods, such as a search method which could be called from inside this action listener. You could add buttons or text areas or whatever you like in addition to / instead of the textFields.

    Does that help?

  19. #19
    adjit is offline Senior Member
    Join Date
    Mar 2011
    Posts
    171
    Rep Power
    0

    Default Re: JOptionPane Help

    it definitely helps a lot! thank you! I just don't really understand where I would call the method that translates the inputed number into words or how I would go about doing that. due to your comments I understand what that code does, I just don't understand how to put that into my other class. Like would I call the LivePrint in my NumbersToWords main method? or would I create in the keyReleased method a new NumbersToWords thing and then call the method within that class in that keyReleased method

  20. #20
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: JOptionPane Help

    I made up live print. You would want to implement a similar structure but with your names for things. Basically you jut need the guts from the app I posted -- The GUI parts, and a KeyListener subclass.
    The GUI parts could just be pasted into your constructor and class body, and then somewhere in your class, you can subclass KeyListener like I did, and it can be called anything, like SuperSearchListener or whatever.

Page 1 of 2 12 LastLast

Similar Threads

  1. JOptionPane
    By louiedogg418 in forum New To Java
    Replies: 3
    Last Post: 07-03-2011, 07:04 PM
  2. Help with JOptionPane
    By sanctishit in forum New To Java
    Replies: 1
    Last Post: 03-10-2010, 11:20 AM
  3. Help with JOptionPane
    By SwEeTAcTioN in forum New To Java
    Replies: 12
    Last Post: 11-03-2009, 05:12 AM
  4. JOptionPane
    By Mir in forum New To Java
    Replies: 5
    Last Post: 11-29-2008, 03:04 AM
  5. JOptionPane
    By whosadork in forum New To Java
    Replies: 2
    Last Post: 10-23-2008, 03:17 AM

Posting Permissions

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