Results 1 to 8 of 8
  1. #1
    billbo123 is offline Member
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

    Default 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);
    
    		}
    
    
    	}
    
    }

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    setLocationRelativeTo(Component)

    see the API docs for Window.

  3. #3
    billbo123 is offline Member
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

    Default

    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.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Java Code:
    newFrame.setLocationRelativeTo(lastFrame);

  5. #5
    billbo123 is offline Member
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

    Default

    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

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    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".

  7. #7
    billbo123 is offline Member
    Join Date
    Mar 2009
    Posts
    13
    Rep Power
    0

    Default

    another quick question is there a bit of code that would enable only one new frame to be opened at any one time?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Moments with Windows
    By tim in forum Entertainment
    Replies: 13
    Last Post: 11-10-2009, 12:34 PM
  2. Cascading Style Sheets
    By smart in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 12-27-2008, 05:25 PM
  3. Docking windows
    By Levish2002 in forum AWT / Swing
    Replies: 1
    Last Post: 09-03-2008, 03:13 AM
  4. windows registry
    By Fleur in forum New To Java
    Replies: 3
    Last Post: 06-20-2008, 03:23 AM
  5. swt for windows
    By Gajesh Tripathi in forum SWT / JFace
    Replies: 2
    Last Post: 10-18-2007, 06:43 AM

Posting Permissions

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