Calling an array from another class

Ok I have this little program that I created to display values stored in an array. Well, I want my array to automatically be populated with the same values that are in another array that resides in a different class. Here is my code for the class that I want the values to be displayed:
import javax.swing.*;
import java.awt.*;
public class GUIRead extends JFrame
     double[] interest = new double[3];
     MemicCSVReader[] interestObjectArray = new MemicCSVReader[3];
     interestObjectArray[0] = new MemicCSVReader();
     interestObjectArray[1] = new MemicCSVReader();
     interestObjectArray[2] = new MemicCSVReader():
     for(int x = 0; x < 3; x++)
     JPanel display = new JPanel();
     JTextField interestText = new JTextField(10);
     JPanel display2 = new JPanel();
     JTextField interestText2 = new JTextField(10);
     JPanel display3 = new JPanel();
     JTextField interestText3 = new JTextField(10);
     public GUIRead()
          super("Test");
          setSize(160,200);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setVisible(true);
          GridLayout config = new GridLayout(3,1);
          FlowLayout config2 = new FlowLayout(FlowLayout.LEFT,10,10);
          Container pane = getContentPane();
          pane.setLayout(config2);
          display.setLayout(config2);
          display.add(interestText);
          interestText.setText(interest[0]);
          interestText.setEditable(false);
          pane.add(display);
          display2.setLayout(config2);
          display2.add(interestText2);
          interestText2.setText(interest[1]);
          interestText2.setEditable(false);
          pane.add(display2);
          display3.setLayout(config2);
          display3.add(interestText3);
          interestText3.setText(interest[2]);
          interestText3.setEditable(false);
          pane.add(display3);
          setContentPane(pane);
     public static void main(String[] args)
          GUIRead run = new GUIRead();
Here is my code that I want the double[] interest array to pull it's data from:
public class MemicCSVReader
     double interestArray[];
     MemicCSVReader()
          String[] interestString = {"5.5","5.35","5.75"};
          for(int x = 0; x < 3; x++)
               interestArray[x] = Double.parseDouble(interestString[x]);
}

Ok, thanks for your help. Now I have another problem. I can't get the values to display. Here is my code:
This is the program that runs the application:
import javax.swing.*;
import java.awt.*;
public class GUIRead extends JFrame
     MemicCSVReader readCSV = new MemicCSVReader();
     double[] interestDouble = readCSV.getInterestArray();
     String[] interest;
     JPanel display = new JPanel();
     JTextField interestText = new JTextField(10);
     JPanel display2 = new JPanel();
     JTextField interestText2 = new JTextField(10);
     JPanel display3 = new JPanel();
     JTextField interestText3 = new JTextField(10);
     public GUIRead()
          super("Test");
          setSize(160,200);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setVisible(true);
          GridLayout config = new GridLayout(3,1);
          FlowLayout config2 = new FlowLayout(FlowLayout.LEFT,10,10);
          Container pane = getContentPane();
          pane.setLayout(config2);
          display.setLayout(config2);
          display.add(interestText);
          interestText.setEditable(false);
          pane.add(display);
          display2.setLayout(config2);
          display2.add(interestText2);
          interestText2.setEditable(false);
          pane.add(display2);
          display3.setLayout(config2);
          display3.add(interestText3);
          interestText3.setEditable(false);
          pane.add(display3);
          setContentPane(pane);
     public void actionPerformed()
          for(int x = 0; x < 3; x++)
               interest[x] = Double.toString(interestDouble[x]);
          interestText.setText(interest[0]);
          interestText2.setText(interest[1]);
          interestText3.setText(interest[2]);
     public static void main(String[] args)
          GUIRead run = new GUIRead();
This is the file that it pulls the array from:
public class MemicCSVReader
     private double[] InterestArray = new double[3];
     MemicCSVReader()
          double[] InterestArray = {5.5,5.35,5.75};
     public double[] getInterestArray()
          return InterestArray;
}

Similar Messages

  • Calling a method from another class... that requires variables?

    I'm calling a method from another class to change the date (its a date object) in another class.
    I keep getting the error 'setDate(int,int,int) in Date cannot be applied to ()'
    My code is:
    public int changeDate()
         cycleDate.setDate();
    }I'm guessing I need to pass 3 parameters (day, month, year), but I'm not sure how! I have tried, but then I get errors such as ')' expected?
    Any ideas! :D

    f1d wrote:
    I'm calling a method from another class to change the date (its a date object) in another class.
    I keep getting the error 'setDate(int,int,int) in Date cannot be applied to ()'
    My code is:
    public int changeDate()
    cycleDate.setDate();
    }I'm guessing I need to pass 3 parameters (day, month, year), seems that way from the error you posted
    but I'm not sure how!
    setDate(16, 6, 2008);
    I have tried, but then I get errors such as ')' expected?
    Any ideas! :Dyou need to post your code if you're getting specific errors like that.
    but typically ')' expected means just that, you have too many or not enough parenthesis (or in the wrong place, etc.)
    i.e. syntax error

  • Help on Calling a method from another class

    how can i call a method from another class.
    Class A has 3 methods
    i just want to call only one of these 3 methods into my another class.
    How can I do that.

    When i am trying this
    A a=new A;
    Its calling all the methods from class A. I just want
    to call a specfic method.How can it be done?When i am trying this
    A a=new A();
    Its calling all the methods from class A. I just want to call a specfic method.How can it be done?

  • Calling repaint method from another class

    My question in a very simple form :
    how do I call repaint mathod from another class.
    e.g: Let's say class "A.java" is a JFrame .
    Class "B.java" is a JPanel which is added to the JFrame above.
    Class "C.java" is a JDialog containing some JButtons.
    How do I call the repaint method from the class "C.java".
    Thank you in advance!!

    My question in a very simple form :
    how do I call repaint mathod from another class.
    e.g: Let's say class "A.java" is a JFrame .
    Class "B.java" is a JPanel which is added to the JFrame above.
    Class "C.java" is a JDialog containing some JButtons.
    How do I call the repaint method from the class "C.java".
    Thank you in advance!!

  • How do you call a method from  another class without extending as a parent?

    How do you call a method from another class without extending it as a parent? Is this possible?

    Why don't you just create an instance of the class?
    Car c = new Car();
    c.drive("fast");The drive method is in the car class, but as long as the method is public, you can use it anywhere.
    Is that what you were asking or am I totally misunderstanding your question?
    Jen

  • How to call a method from another class

    I have a problem were i have to call a method from another class. What is the command line that i have to use. Thanks.

    Here's one I wipped up in 10 minutes... Cool!
    package forums;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import krc.utilz.io.Filez;
    import java.io.FileNotFoundException;
    class FileDisplayer extends JFrame
      private static final long serialVersionUID = 0L;
      FileDisplayer(String filename) {
        super(filename);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setSize(600, 800);
        JTextArea text = new JTextArea();
        try {
          text.setText(Filez.read(filename));
        } catch (FileNotFoundException e) {
          text.setText(e.toString());
        this.add(text);
      public static void main(String args[]) {
        final String filename = (args.length>0 ? args[0] : "C:/Java/home/src/forums/FileDisplayer.java");
        try {
          java.awt.EventQueue.invokeLater(
            new Runnable() {
              public void run() {
                new FileDisplayer(filename).setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
    Filez.read
       * reads the given file into one big string
       * @param String filename - the name of the file to read
       * @return the contents filename
      public static String read(String filename) throws FileNotFoundException {
        return Filez.read(new FileReader(filename));
       * Reads the contents of the given reader into one big string, and closes the reader.
       * @param java.io.Reader reader - a subclass of Reader to read from.
       * @return the whole contents of the given reader.
      public static String read(Reader in) {
        try {
          StringBuffer out = new StringBuffer();
          try {
            char[] bfr = new char[BFRSIZE];
            int n = 0;
            while( (n=in.read(bfr,0,BFRSIZE)) > 0 ) {
              out.append(bfr,0,n);
          } finally {
            if(in!=null)in.close();
          return out.toString();
        } catch (IOException e) {
          throw new RuntimeIOException(e.getMessage(), e);
      }Edited by: corlettk on Dec 16, 2007 1:01 PM - dang [code [/tags][                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Need to call a repaint from another class

    I need to call a repaint from another class, but since I can't make the paint() method a static one, it will not let me access the repaint method, any ideas? thanks guys
    Conor

    ok here's a very basic idea of whats goin on...
    <code>
    MainFrame class (in which the paint method resides)
    public void paint()
    // draw lines
    // opens an instance of the settings window
    void procSettings_actionPerformed(ActionEvent e)
    if(procRunning)
    procRunning = false;
    timer.stop();
    procPaused = true;
    procSettings s = new procSettings();
    s.setSize(370, 467);
    s.setLocationRelativeTo(null);
    s.setResizable(false);
    s.setVisible(true);
    ProcSettings class
    if(certain condition)
    // reset the lines of the graph
    // REPAINT <--- I want to know how to call this repaint, MainFrame.repaint() does not work
    thanks

  • Can't add list element when calling a method from another class

    I am trying to call a method in another class, which contains code listmodel.addElement("text"); to add an element into a list component made in that class.
    I've put in System.out.println("passed"); in the method just to make sure if the method was being called properly and it displays normally.
    I can change variables in the other class by calling the method with no problem. The only thing I can't do is get listmodel.addElement("text"); to add a new element in the list component by doing it this way.
    I've called that method within it's class and it added the element with no problem. Does Java have limitations about what kind of code it can run from other classes? And if that's the case I'd really like to know just why.

    There were no errors, just the element doesnt get added to the list by doing it this way
    class showpanel extends JPanel implements ActionListener, MouseMotionListener {
           framepanel fp = new framepanel();
           --omitted--
         public void actionPerformed(ActionEvent e){
                  if(e.getSource() == button1){
                       fp.addLayer();
    /*is in a different class file*/
    class framepanel extends JPanel implements ActionListener{
            --omitted--
         public void addLayer(){
              listmodel.addElement("Layer"+numLayer);
              numLayer++;
    }

  • Calling a function from another class - help!

    I realize that this is probably a basic thing for people who have been working with JavaFX for a while, but it is eluding me, and I have been working on it for over a week.
    I need to call a function that is in another class.  Here's the deal.  In EntryDisplayController.java, there are 2 possible passwords that can be accepted - one will give full access to the car, the second will limit functions on the car.  So when a password is entered and verified as to which one it is, I need to call a function in MainDisplayController.java to set a variable that will let the system know which password is being used - full or restricted.
    So in MainDisplayController.java I have this snippet to see
    public class MainDisplayController implements Initializable, ControlledScreen {
        ScreensController myController;
        public static char restrict;
         * Initializes the controller class.
        @Override
        public void initialize(URL url, ResourceBundle rb) {
            // TODO
        public void setRestriction(){
            restrict = 0;
            restrictLabel.setText("RESTRICTED");
        public void clearRestriction(){
            restrict = 1;
            restrictLabel.setText("");
    And in EntryScreenDisplay.java I have this snippet:
    public class EntryDisplayController implements Initializable, ControlledScreen {
         @FXML
        private Label passwordLabel ;
        static String password = new String();
        static String pwd = new String("");
         ScreensController myController;
         private MainDisplayController controller2;
         * Initializes the controller class.
        @Override
        public void initialize(URL url, ResourceBundle rb) {
            setPW(pwd);    // TODO
         @FXML
        private void goToMainDisplay(ActionEvent event){
            if(password.equals ("123456")){
              controller2.clearRestriction();
               myController.setScreen(ScreensFramework.MainDisplayID);
               pwd = "";
               password = "";
               setPW(pwd);
            else if(password.equals ("123457")){
               controller2.setRestriction();
                myController.setScreen(ScreensFramework.MainDisplayID);
               pwd = "";
               password = "";
               setPW(pwd);
            else{
                password = "";
                pwd = "";
                setPW(pwd);
    When I enter the restricted (or full) password, I get a long list of errors, ending with
    Caused by: java.lang.NullPointerException
      at velocesdisplay.EntryDisplayController.goToMainDisplay(EntryDisplayController.java:60)
    Line 60 is where "controller2.setRestriction(); is.
    As always, thanks for any help.
    Kind regards,
    David

    You never set the controller2 variable to anything, which is why you get a null pointer exception when you try to reference it.
    public static variables (and even singletons) are rarely a good idea as they introduce global state (sometimes they can be, but usually not).
    If a static recorder like this can only have a binary value, use a boolean, if it can have multiple values, use an enum.
    Some of the data sharing solutions in dependency injection - Passing Parameters JavaFX FXML - Stack Overflow might be more appropriate.
    I also created a small framework for roll based solutions in JavaFX which perhaps might be useful:
    http://stackoverflow.com/questions/19666982/is-there-a-way-to-implement-a-property-like-rendered-on-javafx
    https://gist.github.com/jewelsea/7229260
    It was just something I hacked together, so it's not a fully polished solution and you probably don't need something quite so complex.
    What you need is a model class shared between the components of your application.  Dependency injection frameworks such as afterburner.fx can help achieve this in a fairly simple to use way.
    If you don't want to go with a dependency injection framework, then you could use either a static singleton model class for your settings or pass the model class using the mechanism in defined in the passing parameters link I provided.  For a model class example, see the ClickCounter class from this example.   That example doesn't use FXML, but to put it together with the passing parameters solution, for your restriction model, you would have something like the following:
       private class Restricted {
         private final ReadOnlyBooleanWrapper restricted;
         public Restricted(boolen isRestricted) {
           restricted = new ReadOnlyBooleanWrapper(isRestricted);  
         public boolean isRestricted() {
           return restricted.get();
         public ReadOnlyBooleanProperty restrictedProperty() {
           return restricted.getReadOnlyProperty();
    Then in your EntryDisplayController you have:
    @FXML
    public void goToMainDisplay(ActionEvent event) {
         // determine the restriction model as per your original code...
         RestictionModel restrictionModel = new RestrictionModel(<appropriate value>);
      FXMLLoader loader = new FXMLLoader(
      getClass().getResource(
       "mainDisplay.fxml"
      Stage stage = new Stage(StageStyle.DECORATED);
      stage.setScene(
       new Scene(
         (Pane) loader.load()
      MainDisplayController controller =
        loader.<MainDisplayController>getController();
      controller.initRestrictionModel(restrictionModel);
      stage.show();
    class MainDisplayController() {
      @FXML private RestrictionModel restrictionModel;
      @FXML private RestrictLabel restrictLabel;
      public void initialize() {}
      // naming convention (if the restriction model should only be set once per controller, call the method init, otherwise call it set).
      public void initRestrictionModel(RestrictionModel restrictionModel) {
        this.restrictionModel = restrictionModel;
         // take some action based on the new restriction model, for example
        restrictLabel.textProperty.bind(
          restrictionModel.restrictedProperty().asString()
    If you have a centralized controller for your navigation and instantiation of your FXML (like in this small FXML navigation framework), then you can handle the setting of restrictions on new screens centrally within the framework at the point where it loads up FXML or navigates to a screen (it seems like you might already have something like this with your ScreensController class).
    If you do this kind of stuff a lot, then you would probably benefit from a move to a framework like afterburner.  If it is just a few times within your application, then perhaps something like the above outline will give you enough guidance to implement a custom solution.

  • Calling DB connection from another class

    Hi I am trying to create a method in another class to call another method that checks for userid in DB? What is the best way to set this up? I.e. calling method?
    //calling method below line
    private boolean setuserExist(String username) {
         else return false;
    public String getserExist() {
              return userexist;
    // Method being called below this line
    public boolean userExist(String username){
              boolean exist = false;
              System.out.println("in LoginDBAccess: username is " + username);
              try{
    Connection conn = dbc.getConnection();
    Statement statement = conn.createStatement();
    String query =      "SELECT User.UserID " +
                        "FROM User " +
                        "WHERE User.UserID = '" + username + "'";
    ResultSet resultSet = statement.executeQuery(query);
    System.out.println("1.1");
    ArrayList result = new ArrayList();
    while(resultSet.next()){
         //System.out.println("In While:");
    ArrayList item = new ArrayList();
    item.add(resultSet.getString("UserID"));
    result.add(item);
    System.out.println("LoginDBAccess Count: " + result.size());
    conn.close();
    if (result.size() == 1){
         exist = true;
              }catch(Exception e){
         e.printStackTrace();
              return exist;
         }

    Cross-post
    http://forum.java.sun.com/thread.jspa?threadID=603496

  • ABAP Objects : calling one method from another class

    Hi,
    Can you please tell me how to call method from one class or interfce to another class.The scenario is
    I have one class CL_WORKFLOW_TASK, this class have interface IF_WORKFLOW_TASK & this interface have method IF_WORKFLOW_TASK~CLOSE. Now my requirement is ,
    There is another class CL_WORKFLOW_CHAIN ,this class have interface IF_WORKFLOW_CHAIN & this interface have method IF_WORKFLOW_CHAINCLOSE_ALL_PREDECESSORS. Now i have to write my code in this method but i have to use IF_WORKFLOW_TASKCLOSE method for closing the task.
    Can you please give me the code for the above .
    Please waiting for reply.

    Hi,
    You can use the concept of INHERITANCE  in this scenario.By using this concept, you can call all the public and protected  methods of class CL_WORKFLOW_TASK  in the required calss CL_WORKFLOW_CHAIN as per your requirement.
    Go through the  Introdctory(INHERITANCE) programming from this SAPHELP link.
    http://help.sap.com/saphelp_nw70/helpdata/en/1d/df5f57127111d3b9390000e8353423/content.htm
    I hope, it will help in you inresolving your problem.
    by
    Prasad GVK.

  • Compilation error while calling static method from another class

    Hi,
    I am new to Java Programming. I have written two class files Dummy1 and Dummy2.java in the same package Test.
    In Dummy1.java I have declared a static final variable and a static method as you can see it below.
    package Test;
    import java.io.*;
    public class Dummy1
    public static final int var1= 10;
    public static int varDisp(int var2)
    return(var1+var2);
    This is program is compiling fine.
    I have called the static method varDisp from the class Dummy2 and it is as follows
    package Test;
    import java.io.*;
    public class Dummy2
    public int var3=15;
    public int test=0;
    test+=Dummy1.varDisp(var3);
    and when i compile Dummy2.java, there is a compilation error <identifier > expected.
    Please help me in this program.

    public class Dummy2
    public int var3=15;
    public int test=0;
    test+=Dummy1.varDisp(var3);
    }test+=Dummy1.varDisplay(var3);
    must be in a method, it cannot just be out somewhere in the class!

  • Calling a method from another class or accessing a component from another

    Hi all
    im trying to make a find/replace dialog box
    my main application form has a jtextpane and when i open up the find and replace dialog box it has two textboxes (find and replace)
    now i have the code to do the finding on my jtextpane but how do i call that code to do the find method?
    I have tried having the code in my main application class but then how do i call that method from my dialog box class?
    ive also tried having the code in my dialog box class, but then how to i tell it to work on my jtextpane which is in my main ap class?

    well if someone had been nice enough to provide me
    with a tutorial i wouldnt have gotten into this
    muddle, no need to be rude is there!I'm not rude. And you also wouldn't have gotten into the muddle if you searched yourself. This site provides many very good tutorials about all kinds of stuff.
    http://java.sun.com/docs/books/tutorial/java/javaOO/classes.htmlAmong other things, it mentions that "static" defines everything that belongs to a class, as opposed to an object.

  • Simple enough but... calling a method from another class

    Hi all,
    I know it's simple enough, although I can't see where I'm going wrong for some reason. Basically I've got a class which extends JPanel but when I try to call one of its methods from my main class I get an error. The relevant code (I hope) is:
    Main.java
    import java.awt.BorderLayout;
    import javax.swing.UIManager;
    public class Main extends JFrame implements ActionListener {
         JFrame frame;
         public static JPanel statusBar;
         public Main() {
              // Add a status bar
              statusBar = new StatusBar();
              mainPanel.add( statusBar, BorderLayout.SOUTH );
              setContentPane( mainPanel );
         public static void setStatusBarText( String s ) {
              statusBar.setStatusText("Test");
    StatusBar.java
    import java.awt.Color;
    import javax.swing.SwingConstants;
    public class StatusBar extends JPanel {
         private int barWidth;
         private static JLabel status;
         public StatusBar() {
              super();
              //barWidth = Main.getProgramWidth();
              //setPreferredSize( new Dimension(barWidth,22) );
              setLayout( new FlowLayout( FlowLayout.LEADING ) );
              setBorder( BorderFactory.createMatteBorder(1, 0, 0, 0, new Color(160,160,160) ) );
              status = new JLabel("Test", SwingConstants.LEFT);
              //status.setVerticalAlignment( SwingConstants.CENTER );
              add( status );
         protected void setStatusText( String s ) {
              status.setText( s );
              revalidate();
    }The "statusBar.setStatusText("Test");" call in the main class file doesn't work and I get the error:
    >
    cannot find symbol
    symbol : method setStatusText(java.lang.String)
    location: class javax.swing.JPanel
    statusBar.setStatusText("Test");
    >
    Any ideas what I'm doing wrong? I created an instance of the StatusBar class called statusBar, then I use this to call the setStatusText() method (via statusBar.setStatusText()) which I thought was all correct, then it doesn't work? Even if there's an easier way to do what I want to achieve (i.e. update a JLabel from the 'central' main class file instead of setting the JLabel to static and having every class call it directly), I'd appreciate knowing where I'm going wrong here nonetheless.
    Many thanks,
    Tristan

         public static JPanel statusBar;
         public static void setStatusBarText( String s ) {
              statusBar.setStatusText("Test");
    >
    cannot find symbol
    symbol : method setStatusText(java.lang.String)
    location: class javax.swing.JPanel
    statusBar.setStatusText("Test");
    >The type of variable statusBar is JPanel. That's all the compiler takes into account. It's irrelevant that at some point in the program the type of the object pointed to by this variable is a subclass of JPanel (because at another point it might be an instance of another subclass, or of JPanel itself.

  • How call a method from another class

    i make three class.class A,class B and class C.
    class A have One button.
    Class B have a action listener of that button.
    class c have a metod like
    public void test(){     }
    how can i call a method in class b from class c;
    is it necessary to pass the class a or b through the constructor of class c or another way to call the method.

    public class Foo
        public static void main(String[] args)
            Bar.staticFn();
            Bar b = new Bar();
            b.memberFn();
    class Bar
        public void memberFn()
            System.out.println("memberFn");
        public static void staticFn()
            System.out.println("staticFn");
      }

Maybe you are looking for

  • Can't open .xls file in numbers '09

    Please help, I just purchased iwork '09 and installed recently.  I'm using Mac OS X 10.6.8, numbers '09 version 2.0, pages '09 version 4.0.  When I try to open an .xls file the error is given 'Numbers quit unexpectedly while using the SFCompatibility

  • Apple Mac no longer accesses Windows XP shared Folder on Network

    Hi All, I have a PC running Windows XP hooked up via an ethernet hub to my three macs. I was able to access the "My Documents" folder on the PC no problem (I set it up to be shared on the PC). I recently had to replace my ADSL modem. I used the insta

  • How to extract a still from iMovie to create a jpg

    I'm trying to extract a still from iMovie to create a jpg. I've been able to make a freeze frame, but now I want to export it into iPhoto as a jpg? How?

  • Word 2008 won't recognize keyboard

    Any clues on why Word 2008 won't recognize my keyboard? I'd update the program, but can't find one. Help!

  • Why app "ps touch: (adobe photoshop touch) not installed to my iphone?

    Hi, i bought the application " adobe photoshop touch" from Apple iTunes, the money is already disscharged, but the problem is this app not installed to my iphone, and the full message that apperes when i try to installe is: the app PS Touch" was not