Go Back   Java Programming Forums > Java Standard Edition Programming Help > AWT / Java Swing

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-02-2010, 03:06 PM
Junior Member
 

Join Date: Feb 2010
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
amahara is on a distinguished road
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

Java Code
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

Java Code
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



Reply With Quote Share this thread on Facebook
Sponsored Links
  #2 (permalink)  
Old 03-02-2010, 03:33 PM
Junior Member
 

Join Date: Feb 2010
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
amahara is on a distinguished road
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());
Reply With Quote
  #3 (permalink)  
Old 03-02-2010, 04:42 PM
Member
 

Join Date: Oct 2009
Posts: 311
Thanks: 4
Thanked 90 Times in 84 Posts
copeg will become famous soon enough

I'm feeling Mellow
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.
Reply With Quote
  #4 (permalink)  
Old 03-02-2010, 04:54 PM
Junior Member
 

Join Date: Feb 2010
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
amahara is on a distinguished road
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

Java Code
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);
}
Reply With Quote
  #5 (permalink)  
Old 03-02-2010, 06:18 PM
Member
 

Join Date: Oct 2009
Posts: 311
Thanks: 4
Thanked 90 Times in 84 Posts
copeg will become famous soon enough

I'm feeling Mellow
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
Reply With Quote
The Following User Says Thank You to copeg For This Useful Post:
amahara (03-02-2010)
  #6 (permalink)  
Old 03-02-2010, 06:40 PM
Junior Member
 

Join Date: Feb 2010
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
amahara is on a distinguished road
Default Re: need help with ActionListener,JPanel,JFrame

wow, thanks for your explanation copeg, this solved the problem
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about ActionListener TimW AWT / Java Swing 6 04-11-2009 04:00 PM
How to Add ActionListener to a JButton. Java Swing JavaPF Java Code Snippets and Tutorials 3 20-10-2009 11:15 AM
SOMEONE PLEASE HELP ME ADD THE ACTIONLISTENER INTERFACE... beginning2Understand AWT / Java Swing 5 30-06-2009 05:42 AM
How to write above a JPanel bruno88 AWT / Java Swing 4 23-06-2009 11:16 PM
Creating, and displaying a JPanel inside another JPanel JayDuck AWT / Java Swing 1 07-04-2009 01:02 PM


100 most searched terms
Search Cloud
2 dimensional arraylist java 2d arraylist java actionlistener actionlistener in java actionlistener java actionlistener jbutton addactionlistener addactionlistener java avatar hardware id convert double to integer java double format java double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java hardware id avatar java 2 dimensional arraylist java 2d arraylist java actionlistener java addactionlistener java button actionlistener java convert double to int java double format java double to int java double to integer java for beginner eclippse java format double java forum java forums java get mouse position java ipod touch java jbutton java list to map java mouse position java programming forum java programming forums java sendkeys java.lang.reflect.invocationtargetexception java.util.arraylist jbutton actionlistener jbutton java programming forums string to int java two dimensional arraylist java writing apps for ipod touch

All times are GMT. The time now is 01:01 PM.
Powered by vBulletin® Copyright ©2000-2009, Jelsoft Enterprises Ltd.