Results 1 to 11 of 11
- 09-17-2009, 08:33 PM #1
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
How do you refere to a class Instance from another java file?
How?
I meen If Im making like this:
How do I refere to the window instance from another file?PHP Code:public class run{ public static void main(String args[]){ try{ GameFrame window = new GameFrame(); } catch (Exception e){ e.printStackTrace(); } } }
-
window.doSomething();How do I refer to the window instance from another file?
- 09-17-2009, 09:21 PM #3
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
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..
-
- 09-17-2009, 10:05 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
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?
- 09-18-2009, 08:40 AM #6
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
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.
- 09-18-2009, 11:49 AM #7
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
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(); } }
- 09-18-2009, 11:58 PM #8
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
No one knows the answer to this or ahve I missed something?
- 09-19-2009, 12:02 AM #9
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
look up getter and setter (accessor and modifier) methods
- 09-19-2009, 01:27 PM #10
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
AHAAA!!!
Good thinking!!'
I knew about those but I never tought of it anyways XD
thanks!
- 09-19-2009, 02:19 PM #11
Senior Member
- Join Date
- Aug 2009
- Posts
- 294
- Rep Power
- 0
Similar Threads
-
converting java class file to exe file
By satheeshtech in forum Advanced JavaReplies: 5Last Post: 07-18-2009, 11:47 PM -
Help me finish an instance method which references a class variable?
By trueblue in forum New To JavaReplies: 20Last Post: 06-03-2009, 05:33 PM -
create new instance of variable class
By Fedor in forum New To JavaReplies: 5Last Post: 04-12-2009, 08:13 PM -
create Instance of class in Javascript
By TDMaster in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 03-09-2009, 04:26 PM -
Naming a class instance with a variable
By pikalex88 in forum New To JavaReplies: 3Last Post: 09-30-2008, 06:27 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks