Results 1 to 3 of 3
  1. #1
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Controlling GUI from another Class?

    Hello,

    I'm making a relatively large program for which I'd like to be able to control my gui from other classes.

    I've created a simple example to explain my dilemma - All this is supposed to do is change the text of the label to "It Works!" when the button is pressed.

    Here's the important part of the main class:

    Java Code:
    public class main extends javax.swing.JFrame {
        
        secondary secobj = new secondary();
        
        //...Some other code
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        secobj.buttonpress();
        }
        //...
        public javax.swing.JButton jButton1;
        //...
    }
    And here's the outside class that's meant to be controlling the JLabel:

    Java Code:
    public class secondary {
    main mobj = new main();
    
    public void buttonpress(){
    mobj.jLabel1.setText("It works!");
    System.out.println("This function has been successfully called.");
    }
    
    }
    When I press the button, the "System.out.println" part runs, but for whatever reason the label's text doesn't change. This is consistent with the problems I had with trying to do this on my original program. Any suggestions?

    Thanks,

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

    Default Re: Controlling GUI from another Class?

    My guess, and it's only a guess since we're viewing limited code, is that you are changing the text of a JLabel held by a main object, but it's just not the main object that is visualized. In other words your secondary class creates its own main object, held by the mobj variable, while there's likely another main object that was created somewhere else and is the one that's displayed.

    If I'm right, then to correct this, the secondary class is going to need a reference to the original displayed main object. Try not creating a new main object in this class, but rather give it a getMain(main main) method where you set the main object held by this class.

    Also, please read up on and use the standard Java naming conventions including capitalizing the first letter of your class names. These conventions *are* important, especially when you ask strangers to try to understand your code and help you.

  3. #3
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Re: Controlling GUI from another Class?

    Right you are! Here's the final code:

    Main:
    Java Code:
        private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        secobj.buttonpress(this);
        }
    Secondary:
    Java Code:
    public void buttonpress(main main){
    main.jLabel1.setText("It works!");
    System.out.println("This function has been successfully called.");
    }
    Thanks!

Similar Threads

  1. Controlling User Inputs
    By time-space in forum New To Java
    Replies: 5
    Last Post: 05-30-2011, 02:50 PM
  2. Cursor controlling?
    By UJJAL DHAR in forum Advanced Java
    Replies: 2
    Last Post: 01-31-2011, 05:19 AM
  3. Controlling cmd from java...HELP!!
    By eponcedeleon in forum Advanced Java
    Replies: 12
    Last Post: 02-23-2010, 06:33 PM
  4. Controlling loop using next button
    By cassysumandak in forum New To Java
    Replies: 1
    Last Post: 03-21-2009, 02:56 AM
  5. controlling GC
    By ravian in forum Eclipse
    Replies: 2
    Last Post: 01-03-2008, 08:13 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
  •