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 !


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 šŸ˜€
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.

public DrawWindow() {


Nr = nextNr++;

("Drawing "+ (Nr+1));




.add(New); men.add(close);



        & nbsp;       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) {




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.

private WindowListener w = new WindowAdapter() {

public void windowClosed(WindowEvent e) {





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 šŸ™‚

What Do You Think on This ? Say Here