Solved

Add ActionListener to change TextArea

Posted on 2006-10-31
7
1,673 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
Industry Leaders: 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!

 
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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
even odd program using while loop 3 72
how to see all occupied ports on windows 10 laptop 15 82
Bot application - advice 3 58
DTD and JAVA versions 1 27
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

713 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