Solved

Add ActionListener to change TextArea

Posted on 2006-10-31
7
1,649 Views
Last Modified: 2012-06-21
Here is my code so far:

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

public class TestModule {
      public static void main(String[] args) {
            new Window();         
      }
}
class Window extends JFrame {
      public Window() {
            super ("Thermostat");
            setSize(600, 500);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setVisible(true);
            Container ca = getContentPane();
            ca.setBackground(Color.lightGray);
            GridBagLayout gblm = new GridBagLayout();
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.fill = GridBagConstraints.HORIZONTAL;
            ca.setLayout(gblm);
            
            //DISPLAY TEMPERATURE      
            JTextArea tempView = new JTextArea("76", 25, 25);
            gbc.gridx = 0;
            gbc.gridy = 0;
            ca.add(tempView,gbc);
                        
            //WARMER      BUTTON
            JButton warmer = new JButton("Warmer");
            gbc.gridx = 0;
            gbc.gridy = 1;
            ca.add(warmer,gbc);
            //?????   warmer.addActionListener(this);    ?????            
            
            //COOLER BUTTON
            JButton cooler = new JButton("Cooler");
            gbc.gridx = 0;
            gbc.gridy = 2;
            ca.add(cooler,gbc);      
                //?????   cooler.addActionListener(this);    ?????      
            
            setContentPane(ca);      
      }
      public void warmerActionPerformed(ActionEvent event) {
            //CHANGE TextArea up one degree      
      }
      
      public void coolerActionPerformed(ActionEvent event) {
            //CHANGE TextArea down one degree      
      }
}                  

Can someone please help me understand the ActionListener event ... how I can add it to the button ... capture the content of the text area, add or subtract 1 to it then redisplay it.  Also, please feel free to comment on how I have this set up ... I am new to JAVA and just trying to get a grasp on this process of actions.

Thanks in advance,
Rocky
0
Comment
Question by:rockymagee
  • 4
  • 3
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17843997
You need to add this method to your class

public void actionPerformed(ActionEvent e) {
      if ("Cooler".equals(e.getActionCommand()) ) {
            coolerActionPerformed(e);
      }
      else if ("Warmer".equals(e.getActionCommand()) ) {
            warmerActionPerformed(e);
      }
}
0
 
LVL 6

Author Comment

by:rockymagee
ID: 17844425
How to I reference the method from public Window()?

I added the recommended code .... and uncommented

warmer.addActionListener(this);
AND
cooler.addActionListener(this);

I get the following error:

addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (Window)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17844596
extends JFrame implements ActionListener
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 6

Author Comment

by:rockymagee
ID: 17844700
Thanks, that works!

If I could ask on more thing:

This is my modification to your actionPerformed(...):

public void actionPerformed(ActionEvent e) {
     if ("Cooler".equals(e.getActionCommand()) ) {
       tempValue = tempView.getText();
       tempValue = (int) tempValue;
       tempValue  = tempValue + 1;
//
       tempView.setText(tempValue);      
     }
     else if ("Warmer".equals(e.getActionCommand()) ) {
       tempValue = tempView.getText();
       tempValue = (int) tempValue;
       tempValue  = tempValue + 1;
       tempView.setText(tempValue);
     }
}

Questions:  Can I read out the string from the TextArea, cast it to an int | add or subtract | recast as string and set the new value back into the text area .... ?

I really appreciate the help, you got me around a big curve with the ActionListener that I could not figure out !
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 17845119
>>
Questions:  Can I read out the string from the TextArea, cast it to an int | add or subtract | recast as string and set the new value back into the text area .... ?
>>

Sure

//Let text area contain 100

tempView.setText("" + Integer.parseInt(tempView.getText()));
0
 
LVL 6

Author Comment

by:rockymagee
ID: 17845430
Perfect Thanks !

Thanks for the quick responses ...

A++
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17845996
:-)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now