Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Continuing to create an object with the press of a button.

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Continuing to create an object with the press of a button.

    The current project I am working on involves two classes. A student class and a GUI class. We will use the GUI class to continue to make the Student object. The Student class contains major, name, balance, and address. I can make a GUI just fine and I can make a Student constructor and methods. My problem, what I struggle with is arrays and loops. I need to have the Student class array and associated counter variable be a static variable and every time the user enters the information for the Student class(name, address, balance, major) it adds another Student object to the JTextArea. In essence everytime the JButton is pressed the JTextArea updates with the new student's info without deleting the old info. Could somebody provide an example of how to do this?


  2. #2
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Continuing to create an object with the press of a button.

    Do you have any code for the JButton click event yet, if you do please post it so we can see what you have so far. Please use code tags around the code you post.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Continuing to create an object with the press of a button.

    Here is the button event within my GUI class and my complete Student class.

    private class ButtonListener implements ActionListener{
    		public void actionPerformed(ActionEvent event) {
     
    			Object srcObj = event.getSource();
    			if (event.getSource() == submitButton){
    				STextArea = student.getText();
    				displayInfo.setText(displayInfo.getText().concat(STextArea));
     
     
    				}
    			}
    }
    ____________________________________________________________________________________________
    public class Student extends GUI{
     
     
    	public Student(String name, String address, double balance, String major) {
    		nameString = name;
    		addressString = address;
    		aBalance = balance;
    		majorString = major;
    	}
     
     
    	public String getName() {
     
    		return nameString;
    	}
     
    	public String getAddress(){
    		return addressString;
    	}
     
    	public String getMajor() {
    		return majorString;
    	}
     
    	public double getBalance() {
    		return aBalance;
    	}
     
    	public String toString() {
    		return "Name: " + nameString + "address" + addressString + "Balance" + aBalance + " major " + majorString;
    	}
    }

  4. #4
    Member
    Join Date
    Mar 2012
    Location
    United States
    Posts
    118
    My Mood
    Inspired
    Thanks
    1
    Thanked 33 Times in 31 Posts

    Default Re: Continuing to create an object with the press of a button.

    I suggest looking at the following, particularly the methods section.
    JTextArea (Java Platform SE 7 )

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Continuing to create an object with the press of a button.

    Okay i've made progress on my question but the final piece I need help with. I need to create an array in the Student class with a static variable that keeps track of how many times the Student form is filled out. In other words limit it to a certain number, say 2 entries or something like that. Any examples on how I implement this?


    class Student extends GUI{
    	private static Student[] student =
    			new Student[2];
    	private static int counter;
     
    public String nameString, addressString, majorString, balanceString;
    public double aBalance;
     
    	public Student(String name, String address, String major, double balance) {
     
    		this.nameString= name;
    		this.addressString = address;
    		this.aBalance = balance ;
    		this.majorString = major;
    }
    	public String getName() {
     
    		return nameString;
    	}
     
    	public String getAddress(){
    		return addressString;
    	}
     
    	public String getMajor() {
    		return majorString;
    	}
     
    	public double getBalance() {
     
    		return  aBalance;
    	}
     
    	public String toString() {
     
    		return "Name: " + nameString + "\n" + "Address: " + addressString + 
    				"\n" + "Major: " + majorString + "\n" + "Balance: " + aBalance + "\n";
    	}
    }
    _________________________________________________________________________________________________________
    and from my GUI class
    private class ButtonListener implements ActionListener{
    		public void actionPerformed(ActionEvent event) {
     
    			Object srcObj = event.getSource();
    			if (srcObj == submitButton){
     
     
    				Student student = new Student(nameField.getText(), addressField.getText(), majorField.getText(), Double.parseDouble(balanceField.getText()));
    				displayInfo.append(lineSeparator + student);
     
     
    				nameField.setText("");
    				balanceField.setText("");
    				majorField.setText("");
    				addressField.setText("");
    				}
    			}
    		}

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,760
    My Mood
    Mellow
    Thanks
    233
    Thanked 712 Times in 698 Posts

    Default Re: Continuing to create an object with the press of a button.

    What you describe doesn't make sense.

    A static variable to track the number of times a form has been filled out would typically be a static int, something like "formsCompleteCounter," that is incremented each time a form is completed by any student object. However, why one would then use that to limit the forms completed to a certain number is confusing.

    Perhaps you can explain better the purpose of the static variable and/or what you've been tasked to do.

  7. #7
    Junior Member
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Continuing to create an object with the press of a button.

    Sure, here is an exert from the assignment: "Note from the use case that our goal is to store the Student objects. Therefore, your Student class should contain an array responsible for storing created Student objects. You’ll also want to create a counter variable responsible for keeping track of the amount of Student objects created. For now, your array can simply be a static size, say 50.

    Since the Student array and associated counter variable will be the same for any Student object created, they should be defined as static variables. This way, the array/counter will be consistent across all created Student objects."

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,760
    My Mood
    Mellow
    Thanks
    233
    Thanked 712 Times in 698 Posts

    Default Re: Continuing to create an object with the press of a button.

    Okay. The counter variable is a static int as I described (also known as a "class variable"), but it isn't meant to limit the number of Student objects created. The number 50 was given as the maximum number of Student objects expected at this point, so 50 can be used as the size of the array in which those objects will be stored. I believe the term "static size" was meant to indicate the number of student objects would be no more than 50 and wouldn't expand beyond that for this phase of the program.

    In summary, there will be two class variables:

    static int counter;
    static Student[] students = new Student[50];

Similar Threads

  1. Create a JText field upon button click
    By jo15765 in forum AWT / Java Swing
    Replies: 3
    Last Post: May 28th, 2012, 07:06 PM
  2. Replies: 5
    Last Post: August 11th, 2011, 12:39 PM
  3. how two create two 8*8 button border
    By mr.p4r4d0x in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 26th, 2011, 10:25 AM
  4. How to press button to open another window
    By vkokaram in forum AWT / Java Swing
    Replies: 1
    Last Post: July 18th, 2010, 03:51 PM
  5. press a button, get textfield content
    By chopficaro in forum AWT / Java Swing
    Replies: 1
    Last Post: May 2nd, 2010, 12:28 PM