Solved

Add ActionListener to change TextArea

Posted on 2006-10-31
7
1,653 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

911 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

24 Experts available now in Live!

Get 1:1 Help Now