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

Thread: Multiply windows

  1. #1
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    20
    Posts
    3,842
    Blog Entries
    4

    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 !

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

  2. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    37
    Posts
    12,912
    Blog Entries
    57

    Re: Multiply windows

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

  3. #3
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    20
    Posts
    3,842
    Blog Entries
    4

    Re: Multiply windows

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

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

  4. #4
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,035

    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?

  5. #5
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    20
    Posts
    3,842
    Blog Entries
    4

    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 09:49 AM. Reason: I was lost beyond reasoing.

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

  6. #6
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,035

    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.

  7. #7
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    20
    Posts
    3,842
    Blog Entries
    4

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

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

  8. #8
    Code Warrior marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89 is a glorious beacon of light marwex89's Avatar
    Join Date
    Jul 2008
    Location
    Somewhere that is shorter to write than "In the gloomy shadows of my personal namespace"
    Posts
    10,737
    Blog Entries
    2

    Re: Multiply windows

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

  9. #9
    Code Warrior Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n has much to be proud of Turk4n's Avatar
    Join Date
    May 2008
    Location
    4chan.org/g/
    Age
    20
    Posts
    3,842
    Blog Entries
    4

    Re: Multiply windows

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

    Hatsune Miku ~❤❤❤
    初音ミク。~❤❤❤

  10. #10
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,751
    Blog Entries
    97

    Re: Multiply windows

    I like it! +rep

+ Reply to Thread
Page 1 of 2
1 2 LastLast

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Conficker??? real or fake
    By cabalsun in forum Software Security
    Replies: 19
    Last Post: 08-29-2009, 08:09 PM
  2. Windows 7 Beta Users Upgrade Fast
    By Chinmoy in forum Computer Software/OS
    Replies: 0
    Last Post: 06-24-2009, 04:59 PM
  3. Visual Studio 2005 and Windows Vista
    By Jordan in forum General Programming
    Replies: 14
    Last Post: 01-10-2009, 12:39 PM
  4. Windows XP Tricks & Tips!!!!..new ones.
    By pranky in forum Tutorials
    Replies: 9
    Last Post: 08-23-2008, 12:22 PM
  5. Windows Shortcut List - Saves Time
    By 2stamlers in forum The Lounge
    Replies: 6
    Last Post: 04-10-2008, 03:58 AM