Solved

Add ActionListener to change TextArea

Posted on 2006-10-31
7
1,667 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Tomcat: Unable to run tomcat service. 2 23
Java program running SQL query 5 37
jsp error 6 25
Running JavaFX on the Raspberry Pi 27 40
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

860 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