Results 1 to 10 of 10

Thread: GUI in Netbeans

  1. #1
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default GUI in Netbeans

    I've spent the last couple of days get my code to work using two classes. I used the Scanner class to get user input (in this case an integer). I ran that input through a series of loops to detect whether it was, in fact, an integer and whether is was greater than or equal to 0. I got everything working flawlessly, but...

    The assignment I'm working on requires that I use a GUI for user input and to display the output. I've created and designed my JFrame. I've already parsed the string value to an integer type. My problem is verifying the input IS a number (not a character) and that it's greater than 0. I tried to use a while loop. When I ran the program and I enter either a character or a negative number my error box appears (which is what I want), but I could not close it. I suspect that it was because of the loop I had written.

    Before trying to put this in a GUI, I'd capture the input and check to see if it wasn't a number (!in.hasNextInt()). I obviously can't do that now...or can I? Life would be much simpler! Hopefully I haven't confused anyone.

  2. #2
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    My problem is verifying the input IS a number (not a character)
    Can I see this code?

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,889
    Rep Power
    8

    Default

    If you want help, you'll have to provide an SSCCE that demonstrates what you're actually doing, in as few lines as possible. Without it, we're just guessing.

    PS- Cool name.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    You have not showed me verifying input is a number, you have showed me veryifying an integer is greater than zero. Not the same thing! What would you need to study to get close to your answer? How to verify input is number perhaps.

    Have a look at the Character class and her methods.

  5. #5
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    I just figured out what I needed to do. I orginally had my error check inside an event action for a button. I've moved it to an event action of the text box it is being entered into. I didn't need to use a while loop...instead used try-catch, and an if statement.

    And thanks...I've been using this handle since high school (...many years ago...lol)

    Let me know if this looks okay...it's working, but maybe it's not perfect??

    Java Code:
        private void speedFieldActionPerformed(java.awt.event.ActionEvent evt) {                                           
            try {
    
                int speed = Integer.parseInt(this.speedField.getText());
                if (speed < 0) {
                    JOptionPane.showMessageDialog(this, "Number must be greater"
                            + "than or equal to 0!", "ERROR", JOptionPane.ERROR_MESSAGE);
                }
    
            } catch (Exception e) {
                JOptionPane.showMessageDialog(this, "Cannot be a letter!", "ERROR",
                        JOptionPane.ERROR_MESSAGE);
            }
        }

  6. #6
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    what happens if speedField.getText().equals("non number");?

  7. #7
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    If I place a letter in the box and press enter, an error box will pop up saying that it wasn't a number. That's what the catch statement is saying. Besides, a string cannot be passed into an integer. But I will try what you suggested and see what happens.

  8. #8
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    You are fine, you have found a solution and understood how it works.

  9. #9
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    Now I've realized another problem. I have another class, and I need to create an instance with parameters (one of them being "speed"...the other "hours"). I have two text boxes...one for each variable. These variables are locked to the event actions.

    I have an event action set on a button (this is where I tried to created an object). From there, I want to call a method that does a particular calculation (I know how to do that) and display it in a Text Area (I know how to do this too). My problem is having access to those variables. Now that I think of it...should I define the variables at the top of the JFrame source code?? And will this allow me to access the values they contain??

  10. #10
    Kevinius is offline Member
    Join Date
    Mar 2011
    Posts
    46
    Rep Power
    0

    Default

    I just answered my question! I do appreciate the help I received though. I'm sure I'll have a question about something else in the future!

Similar Threads

  1. Help in NetBeans
    By Phanindra in forum NetBeans
    Replies: 8
    Last Post: 07-01-2010, 01:47 PM
  2. Netbeans Help
    By MuslimCoder in forum New To Java
    Replies: 1
    Last Post: 11-07-2009, 02:23 AM
  3. Using SGT API in netbeans
    By Manfizy in forum New To Java
    Replies: 1
    Last Post: 02-13-2009, 10:20 AM
  4. help with netBeans
    By the godfather01 in forum NetBeans
    Replies: 11
    Last Post: 02-01-2009, 04:48 PM
  5. Hi Am New To Use Netbeans
    By arunkumarinfo in forum NetBeans
    Replies: 1
    Last Post: 01-23-2009, 04:18 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
  •