Results 1 to 5 of 5
  1. #1
    mixpro is offline Member
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Default add an actionListener

    hi,
    im new to java , and im trying to get this to work , this is my code :
    Java Code:
    public class TextDemo extends JPanel implements ActionListener {
    	protected static JTextField commandField;
    	protected static JTextArea consoleArea;
    	protected static JTextArea outputArea;
    	private final static String newline = "\n";
    	
    
    	public void actionPerformed(ActionEvent evt) {
    		String text = commandField.getText();
    		consoleArea.append(text + newline);
    		commandField.selectAll();
    
    		// Make sure the new text is visible, even if there
    		// was a selection in the text area.
    		consoleArea.setCaretPosition(consoleArea.getDocument().getLength());
    	}
    	private static void createAndShowGUI() {
    		JFrame frame = new JFrame("Client");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Container contentPane = frame.getContentPane();
    		contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
    
    		JPanel left = new JPanel();
    		left.setLayout(new BoxLayout(left, BoxLayout.Y_AXIS));
    		consoleArea = new JTextArea(19,20);
    		JScrollPane scrollPaneConsole = new JScrollPane(consoleArea);
    		left.add(scrollPaneConsole);
    		commandField = new JTextField();
    		left.add(commandField);
    		left.setBorder(BorderFactory.createTitledBorder("Console"));
    		JPanel right = new JPanel();
    		right.setLayout(new BoxLayout(right, BoxLayout.Y_AXIS));
    		outputArea = new JTextArea(20,20); 
    		JScrollPane scrollPaneOutput = new JScrollPane(outputArea);
    		right.add(scrollPaneOutput);
    		right.setBorder(BorderFactory.createTitledBorder("Output"));
    
    		
    		contentPane.add(left);
    		contentPane.add(right);
    		frame.pack();
    		frame.setVisible(true);
    	}
    
    	public static void main(String args[]) {
    
    		javax.swing.SwingUtilities.invokeLater(new Runnable() {
    			public void run() {
    				createAndShowGUI();
    			}
    		});
    	}
    
    }
    i want to be able to enter a text in the commandField and when pressing enter the text to be shown in the console area ,
    why isnt my actionPerformed never gets called ??

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,408
    Rep Power
    25

    Default Re: add an actionListener

    Where is the actionListener added to a component?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    mixpro is offline Member
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Default Re: add an actionListener

    well, because of my class implements an actionlistener() , doesnt that mean when an action is happening in it it triggers the actionperformed method ?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,408
    Rep Power
    25

    Default Re: add an actionListener

    Implementing an interface does not connect a class to a component. The code needs to explicitly tell the component what its actionlistener is. It is possible to have many different actionlisteners in a class by having inner classes for example. The code must tell the JVM which one to use by adding it to the component.


    Also posted at: http://www.javaprogrammingforums.com...tml#post134640
    Last edited by Norm; 12-21-2013 at 02:03 PM.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    mixpro is offline Member
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Default Re: add an actionListener

    ok thanks
    Last edited by mixpro; 12-21-2013 at 02:07 PM.

Similar Threads

  1. ActionListener help
    By nat in forum New To Java
    Replies: 2
    Last Post: 12-14-2013, 09:07 PM
  2. Actionlistener q
    By stuckonjava in forum New To Java
    Replies: 2
    Last Post: 05-16-2012, 08:17 PM
  3. ActionListener Help
    By rakosky in forum AWT / Swing
    Replies: 4
    Last Post: 04-06-2012, 03:59 PM
  4. Please help with actionlistener
    By ADustedEwok in forum New To Java
    Replies: 5
    Last Post: 12-08-2011, 10:04 PM
  5. ActionListener
    By jaylimix in forum Java Applets
    Replies: 5
    Last Post: 11-06-2011, 06:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •