Solved

Open JFrame from JFrame

Posted on 2004-04-17
7
765 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

828 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