Hi all,
please help me connecting exit menu to OnExit event handler
Here is what I have coded so far!

Also a coding question: What is the best method between these two:
1. Creating instance of JFrame and call its methods like set Size
2. create class that extends JFrame and override the methods with mine

Thanks a lot

package learnswing;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class MainJava{
    public static void main(String[] args){
        Frame test = new Frame();
        test.MakeFrame();
    }
}

class Frame{
    public void MakeFrame(){
        JFrame frame = new JFrame();
        //make menu
        this.MakeMenu(frame);
        frame.setSize(300, 200);
        frame.setTitle("Iam JFrame");
        frame.setVisible(true);
    }

    private void MakeMenu(JFrame fr){
        //declarations
        JMenuBar menubar;
        JMenu file, help;
        JMenuItem openfile, exit;
        JMenuItem about;
        //create menu
        menubar = new JMenuBar();

        //file menu
        file = new JMenu("File");
        //add components
        openfile = new JMenuItem("Open...Ctrl+O");
        openfile.setMnemonic(KeyEvent.VK_O);
        file.add(openfile);
        exit = new JMenuItem("Exit...Ctrl+Q");
        exit.setMnemonic(KeyEvent.VK_Q);
        //tie to action
        //exit.addActionListener(this.OnExit());
        file.addSeparator();
        file.add(exit);
        //ad them to menubar
        menubar.add(file);

        //help menu
        help = new JMenu("Help");
        //add components
        about = new JMenuItem("Help...Ctrl+H");
        about.setMnemonic(KeyEvent.VK_H);
        help.add(about);
        //ad them to menubar
        menubar.add(help);

        //set menubar
        fr.setJMenuBar(menubar);
    }

    private void OnExit(ActionEvent evt){
        System.exit(0);
    }
}

You should only extend a class if you need to add functionality to the class (or modify the class's functionality so that it does something different). I doubt that you need to do either of those things for JFrame, so unless you have a compelling argument, don't extend JFrame.

Also, I'm pretty sure that AWT already has a class called Frame, so you should probably pick a different class name which is more suited to your application, or it might result in confusion. And the way in which you can "know" when your JFrame's "x" has been clicked to close it is by implementing WindowListener. So use class Frame implements WindowListener, then override all of the necessary methods.

However, you should be aware that in your particular case, what I mentioned above will work but is not necessary. Instead, you can simply use the following bit of code to cause the JFrame to be closed when it is clicked on:
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFrame.html#setDefaultCloseOperation%28int%29

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Thanks a lot. Your explanation makes sense and it is invaluable help!
I really appreciate. However, I have a question here. How can I bind my frame's menu item to a method?

//package learnswing;
import java.awt.event.ActionEvent;
import java.awt.event.*;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class MainJava{
    public static void main(String[] args){
        Frame test = new Frame();
        test.MakeFrame();
    }
}

class Frame{
    public void MakeFrame(){
        JFrame frame = new JFrame();
        //make menu
        this.MakeMenu(frame);
        frame.setSize(300, 200);
        frame.setTitle("Iam JFrame");
        frame.setVisible(true);
    }

    private void MakeMenu(JFrame fr){
        //declarations
        JMenuBar menubar;
        JMenu file, help;
        JMenuItem openfile, exit;
        JMenuItem about;
        //create menu
        menubar = new JMenuBar();

        //file menu
        file = new JMenu("File");
        //add components
        openfile = new JMenuItem("Open...Ctrl+O");
        openfile.setMnemonic(KeyEvent.VK_O);
        file.add(openfile);
        exit = new JMenuItem("Exit...Ctrl+Q");
        exit.setMnemonic(KeyEvent.VK_Q);
        //tie to action
        exit.addActionListener( new OnExit());
        file.addSeparator();
        file.add(exit);
        //ad them to menubar
        menubar.add(file);

        //help menu
        help = new JMenu("Help");
        //add components
        about = new JMenuItem("Help...Ctrl+H");
        about.setMnemonic(KeyEvent.VK_H);
        help.add(about);
        //ad them to menubar
        menubar.add(help);

        //set menubar
        fr.setJMenuBar(menubar);
    }

    private class OnExit implements ActionListener{
	public void actionPerformed(ActionEvent evt){
        System.exit(0);
	}
    }
}

i think to attach a event handler in java, we must create a class which implements the given listener instead of creating a function and try to attach it as ActionListener.

Yes, you have to have some class that implements ActionListener. It is, however, perfectly acceptable in some cases to be the same class that defines the frame, or an anonymous class, or some other inner class, or some other class that also performs other Listener responsibilities such as WindowListener or ItemListener. IOW it all depends.

Edited 7 Years Ago by masijade: n/a

Also a coding question: What is the best method between these two:
1. Creating instance of JFrame and call its methods like set Size
2. create class that extends JFrame and override the methods with mine

i find the 2nd option best almost in every scenario.

public class MyFrame extends JFrame{

public MyFrame()
{
// either create or design the JFrame here or call the some other function like makeMyFrame() to design the JFrame
}
    public static void main(String[] args) {
    	SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				new MyFrame();
			}
		});
    }
}
}

doesnt it look easy and clean way to make Frames

i find the 2nd option best almost in every scenario.

public class MyFrame extends JFrame{

public MyFrame()
{
// either create or design the JFrame here or call the some other function like makeMyFrame() to design the JFrame
}
    public static void main(String[] args) {
    	SwingUtilities.invokeLater(new Runnable(){
			public void run(){
				new MyFrame();
			}
		});
    }
}
}

doesnt it look easy and clean way to make Frames

Most "beginners" do. That is wrong however. Prefer, as a rule of thumb, composition over inheritance.

Most "beginners" do. That is wrong however. Prefer, as a rule of thumb, composition over inheritance.

can you please elaborate "composition over inheritance" or give any link for it. :)

"extends" is inheritance. Composition is using the base classes rather than extending them. Doing it this way does not "lock" you into a specific "framework". It makes is much easier to provide different GUI's (I.E. a Swing Gui, a web Interface, a CLI) without large changes to the "interaction" between the application itself and it's GUI (these are separate things in your program, right? The app is not completely mixed in with the GUI, is it?). You also can only extend one class, and since you usually only need about 5 things from whatever GUI component you think about extending it makes little sense extending it and inheriting the hundreds of other things along with them (which are, of course, also exposed to the rest of the app which is probably a recipe for disaster, or at least misuse). Also, if you must (or would like) to extend a different class because you need it's functionality, then you're stuck because you've already extended a component class. There are all sorts of reasons not to, and the only reason to do it is the ease of use of, max, 5 methods?

This article has been dead for over six months. Start a new discussion instead.