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

Thread: Search array in separate class ???

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Search array in separate class ???

    So basically I have an array saved in class, I then have a separate class which brings up a form. I wish to use this form to search within the array, i later want to work with the array to edit it.

    class to hold array
        public class EventData {    
     
                                 public static void main(String[] args) {
     
                                     String[] Event = new String[2];
                                     Event[0] = new String("01-01-2013", "closed");
                                     Event[1] = new String("02-01-2013", "closed");;
                                                                      }
                                        }

    class to hold form
    *
      public class Picker {
            public static void main(String[] args ) {
                 final JTextField t = new JTextField(0);  
                 final JTextField t1 = new JTextField(40);
                 JButton b = new JButton("Open Calendar");
                 JLabel l1 = new JLabel("Event");
                 JPanel p = new JPanel();
                 p.add(b);
                 p.add(l1);
                 p.add(t1);
                 final JFrame f = new JFrame();
                 f.getContentPane().add(p);
                 f.pack();
                 f.setVisible(true);
     
                 b.addActionListener(new ActionListener() {
                              public void actionPerformed(ActionEvent ae) {
                                     //*If the array is set here the code works fine, 
                                    // outside of the actionListener and it doesn't work*   
                                    /*
                                    String[] Event;
                                    Event = new String[2];
                                    Event[0] = "";
                                    Event[1] = "01-01-2013 -- Closed";
                                     /*
                                    t.setText(new DatePicker(f).setPickedDate());
                                    String input = t.getText();
     
                                     for(String ent: Event) {
                                          if(ent.startsWith(input)){
                                                 t1.setText("" + ent);
                                                                           }
                                           else {
                                                  t1.setText ("No Event");
                                                  }
                                                                           }
     
     
                                                                   }
                                                                                         }
                                                                            );
     
                                   }
    }
    any suggestions, I have tried placing the array in a method and calling it using EventData.Ent however this didn't work either


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

    Default Re: Search array in separate class ???

    this didn't work
    Please explain what happened.
    There aren't any class level arrays in the posted code. To access an array in another class, the array must be defined at the class level.


    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    Also properly format the code. There should NOT be anything on a line following an ending bracket: }
    }s should be inline with the start of the line with its pairing {
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Search array in separate class ???

    What do you mean by "the array must be defined at the class level" I'm a beginner in Java.

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

    Default Re: Search array in separate class ???

    There are several places to define variables: inside the class and inside a method.
    Variables defined in a method only exist while the method is being executed. They disappear when the method stops executing. Variables defined in the class (not in a method) will continue to exist as long as the instance of the class exists.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: December 26th, 2012, 07:01 PM
  2. First time poster looking for help accessing a variable from a separate class.
    By royalcrown28 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 19th, 2012, 11:41 PM
  3. This Code worked on a separate class but does not work on another class
    By titowinky in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 25th, 2012, 08:48 AM
  4. updating SWT Slider from separate java class
    By ppmckee in forum AWT / Java Swing
    Replies: 0
    Last Post: April 14th, 2011, 03:23 PM
  5. how to separate this code in another class
    By Jhovarie in forum AWT / Java Swing
    Replies: 2
    Last Post: February 28th, 2011, 06:22 PM

Tags for this Thread