Results 1 to 5 of 5
  1. #1
    ejs7597 is offline Member
    Join Date
    Feb 2009
    Posts
    13
    Rep Power
    0

    Default How to get methods to see variables in other methods

    I am working on my class assignment and I am still having problems understanding how to get variable data to flow from one method to another method. For a sample I am just trying to make a press of a button change a text box to be equal to the data stored in an array[0]. I do not understand how to get the method with the button to be able to see the data in the method with the array. What do I need to do to be able to use the data in the array in the button method? I keep getting an error " Can't find symbol: variable itemNum"

    //Array
    public void storedata() {
    String[] itemNum = {"1","2"};
    }

    //VCR FIRST BUTTON
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    String temp = itemNum[0];
    jTextField1.setText(temp);
    }

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    you may modify your method storedata to return String[]

    or you can declare a global variable String[] xxx

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

    Default

    The class that holds the array could have a public method that gets array contents for a specific index, something like:
    Java Code:
    public String getItem(int index)
    {
      // may wish to throw an exception 
      // if the index is out of bounds
      return itemNum[index]
    }
    Then the GUI class that holds the button (or more importantly, the class that holds the button's action listener) needs to have a variable that refers to the relevant array-holding object.

    edit: or just have a getter for the entire array as suggested by mtyoung above.

  4. #4
    ejs7597 is offline Member
    Join Date
    Feb 2009
    Posts
    13
    Rep Power
    0

    Default Still not quite understanding.

    Thanks for the quick replys, but I just can't grasp this what seems to be simple task. Instead of setting the textbox to a variable value when the button is pressed I tried a string value and the program worked. Everything I do to try and get the array value or any variable value to update the textbox I run into variable not found errors. Any additional help would be greatly appreciated as I am very new to Java and if I can get past this I will be able to complete my program with ease. Here is a better example of how my code is actually layed out.



    public class MYGUI extends javax.swing.JFrame {
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

    String temp = "5";
    jTextField1.setText(temp);
    }

    }
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

    String temp = "5";
    jTextField1.setText(temp);
    }


    class storedata {
    String[] itemNum = {"1","2"};
    }

    }

  5. #5
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. accessing instance variables from static methods
    By ravian in forum New To Java
    Replies: 7
    Last Post: 03-01-2009, 11:09 PM
  3. JSP methods example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-30-2008, 11:00 AM
  4. variables-methods
    By Warren in forum New To Java
    Replies: 1
    Last Post: 11-28-2007, 05:14 PM
  5. significance of static variables and methods
    By imran_khan in forum New To Java
    Replies: 4
    Last Post: 08-02-2007, 10:52 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
  •