Results 1 to 7 of 7
Thread: How do you duplicate a window?
- 06-09-2011, 09:26 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 7
- Rep Power
- 0
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!
- 06-09-2011, 12:36 PM #2
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.
- 06-09-2011, 01:00 PM #3
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
- 06-10-2011, 03:06 AM #4
Member
- Join Date
- Jun 2011
- Posts
- 7
- Rep Power
- 0
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.
- 06-10-2011, 03:16 AM #5
Member
- Join Date
- Jun 2011
- Posts
- 7
- Rep Power
- 0
OH BOY! IT WORKS! Anyway, as a follow up, here's the snippet of code that does it.
Assuming newMenuItem is the button which you click to open the new window. THANKS PEOPLE!Java Code:newMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { GUI(); } });
- 06-10-2011, 06:11 AM #6
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.)
- 06-10-2011, 07:19 AM #7
Similar Threads
-
Need to pass a value from a parent window to a pop up or child window
By blackpanther in forum Advanced JavaReplies: 4Last Post: 01-10-2010, 07:48 AM -
Duplicate table
By anilkumar_vist in forum New To JavaReplies: 3Last Post: 01-09-2010, 11:17 AM -
change url in parent window from child window
By rakesh_n_mehta in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 07-09-2009, 12:17 PM -
how can i move one frame window to another window
By santhosh_el in forum AWT / SwingReplies: 8Last Post: 06-10-2009, 03:36 PM -
Duplicate XML decleration
By gyl2009 in forum XMLReplies: 0Last Post: 03-11-2009, 05:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks