Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,452 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,585 people online right now. Registration is fast and FREE... Join Now!




Inserting ActionListener into GUI

 

Inserting ActionListener into GUI, problem inserting listener for text field

javamad

3 Jul, 2009 - 03:07 AM
Post #1

D.I.C Head
**

Joined: 8 Jun, 2009
Posts: 65



Thanked: 1 times
My Contributions
Hi, I am trying to create a GUI and need a hand inserting an ActionListener for a textField. The GUI at present consists of one text field which I would like an operator to insert a value. I am trying to insert an ActionListener for this value but an unsure as to where it goes. Could someone let me know how to insert an ActionListener for a text field into this gui please. Please see below for code.

Many thanks
CODE

package gui2;
import javax.swing.SpringLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public abstract class SpringDemo1 extends JPanel implements ActionListener {
    /**
    * Create the GUI and show it.  For thread safety,
    * this method should be invoked from the
    * event-dispatching thread.
    */
   private static void createAndShowGUI()  {
       //Create and set up the window.
       JFrame frame = new JFrame("SpringDemo1");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       //Set up the content pane.
       Container contentPane = frame.getContentPane();
       SpringLayout layout = new SpringLayout();
       contentPane.setLayout(layout);
      
       JLabel buildUpLabel = new JLabel("buildup DPs: ");
              
       JTextField buildUpTextField = new JTextField("0", 5);
              
          
       contentPane.add(buildUpLabel);
       contentPane.add(buildUpTextField);
            

     //Adjust constraints for the label so it's at (5,5).
       layout.putConstraint(SpringLayout.WEST, buildUpLabel,5,SpringLayout.WEST, contentPane);
       layout.putConstraint(SpringLayout.NORTH, buildUpLabel,5,SpringLayout.NORTH, contentPane);
      
       //Adjust constraints for the text field so it's at
       //(<label's right edge> + 5, 5).
       layout.putConstraint(SpringLayout.WEST, buildUpTextField,24, SpringLayout.EAST, buildUpLabel);
       layout.putConstraint(SpringLayout.NORTH, buildUpTextField,5, SpringLayout.NORTH, contentPane);
      
      

       //Adjust constraints for the content pane: Its right
       //edge should be 5 pixels beyond the text field's right
       //edge, and its bottom edge should be 5 pixels beyond
       //the bottom edge of the tallest component (which we'll
       //assume is textField).
       layout.putConstraint(SpringLayout.EAST, contentPane,500,SpringLayout.EAST, buildUpTextField);
       layout.putConstraint(SpringLayout.SOUTH, contentPane,500,SpringLayout.SOUTH, buildUpTextField);
      
      
       //Display the window.
       frame.pack();
       frame.setVisible(true);
      
      
      
   }

   public static void main(String[] args) {
       //Schedule a job for the event-dispatching thread:
       //creating and showing this application's GUI.
       javax.swing.SwingUtilities.invokeLater(new Runnable() {
           public void run() {
               createAndShowGUI();
              
              
              
           }
          
          
       });
      
      

   }
  
}


User is offlineProfile CardPM
+Quote Post


baavgai

RE: Inserting ActionListener Into GUI

3 Jul, 2009 - 04:25 AM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 4,261



Thanked: 389 times
Dream Kudos: 550
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Your code says the class "implements ActionListener". That means you're entered into a contract to implement the methods defined in that interface. Of course, the "abstract" part saves you from actually doing that.

To be honest, this is one of the strangest ways I've ever seen to fire up a form. Static is bad. Firing up another thread is senseless. And, for your listener to work, it helps to have a object that's willing to listen.

Here's an example
java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class SpringDemo1 extends JFrame implements ActionListener {

// this is our text box, we may want to keep track of it
private JTextField buildUpTextField = null;

// this is a JFrame, use it!
public SpringDemo1() {
super("SpringDemo1");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Container contentPane = getContentPane();
SpringLayout layout = new SpringLayout();
contentPane.setLayout(layout);

JLabel buildUpLabel = new JLabel("buildup DPs: ");

buildUpTextField = new JTextField("0", 5);
// add the listener
buildUpTextField.addActionListener(this);

contentPane.add(buildUpLabel);
contentPane.add(buildUpTextField);

layout.putConstraint(SpringLayout.WEST, buildUpLabel,5,SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, buildUpLabel,5,SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, buildUpTextField,24, SpringLayout.EAST, buildUpLabel);
layout.putConstraint(SpringLayout.NORTH, buildUpTextField,5, SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.EAST, contentPane,500,SpringLayout.EAST, buildUpTextField);
layout.putConstraint(SpringLayout.SOUTH, contentPane,500,SpringLayout.SOUTH, buildUpTextField);

pack();
}

// actions fire here
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if (src==this.buildUpTextField) {
System.out.println("Text Action!");
}
}

public static void main(String[] args) {
JFrame frm = new SpringDemo1();
frm.setVisible(true);
}
}


This post has been edited by baavgai: 3 Jul, 2009 - 04:26 AM
User is offlineProfile CardPM
+Quote Post

javamad

RE: Inserting ActionListener Into GUI

3 Jul, 2009 - 04:59 AM
Post #3

D.I.C Head
**

Joined: 8 Jun, 2009
Posts: 65



Thanked: 1 times
My Contributions
Hi, Many thanks! Your right looking at the code you posted it makes alot more sense!

One last thing, could you let me know how I can hold the contents of the text field in a variable please so I can interrogate it else where in the finished gui

Many thanks again
User is offlineProfile CardPM
+Quote Post

Fuzzyness

RE: Inserting ActionListener Into GUI

3 Jul, 2009 - 06:23 AM
Post #4

Comp Sci Student
Group Icon

Joined: 6 Mar, 2009
Posts: 1,149



Thanked: 173 times
Dream Kudos: 150
My Contributions
String contents = jTextFieldName.getText();
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 01:53AM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month