Results 1 to 7 of 7
  1. #1
    Baldie is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default How do you duplicate a window?

    Hehe. I guess this is a noob question, but anyway...... I wrote this GUI, and included a button called New Window. Basically, I intended this button to open an EXACT DUPLICATE of the GUI. Here's an example to clarify this: You're surfing this page now. When you click the New Window button on your browser, it opens a new BLANK browser. Same idea, just that I need it to open the GUI. Anyone knows how?

    Thanks in advance!

  2. #2
    Mrc0d3r's Avatar
    Mrc0d3r is offline Member
    Join Date
    Jun 2011
    Location
    TCP/IP Layer 3
    Posts
    7
    Rep Power
    0

    Default

    Observe this code snippet (I wrote it in a rush ! ) Perhaps it isn't the best way, But it'll be enough for you understanding purpose.

    Important things to be noticed below :

    1. Adding an ActionListener to the button.
    2. Whenever the button is triggered a new Duplicated Frame is created. ( frame object )
    Java Code:
    import java.awt.Button;
    import java.awt.FlowLayout;
    import java.awt.Frame;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    /**
     *
     * @author Mrc0d3r
     */
    public class GUI extends Frame
    {
    
        Button b;
        public GUI()
        {
            setSize(300,300);
            setLayout(new FlowLayout());
            setTitle("your frame");
            b = new Button("new window");
            add(b);
            b.addActionListener(new ActionListener(){           /*  This is the most important part to be observed. */
                public void actionPerformed(ActionEvent e) {   
                    if(e.getSource() == b){                              
                        new GUI();                                           
                    }                                                          
                }
            });
            setVisible(true);
        }
    
    
        public static void main(String ...args){
            new GUI();
        }
    
    }
    Last edited by Mrc0d3r; 06-09-2011 at 12:47 PM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    Mrc0d3r, we're all here to learn, not to copy. Please don't post full code solutions, and especially not bad code solutions.

    In your ActionListener implementation, would you like to elaborate on when e.getSource() will not be equal to b?

    db

  4. #4
    Baldie is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    Well, like what he said, he wrote it in a rush, and I never expected full codes for copy-paste operations. Hmm...... I'll go try this out. Thanks. (And btw, bad code solutions make you think how to modify to make it work. It's good practice for newbs like me.)
    Last edited by Baldie; 06-10-2011 at 03:18 AM.

  5. #5
    Baldie is offline Member
    Join Date
    Jun 2011
    Posts
    7
    Rep Power
    0

    Default

    OH BOY! IT WORKS! Anyway, as a follow up, here's the snippet of code that does it.
    Java Code:
    newMenuItem.addActionListener(new ActionListener() {
    	       public void actionPerformed(ActionEvent ae) {
    	           GUI();
    			}
    		});
    Assuming newMenuItem is the button which you click to open the new window. THANKS PEOPLE!

  6. #6
    Mrc0d3r's Avatar
    Mrc0d3r is offline Member
    Join Date
    Jun 2011
    Location
    TCP/IP Layer 3
    Posts
    7
    Rep Power
    0

    Default

    Darryl, I'd like to mention a few points here :

    1.OK, I've got that. I'll not post full code .(Yeah, even I thought it was unnecessary.)
    2.*bad code* I already mentioned about it. I know that creating *anonymous objects* at a couple of places is bad. ( I suppose, there are even more parts that require some change.)
    3.Yeah, you are right, the if statement was unnecessary. Because, the ActionListener was defined only for the button b and therefore the event generated will always correspond to b. So, directly writing the set of actions we want to be performed can be written in the actionPerformed(). No requirement of if statement indeed. (I was habituated to write to write like that.Sorry for that.)

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    I know that creating *anonymous objects* at a couple of places is bad.
    Nothing at all wrong with using anonymous inner classes where the question of re-usability doesn't arise.

    For the rest, thanks!

    db

Similar Threads

  1. Replies: 4
    Last Post: 01-10-2010, 07:48 AM
  2. Duplicate table
    By anilkumar_vist in forum New To Java
    Replies: 3
    Last Post: 01-09-2010, 11:17 AM
  3. change url in parent window from child window
    By rakesh_n_mehta in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-09-2009, 12:17 PM
  4. how can i move one frame window to another window
    By santhosh_el in forum AWT / Swing
    Replies: 8
    Last Post: 06-10-2009, 03:36 PM
  5. Duplicate XML decleration
    By gyl2009 in forum XML
    Replies: 0
    Last Post: 03-11-2009, 05:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •