Solved

Add ActionListener to change TextArea

Posted on 2006-10-31
7
1,682 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

728 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