Results 1 to 3 of 3
  1. #1
    mafyou is offline Member
    Join Date
    Feb 2012
    Posts
    10
    Rep Power
    0

    Default Accessing GUI components from different class

    Hi all,

    Was just wondering if there was a way of accessing, say a JPanel (namely getting the width and height) from a different class? I'm currently working on a large project and I can't afford to having these passing through every class...

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: Accessing GUI components from different class

    If you have a reference to the JPanel or can obtain one, you can query its width and height. If not, then not. It's as simple as that.

    I can't afford to having these passing through every class
    What?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default Re: Accessing GUI components from different class

    Let's say your panel class is called MyPanelClass, and you need to access its width and height in MyNormalClass.

    You can simply have the constructor of MyNormalClass accept an instance of a JPanel as an argument, and then assign that instance to a variable

    For example:

    Java Code:
    public class MyPanelClass extends JPanel{
    
       public MyPanelClass(){
    
      }
    }

    Java Code:
    public class MyNormalClass{
     private JPanel panelNeeded;
    
     public MyNormalClass(JPanel panelNeeded){
     this.panelNeeded = panelNeeded;
     }
    }
    At this point you can, from within your MyNormalClass, obtain the width or height from panelNeeded.
    Last edited by sehudson; 04-12-2012 at 05:04 PM.

Similar Threads

  1. Replies: 5
    Last Post: 12-18-2011, 04:06 AM
  2. accessing a singleton class inside another class
    By katturv in forum New To Java
    Replies: 1
    Last Post: 12-08-2010, 06:23 PM
  3. [SOLVED] accessing JPanel.add components
    By VeasMKII in forum New To Java
    Replies: 5
    Last Post: 06-19-2009, 01:29 AM
  4. Replies: 6
    Last Post: 03-31-2009, 04:27 AM
  5. Replies: 8
    Last Post: 03-21-2009, 09:34 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
  •