Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Multiply windows


  • Please log in to reply
12 replies to this topic

#1 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 10 August 2009 - 01:24 AM

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
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
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 :D
Our variables
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
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...
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
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
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
[ATTACH]1880[/ATTACH]

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

Cheers !

Attached Thumbnails

  • outputDrawWindow.png

  • 2

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 10 August 2009 - 06:41 AM

Well done again :) +rep
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 10 August 2009 - 06:46 AM

Well done again :) +rep


Thank you WingedPanther :)
  • 0

#4 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 10 August 2009 - 09:39 AM

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?
  • 0

#5 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 10 August 2009 - 09:42 AM

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.

Edited by Turk4n, 10 August 2009 - 09:49 AM.
I was lost beyond reasoing.

  • 0

#6 chili5

chili5

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3038 posts
  • Programming Language:Java, C#, PHP, JavaScript, Ruby, Transact-SQL
  • Learning:C, Java, C++, C#, PHP, JavaScript, Ruby, Transact-SQL, Assembly, Scheme, Haskell, Others

Posted 10 August 2009 - 10:04 AM

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

#7 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 10 August 2009 - 10:12 AM

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

#8 marwex89

marwex89

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 2857 posts

Posted 10 August 2009 - 10:58 AM

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

#9 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 10 August 2009 - 10:59 AM

Nice work! (can't +rep you as you know)


Haha no problem :D
Thanks !
  • 0

#10 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 28 August 2009 - 05:54 AM

I like it! +rep
  • 0

#11 Turk4n

Turk4n

    ???

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1919 posts
  • Location:Sweden
  • Programming Language:C, Java, PHP, Python, Bash
  • Learning:C++, C#, JavaScript, Visual Basic .NET, Others

Posted 28 August 2009 - 06:31 AM

I like it! +rep


Why thank you :D
  • 0

#12 H2O Pure

H2O Pure

    CC Regular

  • Just Joined
  • PipPipPip
  • 26 posts

Posted 17 June 2010 - 07:19 AM

I know its an old thread and all but thanks for your work. Really helped with my application :thumbup:
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download