943,608 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 17538
  • Java RSS
Apr 12th, 2008
0

Switching between Multiple Frames

Expand Post »
The following four classes are an attempt to set up three different JFrames and switch between them. The three frames are: MainView, DetailView, and FormView. The main class is MultipleFramesExample. The four source codes constitute a NetBeans project with a package called FrameViews. MainView, DetailView, and FormView are essentially identical source codes, each containing a JButton. When a button in one of the views is clicked, you are to be taken to another view.
When I try to Run the project, the last line of each source code creates a compile error, "Cannot find variable". Can anyone tell me how to correct the following four source codes?

package FrameViews;
import java.awt.*;
import javax.swing.*;
public class MultipleFramesExample extends JFrame {   
   public MultipleFramesExample() {  
   }
   public void main(String[] args) { 
    mV.createMainView(); 
   }   
}

_______________________________________

package FrameViews;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MainView extends JFrame implements ActionListener  {
      MainView MV = new MainView();      
      public void createMainView() {         
      JFrame frame1 = new JFrame();
      frame1.setTitle("Main View");
      frame1.setSize(50,50);
      frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame1.setVisible(true);      
      JButton button1 = new JButton("Click for Detail View");
      frame1.getContentPane().add(button1);
      button1.addActionListener(this);
      }
      public void actionPerformed(ActionEvent event) {
        dV.createDetailView();
      }
}
__________________________________________

package FrameViews;
import java.awt.event.*;
import javax.swing.*;
public class DetailView extends JFrame implements ActionListener {
      DetailView dV = new DetailView();      
      public void createDetailView() {      
      JFrame frame2 = new JFrame();
      frame2.setTitle("Detail View");
      frame2.setSize(100,100);
      frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame2.setVisible(true);     
      JButton button2 = new JButton("Click for Form View");
      frame2.getContentPane().add(button2);
      button2.addActionListener(this);
   }
      public void actionPerformed(ActionEvent event) {
      fV.createFormView();
}
}
____________________________________________

package FrameViews;
import java.awt.event.*;
import javax.swing.*;
public class FormView extends JFrame implements ActionListener {
      FormView fV = new FormView();   
   public void createFormView() {     
      JFrame frame3 = new JFrame();
      frame3.setTitle("Form View");
      frame3.setSize(75,75);
      frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame3.setVisible(true);      
      JButton button3 = new JButton("Click for Main View");
      frame3.getContentPane().add(button3);
      button3.addActionListener(this);
   }
   public void actionPerformed(ActionEvent event) {
        mV.createMainView();
   }
   }
_______________________________________

Thank you,
J.D. Seader
Reputation Points: 11
Solved Threads: 0
Newbie Poster
jdseader is offline Offline
15 posts
since Dec 2007
Apr 12th, 2008
0

Re: Switching between Multiple Frames

mV.createMainView();
mV has not been declared in this class.

dV.createDetailView();
dV has not been declared in this class.

etc etc.

You should make a new instance for the variables you want to use, or let them get returned from other classes (if you don't want a new one).

So in the first class this would be:
MainView MV = new MainView();

This should solve the problem I think. Please say so if I am wrong.

Greetings, jens
Reputation Points: 13
Solved Threads: 6
Light Poster
Jens is offline Offline
47 posts
since Apr 2008
Apr 12th, 2008
0

Re: Switching between Multiple Frames

Click to Expand / Collapse  Quote originally posted by Jens ...
mV.createMainView();
mV has not been declared in this class.

dV.createDetailView();
dV has not been declared in this class.

etc etc.

You should make a new instance for the variables you want to use, or let them get returned from other classes (if you don't want a new one).

So in the first class this would be:
MainView MV = new MainView();

This should solve the problem I think. Please say so if I am wrong.

Greetings, jens

Jens,
Thank you for your help. I followed your suggestion and added a declaration to each class directly after the class declaration. The program now compiles without any errors, but when I run it, no frames with buttons appear. I must have another problem. Can you help me?
J.D. Seader
Reputation Points: 11
Solved Threads: 0
Newbie Poster
jdseader is offline Offline
15 posts
since Dec 2007
Apr 12th, 2008
1

Re: Switching between Multiple Frames

Jens,
With a slight change, my project now works beautifully. Thank you very much for your help. I thought the package statement would eliminate the need for the declarations you suggested, but it didn't.
J.D. Seader
Reputation Points: 11
Solved Threads: 0
Newbie Poster
jdseader is offline Offline
15 posts
since Dec 2007
Sep 16th, 2009
0

Re: Switching between Multiple Frames

how would you do this in Netbean IDE? can you show the examples?

let say:

the first screen appear, and it has a series of button. each button goes to specific JFrame... but i have no clues until now.
Last edited by pvt_geeker; Sep 16th, 2009 at 1:30 pm. Reason: additional info added...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pvt_geeker is offline Offline
3 posts
since Sep 2009

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: Dumping bulk data from csv file into MS-Access DB.
Next Thread in Java Forum Timeline: Java code packaging for distribusion





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


Follow us on Twitter


© 2011 DaniWeb® LLC