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 2 of 2

Thread: Is there a wrapper for non-static field to be able accesed by static method?

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Is there a wrapper for non-static field to be able accesed by static method?

    Hi all,
    I have a little problem. I create a GUI Application using Netbeans IDE 7.1. I want to pass a string from static method to the JTextArea. Here is the problem:
    1. the JTextaArea variable name is declared non-static by IDE (I can't even modify it to static type).
    2. the method to which I want to pass the JTextArea is static (surely I can't modify it since its a third party library).

    Here is the snippet declaration by Netbeans GUI:

    // Variables declaration - do not modify
    .......
    private javax.swing.JLabel machinePortLabel;
    private javax.swing.JTextField machinePortTextField;
    private javax.swing.JTextArea machineLoggingTextField;
    // End of variables declaration

    Here is the the static method:

    public class MachineAgent extends Agent{
    private boolean finished = false;
    final String newline = "\n";

    @Override
    public void setup() {
    addBehaviour(new SimpleBehaviour() {
    private static final long serialVersionUID = 1L;

    @Override
    public void action() {
    MachineFrame.getMachineLoggingTextField().getText( ); //Compiler complain "non-static method //getMachineLoggingTextField() can not be referenced from a static context.
    }

    @Override
    public boolean done() {
    return finished;
    }
    });
    }
    }


    Can anyone offer a solution to the problem? A useful hint or clue will be appreciated. Thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Is there a wrapper for non-static field to be able accesed by static method?

    want to pass a string from static method to the JTextArea.
    Not sure what the problem is. You can always call a static method to get a String that can be put into a text field.
    aTextField.setText(ClassName.staticMethod()); // set value of textfield using value from a static method.

    Or are you doing it the other way? Trying to get to a non static field in some class from a static method? Then you need a reference to a class object:
    staticTextField.setText(aRefToClassObject.nonStati cMethod());
    Last edited by Norm; April 3rd, 2012 at 06:35 AM.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. What's the difference between a static and non-static method?
    By wholegrain in forum Java Theory & Questions
    Replies: 4
    Last Post: February 23rd, 2012, 01:06 AM
  2. update static field and call method within the class?
    By GeneralPihota in forum Object Oriented Programming
    Replies: 7
    Last Post: February 6th, 2012, 09:20 PM
  3. Replies: 9
    Last Post: November 5th, 2011, 10:22 AM
  4. non-static method cannot be referenced from a static context
    By Kaltonse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 21st, 2010, 07:51 PM
  5. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM