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 6 of 6

Thread: need help with ActionListener,JPanel,JFrame

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default need help with ActionListener,JPanel,JFrame

    hey everyone,

    can someone help me on this problem
    this is the problem, i have one instance of JFrame that implements an ActionListener
    and from this JFrame i need to call one instance of JPanel that also implements an ActionListener

    i have write these code below for testing purpose, and i still dont know what wrong with these code
    somehow i was unable to call the JPanel into the JFrame

    AL.java

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class AL extends JFrame implements ActionListener {
    	public JMenuBar menuBar;
    	public JMenu fileMenu, helpMenu;
    	public JMenuItem newAction, exitAction, tombolAction;
     
    	public AL() {
    		menuBar = new JMenuBar();
        	setJMenuBar(menuBar);
     
        	fileMenu = new JMenu("File");
            menuBar.add(fileMenu);
     
            newAction = new JMenuItem("New");
            exitAction = new JMenuItem("Exit");
            fileMenu.add(newAction);
            fileMenu.addSeparator();
            fileMenu.add(exitAction);
     
            newAction.addActionListener(this);
            exitAction.addActionListener(this);
     
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(300, 300);
            setLocationRelativeTo(null);
            setTitle("Save The Villages");
            setResizable(false);
            setVisible(true);    
    	}
     
    	public void actionPerformed(ActionEvent event) {
    		if(event.getSource()==newAction) {
    			System.out.println("You have clicked on the new action");
    			add(new AL_test());
    		}
    		if(event.getSource()==exitAction) {
    			System.exit(0);
    		}
    	}
     
    	public static void main(String[] args) {
            new AL();
        }
    }

    AL_test.java

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
    public class AL_test extends JPanel implements ActionListener {
    	JButton btn1 = new JButton();
     
    	public AL_test() {
    		btn1 = new JButton("baru");
    		add(btn1);
    		btn1.addActionListener(this); 
    	}
     
    	public void actionPerformed(ActionEvent event) {
    		if(event.getSource()==btn1) {
    			System.out.println("You have clicked on button");
    		}
    	}
    }

    can someone please enlighten me, what am i doing wrong

    and sorry for the bad English


  2. #2
    Junior Member
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: need help with ActionListener,JPanel,JFrame

    never mind, i already find out that im missing JFrame.show();
    under JFrame.add(new AL_test());

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: need help with ActionListener,JPanel,JFrame

    Quote Originally Posted by amahara View Post
    never mind, i already find out that im missing JFrame.show();
    under JFrame.add(new AL_test());
    JFrame.show is deprecated and a bit overkill, you can just call pack() and the JFrame will repack all of its container components.

  4. #4
    Junior Member
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: need help with ActionListener,JPanel,JFrame

    Quote Originally Posted by copeg View Post
    JFrame.show is deprecated and a bit overkill, you can just call pack() and the JFrame will repack all of its container components.
    thanks for the info copeg, i tried using pack() as you suggested, but somehow it resize my JFrame dimension
    so can i stop it from resizing itself?

    and just a quick question, can i use KeyListener in AL_test.java?
    because when i try it, its not working

     
    public AL_test() {
    	btn1 = new JButton("baru");
    	add(btn1);
    	btn1.addActionListener(this); 
    	KeyListener listener = new KeyListener() {
    		public void keyPressed(KeyEvent e) {
    			int key = e.getKeyCode();
          			if (key == KeyEvent.VK_SPACE) {
                				System.out.println("You have clicked on SPACE");
    	        		}
          		}
    		public void keyReleased(KeyEvent e) {}
    		public void keyTyped(KeyEvent e) {}
    	};
    	addKeyListener(listener);
    }

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: need help with ActionListener,JPanel,JFrame

    It will resize because it recalculates the layout based upon the preferred sizes of all it components. You can play with setting the preferred size of the panel to prevent this.

    The keyListener code won't compile because you have not implemented the entire interface. You can use a KeyAdapter which makes implementing the interface easier. In addition, the component you wish to listen for event must have focus, so after you call pack you need to call requestFocus on the appropriate component

  6. The Following User Says Thank You to copeg For This Useful Post:

    amahara (February 3rd, 2010)

  7. #6
    Junior Member
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: need help with ActionListener,JPanel,JFrame

    wow, thanks for your explanation copeg, this solved the problem

Similar Threads

  1. How to Add ActionListener to a JButton in Swing?
    By JavaPF in forum Java Swing Tutorials
    Replies: 17
    Last Post: April 24th, 2013, 05:14 PM
  2. Question about ActionListener
    By TimW in forum AWT / Java Swing
    Replies: 6
    Last Post: November 4th, 2009, 11:00 AM
  3. SOMEONE PLEASE HELP ME ADD THE ACTIONLISTENER INTERFACE...
    By beginning2Understand in forum AWT / Java Swing
    Replies: 5
    Last Post: June 30th, 2009, 12:42 AM
  4. How to write above a JPanel
    By bruno88 in forum AWT / Java Swing
    Replies: 4
    Last Post: June 23rd, 2009, 06:16 PM
  5. Creating and displaying a JPanel inside another JPanel
    By JayDuck in forum AWT / Java Swing
    Replies: 1
    Last Post: April 7th, 2009, 08:02 AM