Results 1 to 16 of 16
  1. #1
    jwing7's Avatar
    jwing7 is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default My Very first Java Problem - i can't solve the ok button please help

    The problem:


    my only problem is in the


    private void OkActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    }


    The ok Button

    So here's my program about
    1. Create a person Class with the following Attributes
    String Fname
    String Lname
    Int age
    char MInitial
    -create constructors, setters, & getters
    -create a method name public void printDetails() //This method prints the details of a person

    -Create a gui Form where the person will fill up his, First name, Last name, Age, Middle Initial

    - In the GUI class create an array of 5 persons


    Button Functions
    Clear button - Clears all Input Fields

    ok Button - Submits all the personal info and places the person in the array and clears all input fields

    Print All = prints all personal info of all person in the array (in the output)

    It calls printDetails() method for every person in the array
    //don't display in the GUI

    Last edited by jwing7; 05-01-2011 at 05:17 PM. Reason: There's an error

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Trail: Creating a GUI With JFC/Swing: Table of Contents (The Java™ Tutorials)

    Read the sections on "How to Make Buttons" and "How to Write an ActionListener".

    If you have futher problem then post your Short, Self Contained, Correct Example that demonstrates the problem.

  3. #3
    jwing7's Avatar
    jwing7 is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default can u please make me a code of it?

    it doesnt solve my problem

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Your problem isn't stated. Taking a guess you should have the action listener extract all the info from the text fields, create a person object and stores it in the array. This is just a guess though, my crystal ball is broken right now.

  5. #5
    jwing7's Avatar
    jwing7 is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default

    my problem is i dont know how to put multiple functions in a button

  6. #6
    jwing7's Avatar
    jwing7 is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default

    what i mean is in the okay button

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'm confused what you mean by multiple functions. You can have as many statements as you want in the action listener for the button. You can even create a class which implements ActionListener and give it as many methods instance variables as you want, although this won't be needed for your assignment.

    You want the ok button to create a person object with the strings in the text fields, correct?

  8. #8
    jwing7's Avatar
    jwing7 is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default

    yah but when you press the ok button

    the inputted texts in the text fields will be stored in an array
    and while the inputted info will empty the text fields just like in the clear button

    once you press ok inputted texts will be stored in an array and clears everything in the textfields...

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Are you using another class called person or are you using x parallel arrays, where x is the number of text fields, if the latter you may want to consider creating a person class and then do some pseudo code like this
    Java Code:
    extract information from text fields
    create person object with text field values
    store person in array
    clear text areas
    You can clear the text area by calling setText("") on each of them.

  10. #10
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    my problem is i dont know how to put multiple functions in a button
    So where is your SSCCE that shows what you have attempted to do???

  11. #11
    jwing7's Avatar
    jwing7 is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default

    i don't understand the word parallel arrays.
    But i do understand this one

    extract information from text fields
    create person object with text field values
    store person in array
    clear text areas


    yah you can create information through filling up the text fields
    then once the filling up of information is done.
    t you will click the OK button, once you click it the information will be stored in an array and the text fields will be also cleared.

    my problem is i dont know how to put it in the array and at the same how to put the function of clearing up of textfields

  12. #12
    jwing7's Avatar
    jwing7 is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default

    what do you mean by ssce? sorry im a freshman student im just a newbie in this JAVA programming

    my problem is that putting up two functions at one button like in the OK button


    The function of the ok button is, Once the textfields are already filled up with information you will click ok, then clicking ok button will Clear up everything and the filled up information will be stored in an array

  13. #13
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Sorry, I forgot to include the link: Short, Self Contained, Correct Example

    The idea is to start simple and don't try to write your entire program from start to finish in one step.

    So in your particular case you start buy:

    a) adding a single text field and a button to your frame.
    b) add and ActionListener to your button. When you click the button you display the value in the text field and then clear the text field.

    Then once you know how to do this. You add the text field value to your array.

    then once you know how to do this. you add more text fields and create a person object. Then you add the text field text to your person object and add the person object to an array.

    If you have problems. Then you have simple code to post on the forum.
    Last edited by camickr; 05-01-2011 at 04:58 PM.

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    A parallel array is handling an object with multiple arrays, which is a bad idea. Something like this to represent a person
    Java Code:
    int[] numbers = new int[5];
    String[] names = new String[5];
    ...
    Where each person has the same i in each array. This is a bad approach. Do you have a Person class? The whole idea of multiple functions is unnecessary, the single action performed can do whatever you want(as much as you want)

    Java Code:
    public void actionPerformed(ActionEvent){
      extract text with the text field method getText()
      create person object with constructor Person(/*previously gotten variables*/)
      store in array, loop through array to find an empty spot, when found add item there
      erase text fields with setText("")
    }
    How action listeners work: when a button is clicked an action event is fired. Everything listening receives the ActionEvent and does what is said to be done in the actionPerformed method.

  15. #15
    jwing7's Avatar
    jwing7 is offline Member
    Join Date
    May 2011
    Posts
    9
    Rep Power
    0

    Default Thank You!!! ^_^

    Yah i've got the class,

    Thanks SUNDE887 :) i owe you one :)
    Last edited by jwing7; 05-01-2011 at 11:57 PM.

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You are welcome, glad to have helped. Please mark your thread solved with the thread tools at the top.

Similar Threads

  1. Problem Java Applet Button Connects to a Server
    By francojava1 in forum Java Applets
    Replies: 3
    Last Post: 09-09-2010, 07:48 PM
  2. Need Help With Java Code Checking & Problem Solve
    By tking88 in forum New To Java
    Replies: 4
    Last Post: 10-23-2009, 12:55 AM
  3. Need help to solve my problem
    By Copro in forum New To Java
    Replies: 4
    Last Post: 08-21-2009, 04:26 AM
  4. java applet button problem .. :( plz help!
    By i4gotmyid in forum Java Applets
    Replies: 1
    Last Post: 04-17-2008, 04:11 PM
  5. java applet button problem .. :( plz help
    By i4gotmyid in forum New To Java
    Replies: 0
    Last Post: 04-05-2008, 09:55 AM

Tags for this Thread

Posting Permissions

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