Results 1 to 11 of 11
  1. #1
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default Basic GUI question (showing info from another class)

    Hi ive been intensly studying java now for a few weeks and ive moved steadily onto GUI's and my question to the forum is, how do i show information about an objects variable in a label on a GUI form?

    ill write some quick code here, please excuse any errors, i hope this will clarify my question


    ok so ive created a main class called main
    and ive created another class called person

    in the main class
    =================================

    person john = new person(); //created a new object called person

    john.setHeight(175);

    ===================================

    ==

    the person class
    ======================================

    public void person{

    int height = -1;

    void setHeight(int heightPassed){

    height = heightPassed;

    }
    }

    ================================


    ok so from above you can see i set the object viarable john.height to 175.
    Now assuming a created a simple JFrame (GUI) form, how would i then show john.height in a label?

    reason why i ask is because the GUI doesnt reconise the object john so whats the way round this?

    thank you

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Having GUI's work with other classes is little different from having non-GUI classes work with other classes. Let's see your attempt and then perhaps we can better help you with solving your problem.

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Make a method called getHeight() in the person class. Reference person in the JFrame and then do something like:
    Java Code:
    JLabel label = new JLabel(john.getHeight());
    Several ways of achieving this. Assuming your GUI class extends JFrame, you could make a public method in that class that takes height. If so, you can do this in the main:
    Java Code:
    public static void main (String[] args) {
         person john = new person(); //created a new object called person
         john.setHeight(175);
         
         CustomFrame frame = new CustomFrame(); //Your JFrame class is called
                                                                   //CustomFrame
         frame.setHeight(john.getHeight()); //Assuming your frame class has this method
         frame.display(); //This method probably makes the frame visible, display it in the 
                               //center, etc.
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  4. #4
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Having GUI's work with other classes is little different from having non-GUI classes work with other classes. Let's see your attempt and then perhaps we can better help you with solving your problem.
    ok im thinking that i could created a method inside the GUI class and then pass the peter obect to that method.
    and then in the method basicalled make a JLabel = peter.height?
    please excuse the pseudo code.

  5. #5
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    Make a method called getHeight() in the person class. Reference person in the JFrame and then do something like:
    Java Code:
    JLabel label = new JLabel(john.getHeight());
    Hey i tried this code and it didnt reconise the object, then i re-read what you wrote and you've mentioned ive got the "reference" the class person. How do i reference the class?

  6. #6
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Java Code:
    person john = new person();
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  7. #7
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    oh i see what you did, you created the object person in the GUI class.

    In my example i created the object in the main class and wanted information from that object to show in the GUI class.

    the reason why i want to know this is because in the future i may want to create a couple of GUI forms and i want them both to show information from one object which is held in a different class.

    would one way going about it be to create an array list in the GUI class to hold objects, (create a method in the GUI class to get the object from the main class and put it in the arrayList which is in the GUI class) and then bingo, i can now play around with the object from the GUI class by refering to it from its position in the arraylist.

    any thoughts?
    Last edited by alacn; 06-17-2010 at 04:02 AM.

  8. #8
    gafa's Avatar
    gafa is offline Senior Member
    Join Date
    Jun 2010
    Posts
    107
    Rep Power
    0

    Arrow

    WTFIT

    Java Code:
    JLabe label=new JLabel();
    ...
    label.setText("blabla");
    easy
    + 1 my REP
    Good Luck :)

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Then pass the non-GUI information in the main class to the GUI class via a parameter in a setter method or a constructor.

  10. #10
    alacn's Avatar
    alacn is offline Senior Member
    Join Date
    Jun 2010
    Location
    UK. near London
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Then pass the non-GUI information in the main class to the GUI class via a parameter in a setter method or a constructor.

    oh i see, so it would be something like

    public class Main {

    public static void main(String[] args) {

    someCLass object = someClass();
    GUI form = new GUI(object);

    }
    }





    public class GUI extends JFrame{

    public GUI(someClass objectPassed){

    objectPassed.dosomething;

    }

    }


    so in the brief example above, the method "dosomething" should work?

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by alacn View Post
    oh i see, so it would be something like
    so in the brief example above, the method "dosomething" should work?
    The quickest way to find out: try it and see! :)

Similar Threads

  1. How it is possible view info about class?
    By artemff in forum New To Java
    Replies: 3
    Last Post: 12-31-2009, 12:17 AM
  2. Basic Class Interaction Question
    By McChill in forum New To Java
    Replies: 1
    Last Post: 05-09-2009, 11:41 PM
  3. Basic question about EJB
    By javaplus in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 07-15-2008, 05:44 PM
  4. Getting Class info
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-06-2007, 02:46 PM
  5. Very basic question
    By gvi in forum New To Java
    Replies: 2
    Last Post: 10-30-2007, 06:30 PM

Posting Permissions

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