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: Need help calling methods in a GUI

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

    Default Need help calling methods in a GUI

    First: here are the instructions I have been given.

    "As you can see, this GUI shall have three labels, three text fields, a text area, and a single button. Upon entering text, and pressing the button, your GUI should create an instance of your user-defined Dog class. Then, when printing back to the GUI, use methods defined within the Dog class to do this in an easier way than we had done before (I’d suggest using the toString() method)."

    My problem is getting the toString method to work, I can get text from the textFields and output it to a string but that's just not what I am being asked to do. I've tried various ways to pull a String from the Dog class and output it in GUI(Lab3) class, but just don't know what I need to do. Any help is greatly appreciated.

    public class Lab3 extends JFrame {

    public JLabel nameLabel, breedLabel, ageLabel;
    public JTextField nameField, breedField, ageField;
    public JButton dogButton = new JButton("Generate Dog");
    public JTextArea textArea;
    public JPanel panel1, panel2, panel3;
    public String dogName, dogBreed, dogAge;


    public Lab3() {
    nameLabel = new JLabel("Name");
    breedLabel = new JLabel("Breed");
    ageLabel = new JLabel("Age");

    nameField = new JTextField(20);
    breedField = new JTextField(20);
    ageField = new JTextField(20);

    panel1 = new JPanel();
    panel1.add(nameLabel);
    panel1.add(breedLabel);
    panel1.add(ageLabel);
    panel1.add(nameField);
    panel1.add(breedField);
    panel1.add(ageField);

    panel1.setLayout(new GridLayout(2,3));
    add(panel1, BorderLayout.NORTH);

    panel2 = new JPanel();
    panel2.add(dogButton);
    DogListener listener1 = new DogListener();
    dogButton.addActionListener(listener1);
    add(panel2, BorderLayout.CENTER);

    textArea = new JTextArea(20,20);
    textArea.setEditable(false);
    panel3 = new JPanel();
    panel3.add(textArea);
    add(panel3, BorderLayout.SOUTH);




    }

    class DogListener implements ActionListener {

    public void actionPerformed(ActionEvent e){

    dogName = nameField.getText();
    dogBreed = breedField.getText();
    dogAge = ageField.getText();
    textArea.setText("\n" + "Name: " + dogName + "\n" + "Breed: " + dogBreed + "\n" + "Age: " + dogAge );
    }


    }
    public static void main(String[] args){
    Lab3 frame = new Lab3();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    frame.setTitle("Dog Generator");
    frame.setVisible(true);
    frame.setSize(600,450);
    }
    }
    __________________________________________________ ________________________________
    Second the Dog class w/methods


    public class Dog extends Lab3 {


    public Dog(){

    }

    public Dog(String dogName, String dogBreed, String dogAge) {
    this.dogName = dogName;;
    this.dogBreed= dogBreed;
    this.dogAge = dogAge;

    }


    public String getName() {

    return dogName;
    }

    public String getBreed(){

    return dogBreed;
    }

    public String getAge() {

    return dogAge;
    }

    public String toString() {

    return "Name: " + dogName + "\n" + "Breed: " + dogBreed + "/n" + "Age: " + dogAge;
    }


    }


  2. #2
    Junior Member
    Join Date
    Feb 2014
    Location
    Philippines
    Posts
    12
    My Mood
    Tired
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Need help calling methods in a GUI

    hi,

    correct me if i'm wrong.
    your problem is using the 'Dog' class to return the user input to the text area?

    well can create a variable of that class, kinda like this

    public class myMain {
     
    	public static void main(String[] args){
     
    		myPOJO pojo = new myPOJO("the String",2); // can use my defined constructor to initialize my pojo
    		//get values with the getter methods
    		System.out.println("MyString "+ pojo.getMyString());
     
    		//set new values using setter methods
    		pojo.setMyString("my new String");
    		System.out.println("MyString "+ pojo.getMyString());
     
    		//invoke other method inside my pojo
     
    		System.out.println(pojo.toString());
    	}
    }
     
    class myPOJO {
     
    	public myPOJO(){
     
    	}
     
    	public myPOJO(String myString, int myInt) {
     
    		this.myInt = myInt;
    		this.myString = myString;
    	}
     
     
    	String myString;
    	int myInt;
     
    	public String getMyString() {
    		return myString;
    	}
    	public void setMyString(String myString) {
    		this.myString = myString;
    	}
    	public int getMyInt() {
    		return myInt;
    	}
    	public void setMyInt(int myInt) {
    		this.myInt = myInt;
    	}
     
    	public String toString(){
    		return "My String : "+myString+" My Int : "+myInt;
    	}
     
    }

    regards

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need help calling methods in a GUI

    @phunnydoode: Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    @destitute.developer: Please follow Java naming conventions and begin class names with capital letters.

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

    Default Re: Need help calling methods in a GUI

    Quote Originally Posted by destitute.developer View Post
    hi,

    correct me if i'm wrong.
    your problem is using the 'Dog' class to return the user input to the text area?

    well can create a variable of that class, kinda like this

    public class myMain {
     
    	public static void main(String[] args){
     
    		myPOJO pojo = new myPOJO("the String",2); // can use my defined constructor to initialize my pojo
    		//get values with the getter methods
    		System.out.println("MyString "+ pojo.getMyString());
     
    		//set new values using setter methods
    		pojo.setMyString("my new String");
    		System.out.println("MyString "+ pojo.getMyString());
     
    		//invoke other method inside my pojo
     
    		System.out.println(pojo.toString());
    	}
    }
     
    class myPOJO {
     
    	public myPOJO(){
     
    	}
     
    	public myPOJO(String myString, int myInt) {
     
    		this.myInt = myInt;
    		this.myString = myString;
    	}
     
     
    	String myString;
    	int myInt;
     
    	public String getMyString() {
    		return myString;
    	}
    	public void setMyString(String myString) {
    		this.myString = myString;
    	}
    	public int getMyInt() {
    		return myInt;
    	}
    	public void setMyInt(int myInt) {
    		this.myInt = myInt;
    	}
     
    	public String toString(){
    		return "My String : "+myString+" My Int : "+myInt;
    	}
     
    }

    regards
    Well what I'm trying to do is get the Dog class to gather the input from the textfield's in the GUI class and then output it to the textArea in the GUI class using the toString method. I attempted to create a variable of the class Dog like suggested but just don't know how to have the TextArea display it.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need help calling methods in a GUI

    If dog is an instance of Dog and textArea the JTextArea in which you want to invoke the Dog.toString() method, then a way to do that is:

    textArea.setText( "" + dog );

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

    Default Re: Need help calling methods in a GUI

    So now I can get the textArea to print using the toString(), but I just can't seem to figure out how to grab the text from the textFields and display that as well. The first part is just what i've changed to the Lab3(GUI) class.
    class DogListener implements ActionListener {
     
    	public void actionPerformed(ActionEvent e){
    		Dog dogClass = new Dog();
    		dogName = nameField.getText();
    		dogBreed = breedField.getText();
    		dogAge = ageField.getText();
    		textArea.setText("" + dogClass);
    	}
     
     
    }
    _________________________________________________________________________________
    class Dog extends Lab3{
     
     
    	public Dog(){
     
    	}
     
    	public Dog(String dogName, String dogBreed, String dogAge) {
    	this.dogName = dogName;
    	this.dogBreed= dogBreed;
    	this.dogAge = dogAge;
     
    	}
     
    	String dogName, dogBreed, dogAge;
     
    	public String getName() {
     
    		return dogName ;
    	}
     
    	public void setName(String dogName) {
     
    		this.dogName = dogName;
    	}
     
    	public String getBreed(){
     
    		return dogBreed;
    	}
    	public void setBreed(String dogBreed){
    		this.dogBreed = dogBreed;
    	}
     
    	public String getAge() {
     
    		return dogAge;
    	}
    	public void setAge(String dogAge){
    		this.dogAge = dogAge;
    	}
     
    	public String toString() {
     
     
    		return  "Name: " + dogName  + "\n" + "Breed: " + dogBreed + "\n" + "Age: " + dogAge;
    	}
     
     
    	}

    Any thoughts?

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need help calling methods in a GUI

    Okay, cart before the horse = cart doesn't go well (or goes backwards).

    You might change your actionPerformed() method like this:
    // create a new dog instance with the contents of the text fields:
    Dog dogClass = ( new Dog( nameField.getText(), breedField.getText(),
        ageField.getText() );
    textArea.setText("" + dogClass);
    A complication with this approach (and your original) is that the dogClass instance is local to actionPerformed() and can't be used anywhere else, but maybe that's okay.

    A more complicated approach to do the same thing would be to use the Dog's setters AFTER creating the instance dogClass:

    dogClass.setName( nameField.getText() ); // sets the dog's name

    and so on for breed and age.

    You decide.

  8. The Following User Says Thank You to GregBrannon For This Useful Post:

    phunnydoode (February 12th, 2014)

  9. #8
    Junior Member
    Join Date
    Feb 2014
    Location
    Philippines
    Posts
    12
    My Mood
    Tired
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Need help calling methods in a GUI

    Quote Originally Posted by phunnydoode View Post
    Well what I'm trying to do is get the Dog class to gather the input from the textfield's in the GUI class and then output it to the textArea in the GUI class using the toString method. I attempted to create a variable of the class Dog like suggested but just don't know how to have the TextArea display it.
    yes, basically same principle applies there.

    		myPOJO pojo = new myPOJO("the String",2); // can use my defined constructor to initialize my pojo
    		//get values with the getter methods
    		System.out.println("MyString "+ pojo.getMyString());
     
    		//set new values using setter methods
    		pojo.setMyString("my new String");
    		System.out.println("MyString "+ pojo.getMyString());
     
    		//invoke other method inside my pojo
     
    		System.out.println(pojo.toString());

    just modify your code so that once you press you button you'll gather the user input and set the values in the pojo then invoke your 'toSring' method to assign the concatenated values to the text area.

    or did i misinterpret the question? sorry if i did

Similar Threads

  1. Need help with calling methods in Java.
    By searock in forum Java Theory & Questions
    Replies: 2
    Last Post: February 2nd, 2013, 02:22 PM
  2. Calling upon methods
    By jonathanfox in forum Java Theory & Questions
    Replies: 4
    Last Post: August 3rd, 2012, 11:58 AM
  3. Calling methods in other files
    By zdavos in forum Object Oriented Programming
    Replies: 4
    Last Post: March 2nd, 2012, 07:57 AM
  4. [SOLVED] Calling methods from other classes
    By knightknight in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 7th, 2011, 06:16 PM
  5. Calling for methods
    By soccer_kid_6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 1st, 2010, 12:13 AM