2 Replies Latest reply: Apr 10, 2012 2:47 AM by 927536 RSS

    How to use a variable between interfaces or class?

    927536
      Hello All;

      Far by now I can say that the forum is very helpful for me. Thanks for all the people who have the time and generosity to help our problems. Thank you.

      Unfortunately I have a problem <Again :( >

      I want to use a variable in two different *.java files.

      Is it possible?

      The Variable i want to use is; int CiftSayisi

      The variable is declared in the CamCalib.Java under a button's Action Listener.

      Here is the code;
      public void actionPerformed(ActionEvent arg0) {
                          ImagePair.main(null);
                          String[] ImPath = list.getItems();                                           //Storing Items in an Array
                          Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();                     //Dimensions of the Screen Storing in an object
                          int ScreenHeight = dim.height;                                           //Definition ScreenHeight
                          int ScreenWidth = dim.width;                                          //Definition ScreenWidth
                          // System.out.println(ScreenHeight);                                                    //Testing of the Dimensions
                          // System.out.println(ScreenWidth);                                                     //Testing of the Dimensions
                          int CiftSayisi = ImPath.length -1 ;
                          if (CiftSayisi == 0){
                               System.out.println("Added Images do not Construct a Pair");
                          }
                          else {
                               
                                      }
      }
      Now i want to use it in another java file which is ImagePair.Java in the loading part of the interface;
                   setType(Type.UTILITY);
                setTitle("Image Pair Selection");
                setResizable(false);
                setAlwaysOnTop(true);
                setBounds(100, 100, 450, 68);
                contentPane = new JPanel();
                contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
                setContentPane(contentPane);
                contentPane.setLayout(null);
                
                textField = new JTextField();
                textField.setBounds(61, 11, 22, 20);
                textField.setEditable(false);
                textField.setEnabled(false);
                contentPane.add(textField);
                textField.setColumns(2);
      I have a text field in here that i need to fill with the value of CiftSayisi

      Any help please?

      ömer kaya
      METU-GGIT

      Edited by: Ömer K. on Apr 10, 2012 12:43 AM
        • 1. Re: How to use a variable between interfaces or class?
          aksarben
          I didn't bother reading the code, since you didn't format it.

          But to answer your question (if I understand it correctly), yes, it is possible to pass variables between different object instances (not from an interface specifically, since interfaces don't have members). The simplest way to pass the variable as an argument to a method or constructor. If these aren't available, you could create a static global variable, though in a multithreaded app you'd have to synchronize access.
          • 2. Re: How to use a variable between interfaces or class?
            927536
            Hello;

            Thanks for the answers;

            Sorry for the code format; i didn't know it, now i know (sorry new to everything here; :| )

            I couldn't understand what you mean by argument to a method? i searched the web but couldn't find any answers regarding that.

            Could you show some code for me?

            I mean not related to my specific problem but how can it be done?

            any web link is helpful too.

            ömer kaya
            METU-GGIT