Results 1 to 8 of 8
Thread: Cascading Windows
- 03-02-2009, 08:34 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 13
- Rep Power
- 0
Cascading Windows
hi could any one give me the necessary code to implement cascading frames, when i click on my "NEW FRAME" button and it creates more than one frame i need to know how to cascade them so a user can see that multiple screens are open. thanks.
Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; public class C212Ass2v0 extends JFrame { private JButton myFirstButton; private JButton mySecondButton; private JButton myThirdbutton; public C212Ass2v0() { super("Student ID: 50072220"); myFirstButton = new JButton("FIRST"); myFirstButton.setFont(new Font("Arial", Font.BOLD, 18)); myFirstButton.setBackground(Color.red); mySecondButton = new JButton("NEW FRAME"); mySecondButton.setFont(new Font ("Arial", Font.BOLD, 18)); mySecondButton.setBackground(Color.green); Container c = getContentPane(); FlowLayout fl = new FlowLayout(FlowLayout.LEFT); c.setLayout(fl); c.add(myFirstButton); c.add(mySecondButton); ButtonHandler handler = new ButtonHandler(); myFirstButton.addActionListener(handler); mySecondButton.addActionListener(handler); setSize(400, 300); setVisible(true); } public static void main(String[] args) { C212Ass2v0 f = new C212Ass2v0(); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.out.println("Exit via windowClosing."); System.exit(0); } }); } private class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { if (e.getSource() == myFirstButton) { System.out.println("Left Button has been pressed."); } if (e.getSource() == mySecondButton) { JFrame frame = new JFrame("New Frame"); frame.setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); frame.setVisible(true); Container c = frame.getContentPane(); FlowLayout f1 = new FlowLayout(FlowLayout.LEFT); c.setLayout(f1); myThirdbutton = new JButton ("THIRD"); myThirdbutton.setFont(myFirstButton.getFont()); myThirdbutton.setBackground(new Color(180, 100, 255)); c.add(myThirdbutton); frame.pack(); } // allows the button to be seen on the new window ButtonHandler handler = new ButtonHandler(); myThirdbutton.addActionListener(handler); setSize(400, 300); setVisible(true); } } }
- 03-02-2009, 10:28 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
setLocationRelativeTo(Component)
see the API docs for Window.
- 03-02-2009, 10:44 AM #3
Member
- Join Date
- Mar 2009
- Posts
- 13
- Rep Power
- 0
I looked at the api docs but im still unsure of how to go about my next step, when i use setLocationRelative what follows? i apologise i am very new to this. thanks for your help.
- 03-02-2009, 10:52 AM #4
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Java Code:newFrame.setLocationRelativeTo(lastFrame);
- 03-02-2009, 10:58 AM #5
Member
- Join Date
- Mar 2009
- Posts
- 13
- Rep Power
- 0
I keep getting these two errors are theer any parameters i need to put in place or will this statement allow the new frames to cascade? thanks
E:\C212Ass2v0.java:88: cannot find symbol
symbol : variable lastFrame
location: class C212Ass2v0.ButtonHandler
newFrame.setLocationRelativeTo(lastFrame);
^
E:\C212Ass2v0.java:88: cannot find symbol
symbol : variable newFrame
location: class C212Ass2v0.ButtonHandler
newFrame.setLocationRelativeTo(lastFrame);
^
2 errors
- 03-02-2009, 11:13 AM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
newFrame and lastFrame are "bogus" variable names. "lastFrame" will be the last Frame you created (whicheverof your variables that holds that value) and "newFrame" will be the next Frame to be created (whichever of your variables that hold that value). That is not a truely cut-n-paste line of code, simply an example that I thought should have been clear enough.
To tell you the truth, I haven't even looked at your code, yet (and I probably won't), so I don't know how much more work will be needed to implement this. Basically, you keep a reference of the last frame created, then, when you create a new frame, use this method on that new frame, then reset "lastFrame" to the "newFrame". Note, it will not work well if "lastFrame" is closed and then a new Frame should be opened, so may, rather, want to keep a List of all frame references and use the last item in the List as "lastFrame".
- 03-02-2009, 11:49 AM #7
Member
- Join Date
- Mar 2009
- Posts
- 13
- Rep Power
- 0
another quick question is there a bit of code that would enable only one new frame to be opened at any one time?
-
Similar Threads
-
Moments with Windows
By tim in forum EntertainmentReplies: 13Last Post: 11-10-2009, 12:34 PM -
Cascading Style Sheets
By smart in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 12-27-2008, 05:25 PM -
Docking windows
By Levish2002 in forum AWT / SwingReplies: 1Last Post: 09-03-2008, 03:13 AM -
windows registry
By Fleur in forum New To JavaReplies: 3Last Post: 06-20-2008, 03:23 AM -
swt for windows
By Gajesh Tripathi in forum SWT / JFaceReplies: 2Last Post: 10-18-2007, 06:43 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks