Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Need Help Action Listener....

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need Help Action Listener....

    need help on actionlistener....i have 2 use action listener to open a new Jframe from my Main JFrame...wat i have to do is when i click my MenuItem (ex. File, open), a new frame is opened...


    here is my Mainframe
    CODE:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
     
    public class menu extends JFrame{
     
    	private JMenuBar jmMenuBar;
    	private JMenu jmFile;
    	private JMenuItem jmNew;
     
    	public menu(){
     
    		JMenuBar jmbMenuBar = new JMenuBar();
    		JMenu jmFile = new JMenu ("File");
    		JMenuItem jmiOpen = new JMenuItem ("Open");
    		JMenuItem jmiSave = new JMenuItem ("Save");
    		JMenuItem jmiSaveAs = new JMenuItem ("SaveAs");
    		jmFile.add(jmiOpen);
    		jmFile.add(jmiSave);
    		jmFile.add(jmiSaveAs);
    		jmbMenuBar.add(jmFile);
     
    		JMenu jmEdit = new JMenu ("Edit");
    		JMenuItem jmiCut = new JMenuItem ("Cut");
    		JMenuItem jmiCopy = new JMenuItem ("Copy");
    		JMenuItem jmiPaste = new JMenuItem ("Paste");
    		jmEdit.add(jmiCut);
    		jmEdit.add(jmiCopy);
    		jmEdit.add(jmiPaste);
    		jmbMenuBar.add(jmEdit);
     
    		JMenu jmView = new JMenu ("View");
    		JMenuItem jmiToolBar = new JMenuItem ("ToolBar");
    		JMenuItem jmiStatusBar = new JMenuItem ("StatusBar");
    		JMenuItem jmiFormatBar = new JMenuItem ("FormatBar");
    		jmView.add(jmiToolBar);
    		jmView.add(jmiStatusBar);
    		jmView.add(jmiFormatBar);
    		jmbMenuBar.add(jmView);
     
    		setJMenuBar (jmbMenuBar);
    		}
     
    	public static void main (String[]args){
     
    		menu myApp = new menu();
    		myApp.setSize(500,400);
    		myApp.setTitle("BSIT II-O");
    		myApp.setLocation(0,0);
    		myApp.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		myApp.setVisible(true);
    	}
    }		
     
    and this is the second frame that should be opened when i click on my MenuItem from my Main Frame
     
    	import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
     
    public class Menu extends Jframe{
     
    	private JLabel jlbName;
    	private JTextField jtfName;
    	private JButton jbSave;
    	private FlowLayout flLayout;
     
    	public Menu (){
     
    		jlbName = new JLabel ("Name");
    		jtfName = new JTextField (30);
    		jbSave = new JButton ("Save");
    		flLayout = new FlowLayout ();
     
    		add(jlbName);
    		add(jtfName);
    		add(jbSave);
     
    		jlbName.setForeground (Color.BLUE);
    		jtfName.setBackground (Color.PINK);
    		jtfName.setFont (new Font ("Vivaldi",3,12));
    		setLayout (flLayout);
    	}
    public static void main (String args[]){
    	Menu form = new Menu ();
    	form.setLocation (100,200);
    	form.setSize(800,600);
    	form.setTitle ("BSIT II-O");
    	form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	form.setVisible(true);
    	}
    }

    I think i have to use actionListener but i dont know how to...can someone help me with my problem...?Plzzzz....tnx in advance...
    Last edited by Freaky Chris; October 9th, 2009 at 03:21 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need Help Action Listener....

    I don't know if JMenuBar works like MenuBar, but I know that MenuBar makes you add the action listener individually to each menu item. To add an action listener, your class needs to implement the ActionListener interface and implement the actionPerformed(ActionListener e) method.

    // init. code
    		JMenuBar jmbMenuBar = new JMenuBar();
    		JMenu jmFile = new JMenu ("File");
    		JMenuItem jmiOpen = new JMenuItem ("Open");
    		jmiOpen.addActionListener(this);
    		JMenuItem jmiSave = new JMenuItem ("Save");
    		jmiSave.addActionLIstener(this);
    		JMenuItem jmiSaveAs = new JMenuItem ("SaveAs");
    		jmiSave.addActionListener(this):
    		jmFile.add(jmiOpen);
    		jmFile.add(jmiSave);
    		jmFile.add(jmiSaveAs);
    		jmbMenuBar.add(jmFile);

  3. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Need Help Action Listener....

    Correct!

    Chris