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

Thread: add an actionListener

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default add an actionListener

    hi,
    im new to java , and im trying to get this to work , this is my 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
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,739
    My Mood
    Mellow
    Thanks
    233
    Thanked 709 Times in 695 Posts

    Default Re: add an actionListener

    Welcome to the forum! Good first post. I've answered your immediate question below, but I'm going to spend a few more minutes looking at your code and I'll provide feedback on that when done.

    Pressing enter or the return key does not fire the actionPerformed() method unless it has been programmed to do so. You should start by adding a button with a listener that when selected calls the actionPerformed method and then evolve to adding key listeners.

    Edit: You're doing some good things. Comments would be nice, but I'm impressed that you've started the app on the EDT. I recommend that you construct and show the GUI from the class' constructor and methods it calls rather than from a single static method. I see no reason for your class to extend JPanel. Instead of a key listener that looks for returns, you could add a change listener to the JTextField to process keystrokes as the user makes them.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    mixpro (December 21st, 2013)

  4. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: add an actionListener

    thanks for your complements.
    but if you take a look at the first example : How to Use Text Fields (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
    and this is the source code : Java Tutorials Sample Code

    there is no button there and the actionPerformed() do get called when pressing enter, try it.

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,739
    My Mood
    Mellow
    Thanks
    233
    Thanked 709 Times in 695 Posts

    Default Re: add an actionListener

    Ahh. It's helpful to know the source of your code. I'll look at that, but I'd like to add that I didn't mean you HAD to have a button. I was suggesting starting with a button as a starting point from which to learn about and understand how action listeners work and what can be done with them.

    --- Update ---

    There are some significant differences between their code and yours. One of the most significant is that your code doesn't add a listener to the console text area. Besides that, their main frame adds an instance of the enclosing class so that explains why the main class extends JPanel and why their actionPerformed method in the main class is executed in response to the listener on their text field. Those 3 things (did I count right?) all have to be in place in order for the action on the text field to fire the actionPerformed() method in the main class:

    The 3 things:

    The main class extends JPanel and is added to the JFrame
    An action listener is added to the text area
    The main class also implements ActionListener and includes the required actionPerformed() method.

    Understanding how those are all required and then work together to provide the desired results is important to your study of action listeners and why I suggested you start with buttons (and a simpler example).

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

    mixpro (December 21st, 2013)

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,982
    Thanks
    52
    Thanked 2,270 Times in 2,242 Posts

    Default Re: add an actionListener

    Also posted at: add an actionListener
    If you don't understand my answer, don't ignore it, ask a question.

  8. #6
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: add an actionListener

    Also posted at: add an actionListener
    is that illegal ??? if so i didnt know sorry .


    its just i dont want to use buttons (design thing ), just one thing i cant understand , how is the textarea connected to the actionListener ? if u can explain which lines that would be great . and thanks for your help

  9. #7
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: add an actionListener

    Quote Originally Posted by mixpro View Post
    there is no button there and the actionPerformed() do get called when pressing enter, try it.
    Quote Originally Posted by mixpro View Post
    how is the textarea connected to the actionListener ?
    JTextField fires an ActionEvent every time the user presses Enter. This is by default (and by design). If you register an ActionListener you can receive that event.
    JTextArea is different, normally the Enter puts a newline in the text.
    Andrea, www.andbin.net SCJP 5 (91%) SCWCD 5 (94%)

    Useful links for Java beginners My new project Java Examples on Google Code

  10. #8
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: add an actionListener

    @GregBrannon , in the end i did what u said :
    I recommend that you construct and show the GUI from the class' constructor and methods it calls rather than from a single static method
    ok thanks all , i got it working , and now i have much clearer view about listeners thanks a lot

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. Replies: 5
    Last Post: February 15th, 2013, 04:01 PM
  3. ActionListener inside another ActionListener
    By kpat in forum AWT / Java Swing
    Replies: 6
    Last Post: March 28th, 2012, 03:43 PM
  4. How to Add ActionListener to a JButton in Swing?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 15
    Last Post: February 10th, 2011, 01:21 AM
  5. 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