?
Solved

Open JFrame from JFrame

Posted on 2004-04-17
7
Medium Priority
?
778 Views
Last Modified: 2008-02-26
Hi
How do I open a new frame by clickingon a menu item in the first main frame? I tried it but something must be wrong since nothing happens when i click on the menu item anymore
I'm sure this is simple for you experts but it's urgent
thanks
0
Comment
Question by:Samooramad
  • 4
  • 3
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10849175
In the menu item event handler:


addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
            otherFrame.show();
      }
});
0
 

Author Comment

by:Samooramad
ID: 10849206
I'm not sure if that is the problem. The made the secondary frame into a class and declared it with something like:

secframeclass newframe = new secframeclass();

is that wrong?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10849213
>>is that wrong?

No - not necessarily. Obviously you need to set the size before you show it.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Samooramad
ID: 10849228
so i still need to call it with show?
could you give me an example?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 10849317
Something like:


public class F extends JFrame {
      JFrame otherFrame;
      JPanel buttonPanel;


      private void setGui() {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            // Create other frame
            otherFrame = new JFrame("Hello");
            otherFrame.setSize(100, 100);
            buttonPanel = new JPanel();
            JButton bnShowFrame = new JButton("Show/hide frame");
            buttonPanel.add(bnShowFrame);
            bnShowFrame.addActionListener(
                  new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                              otherFrame.setVisible(!otherFrame.isVisible());
                        }
                  });
            getContentPane().add(buttonPanel, BorderLayout.CENTER);
      }


      public static void main(String[] args) {
            F f = new F();
            f.setGui();
            f.pack();
            f.setVisible(true);
      }
}

0
 

Author Comment

by:Samooramad
ID: 10849382
thaaaaaaaaaaaaaaaaaaaaaaaank you
that was quick and great!!!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10849408
8-)
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month15 days, 19 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question