944,027 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1555
  • Java RSS
Feb 17th, 2007
0

Passing control

Expand Post »
yay this first time that I develop something larger
My problem: main method display a frame with buttons where you choose what function/action you want to perform. Once you press a button I want the main window become invisible and windows with option for function to become visible.
So what I did is
Java Syntax (Toggle Plain Text)
  1. public void actionPerformed(ActionEvent e)
  2. {
  3. if(e.getSource() == btnFirst)
  4. {
  5. myApp.setVisible(false); // pass control to selected function
  6. doSomething();
  7. myApp.setVisible(true); // retrive control
  8. }
  9. }
however this get me following error
Java Syntax (Toggle Plain Text)
  1. C:\MyApp.java:54: cannot find symbol
  2. symbol : method doSomething()
  3. location: class MyApp
  4. doSomething();
  5. ^

with call doSomething() I want to triger this
Java Syntax (Toggle Plain Text)
  1. public RenameImage()
  2. {
  3. myFrame = new JFrame("Do Something");
  4. myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  5. buildGUI();
  6. myFrame.setSize(470,470);
  7. myFrame.setVisible(true);
  8. }

I don't even what to google for
Similar Threads
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,658 posts
since Dec 2004
Feb 18th, 2007
0

Re: Passing control

hmm, the blatantly obvious: check whether the method actually exists in the same class you're calling it from.

And of course rename RenameImage to renameImage.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Feb 18th, 2007
0

Re: Passing control

Came to same conclusion when I went to bed(funny enought I always find solution to problem went I go to sleep). But thats fine as long there would be solution :lol:
Now this peace of code doesn't do exactly what I want to do
Java Syntax (Toggle Plain Text)
  1. if(e.getSource() == btnFirst)
  2. {
  3. myApp.setVisible(false); // pass control to selected function
  4. doSomething();
  5. myApp.setVisible(true); // retrive control
  6. }
it hidde myApp for time which take to display second application window and then it will display it again. I'm missing sort of control mechanizm which check if second window still in use, if yes keep it hidden. I tried to use while loop and check boolean in second application, but that kicked my CPU activity to 100% and I have to force to shut-down.
So how I do I get around this?
Secondly, I use JFrame setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) to close my windows, but this kill both frames.
So how do I specify I want to close only second JFrame?
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,658 posts
since Dec 2004
Feb 19th, 2007
0

Re: Passing control

NEW UPDATE
Problem with killing both JFrames sorted, replaced JFrame.EXIT_ON_CLOSE with JFrame.DISPOSE_ON_CLOSE.
Now I add it WindowEvents so if windowDeactivated it will setVisible to false and windowActivated to setVisible to true. However I can't triger windowActivated when I close the second window.
What shall I do?
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,658 posts
since Dec 2004
Feb 21st, 2007
0

Re: Passing control

Finaly found solution to my problem :cheesy:
Bellow is example code from java forum(link here) which shows how this thing can be done
Java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class test extends JFrame implements ActionListener {
  6. JButton frame1Button, frame2Button;
  7. JFrame frame1, frame2;
  8.  
  9. public test() {
  10. super("Frame 1");
  11. frame2 = new JFrame("Frame 2");
  12. frame2Button = new JButton("Hide");
  13. frame2Button.addActionListener(this);
  14. JPanel frame2ButtonPanel = new JPanel();
  15. frame2ButtonPanel.add(frame2Button);
  16. frame2.getContentPane().add(frame2ButtonPanel, "North");
  17. frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  18. frame2.setSize(300,200);
  19. frame2.setLocation(350,350);
  20. frame2.setVisible(true);
  21. frame1 = this;
  22. frame1Button = new JButton("Hide");
  23. frame1Button.addActionListener(this);
  24. JPanel frame1ButtonPanel = new JPanel();
  25. frame1ButtonPanel.add(frame1Button);
  26. getContentPane().add(frame1ButtonPanel, "North");
  27. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  28. setSize(300,200);
  29. setLocation(100,100);
  30. setVisible(true);
  31. }
  32.  
  33. public void actionPerformed(ActionEvent e) {
  34. JButton button = (JButton)e.getSource();
  35. if(button == frame1Button) {
  36. frame1.setVisible(false);
  37. frame2.setVisible(true);
  38. }
  39. if(button == frame2Button) {
  40. frame1.setVisible(true);
  41. frame2.setVisible(false);
  42. }
  43. }
  44.  
  45. public static void main(String[] args) {
  46. new test();
  47. }
  48. }
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,658 posts
since Dec 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Wireless information sharing system
Next Thread in Java Forum Timeline: java swing





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC