Results 1 to 11 of 11
  1. #1
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default How do you refere to a class Instance from another java file?

    How?
    I meen If Im making like this:

    PHP Code:
    public class run{
    	public static void main(String args[]){
    		
    		try{
    			GameFrame window = new GameFrame();
    			
    		} catch (Exception e){
    			e.printStackTrace();
    		}
    	}
    
    }
    How do I refere to the window instance from another file?

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

  3. #3
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    No that doesnt work.
    Then I thinks Im trying to use some kinda of package, which im not!
    Im trying to make one file reffer to the instance of a class that is in another class..

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

    Default

    Quote Originally Posted by Addez View Post
    No that doesnt work.
    This unfortunately tells us little. How does it not work specifically? If you see error messages, what messages do you see?
    Then I thinks Im trying to use some kinda of package, which im not!
    You probably should be using packages, and if not, why not?

  5. #5
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    sry, this is what I ment:

    I got a class named "GameFrame" in one .java file (call it java file 1)
    In that java file theres also a class called "run"
    The run class creates an Instance of the GameFrame class by doing:

    GameFrame window = new GameFrame();


    Then I got another .java file (call it java file 2) that I have a class in.
    Inside that class I wish to be able to get and set some properties of the GameFrame instance which was created in java file 1.

    But if I inside the class, inside java file 2, just type window.methodName();
    Then How in the world is it suppose to know that java file 1 have created an instance of the GameFrame class and is calling it window?

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You need to design your application properly using object oriented principles.
    If an object needs to call actions on another object then either the calling object has a HAS-A relationship with the object it's trying to call so you would need an instance variable of the object in the calling class or you simply want to call the actions in one method of the calling class in which case the method doing the calling needs to take that object as a parameter.

  7. #7
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    Okej, so now I'v trued to fix it.
    But the problem is, I get no error when compiling nor when running.
    And the update function is looping, but it doesnt do the command:

    run.window.status.setText(blahblha);

    Idk why, But it doesnt show anything..

    So what im trying to acomplise in this code is that the myapp function is running. and this function is crypting some files.
    While that one is running the update function should run at same time and change the status label on the top.

    But it doesnt change it, not even once!

    Heres my odd peice of code:

    PHP Code:
    import src.myapp;
    import javax.swing.*;
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.*;
    
    public class run{
    	static double pD = 0.0;
    	static GameFrame window;
    	public static void main(String args[]){
    		
    		try{
    			window = new GameFrame();
    			//wind = window;
    			
    		} catch (Exception e){
    			e.printStackTrace();
    		}
    	}
    
    }
    
    
    class GameFrame extends JFrame implements ActionListener {
    	JLabel status = new JLabel("");
    	JTextField textField = new JTextField(30);
    	JComboBox convertOpt = new JComboBox();
    	JButton go = new JButton("Start");
    	
        public GameFrame() throws IOException {
        	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        	setLayout(new BorderLayout());
        	add(status,BorderLayout.NORTH);
        	add(textField,BorderLayout.WEST);
        	convertOpt.addItem("Crypt");
        	convertOpt.addItem("Decrypt");
        	add(convertOpt,BorderLayout.EAST);
        	add(go,BorderLayout.SOUTH);
        	go.addActionListener(this);
        	status.setText("Status: Inactive");
        	pack();
        	setVisible(true);
        }
        
        public void actionPerformed(ActionEvent e){
        	String path = textField.getText();
        	System.out.println(path);
        	Boolean crypting  =false;
        	if (convertOpt.getSelectedItem().equals("Crypt")) {
        		crypting = true;
        	}
        	if (convertOpt.getSelectedItem().equals("Decrypt")){
        		crypting = false;
        	}
        	if (!path.equals("")){
        		if (crypting != null){
    	    		try {
    	    			status.setText(crypting ? "Crypting" : "Decrypting");
    	    			myapp cApp = new myapp();
    	    			cApp.start();
    	    			cApp.run("C:\\Users\\Andreas\\Desktop\\"+path,crypting);
    	    			
    	    			updateing updat = new updateing();
    	    			updat.start();
    	    			updat.update();
    
    	    			
    	    		} catch (Exception error){
    	    			error.printStackTrace();
    	    		}
    	    	} else {
    	    		status.setText("Choose a crypting Option!");
    	    	}
        	} else {
        		status.setText("Insert a file path");
        	}
        	
        }
        
        
    }
    
    class updateing extends Thread{
    	public void update(){
    		System.out.println("go");
        	double procentDone = run.pD;
        	run.window.status.setText("Status: "+String.valueOf(procentDone)+"% complete!");
        	try {
        		Thread.sleep(1000);
        	} catch (Exception e){}
        	update();
        }
    }

  8. #8
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    No one knows the answer to this or ahve I missed something?

  9. #9
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    look up getter and setter (accessor and modifier) methods

  10. #10
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    AHAAA!!!
    Good thinking!!'
    I knew about those but I never tought of it anyways XD
    thanks!

  11. #11
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    now I give up!

    I have for days trying to fix this shit error and NOTHING works!
    I'v tried doing setter and getter methods, doesnt work!
    Once I dont get any errors it still doesnt change the text!!

    Im sick and tired of this code, im off!

Similar Threads

  1. converting java class file to exe file
    By satheeshtech in forum Advanced Java
    Replies: 5
    Last Post: 07-19-2009, 12:47 AM
  2. Replies: 20
    Last Post: 06-03-2009, 06:33 PM
  3. create new instance of variable class
    By Fedor in forum New To Java
    Replies: 5
    Last Post: 04-12-2009, 09:13 PM
  4. create Instance of class in Javascript
    By TDMaster in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-09-2009, 05:26 PM
  5. Naming a class instance with a variable
    By pikalex88 in forum New To Java
    Replies: 3
    Last Post: 09-30-2008, 07:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •