Results 1 to 6 of 6
  1. #1
    kbyrne is offline Member
    Join Date
    Dec 2007
    Posts
    15
    Rep Power
    0

    Default Accessing one class from another class through swing

    I've got these 2 classes, one of them holds the gui and is called MainGui.java. and the other "Main.java" instanciates this class among other functionalities. I want to add to call a function in MainGui from the Main class and add to a Jpanel in MainGui. Any tips or advice on how best to do this?

    Thanks

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    class Main {
        // Instantiate an instance of MainGui and save a
        // reference to it in a member variable ("gui").
        // Use this reference/variable to call methods
        // and access fields in the MainGui class.
        MainGui gui = new MainGui();
    
        public Main() {
            // Use member variable to call method in MainGui.
            gui.addToPanel(new JPanel());
        }
    }
    
    class MainGui extends JFrame {
        JPanel panel = new JPanel(new GridLayout(0,4));
    
        public MainGui() {
            getContentPane().add(panel);
            setDefaultCloseOperation
            setSize
            setLocation
            setVisible
        }
    
        public void addToPanel(JComponent c) {
            panel.add(c);
            panel.revalidate();
        }
    }

  3. #3
    kbyrne is offline Member
    Join Date
    Dec 2007
    Posts
    15
    Rep Power
    0

    Default

    Thanks, that's great

  4. #4
    Join Date
    Jan 2008
    Posts
    4
    Rep Power
    0

    Default

    Hi to java forums

  5. #5
    kbyrne is offline Member
    Join Date
    Dec 2007
    Posts
    15
    Rep Power
    0

    Default

    Hi there, happy new year

  6. #6
    Join Date
    Jan 2008
    Posts
    4
    Rep Power
    0

    Thumbs up Hiii

    Hi we can access once class from another class using java swings bcoz java swing is completely written in pure java .so we can do it .

Similar Threads

  1. Accessing list out another class
    By Preethi in forum New To Java
    Replies: 23
    Last Post: 10-26-2008, 02:54 PM
  2. Java Swing class capturing output to the console
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-12-2008, 11:24 AM
  3. Replies: 0
    Last Post: 02-17-2008, 09:03 AM
  4. Replies: 0
    Last Post: 02-17-2008, 09:01 AM
  5. Inner class accessing outer class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-17-2008, 08:59 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
  •