+ Reply to Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: Multiply windows

  1. #1
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,851
    Blog Entries
    4
    Rep Power
    49

    Multiply windows

    Good morning CodeCall !

    I had before a request of how to create a multiply window function on a frame. Which gives the user ability to open new frames and dispose it without affecting nor freezing the previous window.

    So let's get going !


    Packages
    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    Since the user requested a detailed one I will explain it with a bit more heart...
    By creating GUI tools, we need to have something that will draw the windows and tools for it. Java presents their own GUI API, swing. The awt presents our Listener functions and the awt.event presents our "event" accordance...

    The class
    Code:
    public class DrawWindow extends JFrame implements ActionListener {
    By using extends we inherit every single method from the JFrame class itself. Which residents inside our javax.swing package...Which makes it really neat
    Our variables
    Code:
    private static int totNumbers = 0;
    	private static int nextNr = 0;
    	private int Nr;
    	private JMenuBar mb = new JMenuBar();
    	private JMenu men = new JMenu("Window");
    	private JMenuItem New = new JMenuItem("New window");
    	private JMenuItem reset = new JMenuItem("Reset");
    	private JMenuItem close = new JMenuItem("Quit");
    By creating 2 static integers to keep the track of numbers of window we create we are able to give a future method which I will present soon, which will help us to dispose and open windows with any bother.
    The int Nr will present the numbers of window created to our WindowAdapter later on.While creating a JMenuBar, you can add buttons and items to it, you can also just add icons.
    Constructor
    Code:
    public DrawWindow() {
    		totNumbers++;
    		Nr = nextNr++;
    		setTitle("Drawing "+ (Nr+1));
    		setLocation(30*Nr,30*Nr);
    		setJMenuBar(mb);
    		mb.add(men);
    		men.add(New); men.add(close);
    		
    		New.addActionListener(this);
    		close.addActionListener(this);
    		addWindowListener(w);
    		setSize(300,300);
    		setVisible(true);
    		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
    	}
    The constructor is our base frame. Now, every single time we are about to launch a new window, we will use these standards we point out. As well does it add the total numbers of windows as the current windows open. Now comes the part. Why didn't I use a JFrame instance? Well if I can inherit it instead. Then you don't need to refer to an instance variable. Just type out the methods and add your components !
    Action methods...
    Code:
    public void actionPerformed(ActionEvent e) {
    		if(e.getSource()==New) {
    			new DrawWindow();
    		}
    		else if (e.getSource()==close) {
    			dispose();
    		}
    	}
    Here we have our actions. While pressing New in the JMenuItem inside the Bar. We can launch a new DrawWindow. Hence this is the method of launching a new frame.
    While our close is disposing the old ones.
    WindowAdapter
    Code:
    private WindowListener w = new WindowAdapter() {
    		public void windowClosed(WindowEvent e) {
    			if(--totNumbers==0)
    				System.exit(0);
    		}
    	};
    Here we have our Window listener. To keep track of our windows we are checking if the totNumbers equals to 0. Making it simple for use to exit without crashing application.
    The main
    Code:
    	public static void main(String arg[]) {
    		new DrawWindow();
    	}
    }
    Our main, the heart of launch our application. Which makes it simple to understand we are only launching the method. DrawWindow, the rest is just the windows

    Hope this will help for the rest and the requester !
    Output
    Multiply windows-outputdrawwindow.png

    P.S - Every window are on they own, you can move every of them without stopping the other !

    Cheers !

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143

    Re: Multiply windows

    Well done again +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,851
    Blog Entries
    4
    Rep Power
    49

    Re: Multiply windows

    Quote Originally Posted by WingedPanther View Post
    Well done again +rep
    Thank you WingedPanther

  5. #4
    Join Date
    Mar 2008
    Posts
    7,144
    Rep Power
    86

    Re: Multiply windows

    Again, very nice.

    Say you want to open a new window but you want to make it so you can't use the window that spawned the new window until the new window is closed. How would you do that?

  6. #5
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,851
    Blog Entries
    4
    Rep Power
    49

    Re: Multiply windows

    Quote Originally Posted by chili5 View Post
    Again, very nice.

    Say you want to open a new window but you want to make it so you can't use the window that spawned the new window until the new window is closed. How would you do that?
    I wouldn't use the same main constructor method to call out a window. I would create a new instance and use it.
    Last edited by Turk4n; 08-10-2009 at 10:49 AM. Reason: I was lost beyond reasoing.

  7. #6
    Join Date
    Mar 2008
    Posts
    7,144
    Rep Power
    86

    Re: Multiply windows

    Yes, you could do that but you could still change focus to a previous window. What if you don't want that to happen?

    You skip using the window listener. You will only spawn new windows with focus on. :>
    You could change the focus back.

  8. #7
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,851
    Blog Entries
    4
    Rep Power
    49

    Re: Multiply windows

    Quote Originally Posted by chili5 View Post
    Yes, you could do that but you could still change focus to a previous window. What if you don't want that to happen?



    You could change the focus back.
    I could, however my reasoning was wrong LOL :X
    In this case I have to change from calling the constructor to making a new instance method to create new windows...

  9. #8
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,725
    Blog Entries
    2
    Rep Power
    90

    Re: Multiply windows

    Nice work! (can't +rep you as you know)
    Hey! Check out my new Toyota keyboaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

  10. #9
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,851
    Blog Entries
    4
    Rep Power
    49

    Re: Multiply windows

    Quote Originally Posted by marwex89 View Post
    Nice work! (can't +rep you as you know)
    Haha no problem
    Thanks !

  11. #10
    Jordan Guest

    Re: Multiply windows

    I like it! +rep

+ Reply to Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 07-12-2011, 09:08 AM
  2. How to multiply INT and DOUBLE....
    By yourmom615 in forum Java Help
    Replies: 7
    Last Post: 03-22-2011, 04:52 PM
  3. COM windows help
    By esum26 in forum C and C++
    Replies: 0
    Last Post: 07-26-2010, 02:22 PM

Tags for this Thread

Bookmarks

Posting Permissions

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