Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Action Listener for AWT Java 1.1 help. 4

Status
Not open for further replies.

mtg82814

Programmer
May 22, 2000
5
US
I'm not too familiar with java, but I was wondering if someone could help me, I have a form with three buttons on it. All I want to do Is call a function when one of the buttons is clicked.

I can't seem to find any examples of ActionListener for AWT that's NOT an applet(all examples i've found are applets).

Code someone take a look at my code and tell me how to make onclick events for my buttons. For example when the button called "Send" is selected, change the text field "Email" to say "Email Me"(or anything for that matter)

I work in an environment where the computers all have java 1.1. So I can't use the Swing classes.

Thanks in advance.


Here is my code:



import java.util.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class rvtDamon
{

public static void main(String args[]) {

//define layout manager
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();

//define buttons
Button bSend = new Button("Send");
Button bBrowse = new Button("Browse");
Button bQuit = new Button("Quit");

//define the textfields
TextField tEmail = new TextField();
TextField tFile = new TextField();

//define the label
Label lblEmail = new Label("Email:");
Label lblFile = new Label("File:");

//create the frame(GUI)
Frame f = new Frame("RVT Damon");
f.setLayout(gridbag);

//add the email label
c.gridx = 0;
c.gridy = 0;
gridbag.setConstraints(lblEmail, c);
f.add(lblEmail);

//add the email textfield
c.gridx = 0;
c.gridy = 1;
c.ipadx = 300;
c.gridwidth = 3;
gridbag.setConstraints(tEmail, c);
f.add(tEmail);
c.ipadx = 0;
c.gridwidth = 1;

//add the file label
c.gridx = 0;
c.gridy = 3;
gridbag.setConstraints(lblFile, c);
f.add(lblFile);

//add the file textfield
c.gridx = 0;
c.gridy = 4;
c.ipadx = 300;
c.gridwidth = 3;
gridbag.setConstraints(tFile, c);
f.add(tFile);
c.ipadx = 0;
c.gridwidth = 1;

//add the browse button
c.gridx = 3;
c.gridy = 4;
gridbag.setConstraints(bBrowse, c);
f.add(bBrowse);

//add the Send Button
c.gridx = 2;
c.gridy = 5;
gridbag.setConstraints(bSend, c);
f.add(bSend);

//add the quit button
c.gridx = 3;
c.gridy = 5;
gridbag.setConstraints(bQuit, c);
f.add(bQuit);

f.pack();
f.show();
}


}
 
Hi,

Actually it is the same for applets or applications :) In case you need it, here is the API for JDK1.1
What you can do is :-

import java.util.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class rvtDamon implements ActionListener
{
// need to declare this as a global variable so that you will be able to access it from other methods
Label lblEmail;
public static void main(String args[]) {
//define layout manager
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();

//define buttons
Button bSend = new Button("Send");
bSend.addActionListener(this);
Button bBrowse = new Button("Browse");
bBrowse.addActionListener(this);
Button bQuit = new Button("Quit");
bQuit.addActionListener(this);

//define the textfields
TextField tEmail = new TextField();
TextField tFile = new TextField();

//define the label
lblEmail = new Label("Email:");
Label lblFile = new Label("File:");

//create the frame(GUI)
Frame f = new Frame("RVT Damon");
f.setLayout(gridbag);

//add the email label
c.gridx = 0;
c.gridy = 0;
gridbag.setConstraints(lblEmail, c);
f.add(lblEmail);

//add the email textfield
c.gridx = 0;
c.gridy = 1;
c.ipadx = 300;
c.gridwidth = 3;
gridbag.setConstraints(tEmail, c);
f.add(tEmail);
c.ipadx = 0;
c.gridwidth = 1;

//add the file label
c.gridx = 0;
c.gridy = 3;
gridbag.setConstraints(lblFile, c);
f.add(lblFile);

//add the file textfield
c.gridx = 0;
c.gridy = 4;
c.ipadx = 300;
c.gridwidth = 3;
gridbag.setConstraints(tFile, c);
f.add(tFile);
c.ipadx = 0;
c.gridwidth = 1;

//add the browse button
c.gridx = 3;
c.gridy = 4;
gridbag.setConstraints(bBrowse, c);
f.add(bBrowse);

//add the Send Button
c.gridx = 2;
c.gridy = 5;
gridbag.setConstraints(bSend, c);
f.add(bSend);

//add the quit button
c.gridx = 3;
c.gridy = 5;
gridbag.setConstraints(bQuit, c);
f.add(bQuit);

f.pack();
f.show();
}

public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
if (s.equals("Send"))
{
// just do what you want here when the button Send is clicked
lblEmail.setText("Email me");
}
}
}

If you don't understand the codes, feel free to ask me again :)

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
That upper code works fine, but you have just modify it litte because you can't refer to non-static variables from static method (and can't use that actionListener).

public class rvtDamon
{
Label lblEmail;

public static void main(String[] args)
{
new rvtDamon();
}

public rvtDamon()
{
... and the code here ...
}
}

...just wanted to mention this in case that you're not able get it work ;)

-Vepo
 
You are correct! I missed out that part...haha...I was just copying his codes and adding the necessary things for the actionlistener. Thanks for mentioning it :)

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Above code works very well. So look at the following as a personal preference of mine:

Instead of just adding
Code:
this
as ActionListener, I prefer making inner classes for all the actions that could occur. The downside is that there are inner classes X-) (if you don't like them), the upside is that a little more structure is achieved (and so the maintainability improves). Also if there are many actions that can be cast, the
Code:
actionPerformed
function does need to distinguish between the different kinds of actions anyway, as follows:

Code:
if (e.getSource == bSend)
{
  System.out.println("Send was pressed");
}
else if (e.getSource == bBrowse)
{
  // browse
}
else if (e.getSource == bQuit)
{
  System.exit(0);
}

In above example the Buttons (bSend, bQuit and bBrowse) also need to be static (class variables) to be able to be accessed from the function later on (after initialization).

To follow the different approach try this: Instead of just 3 times adding
Code:
this
, do the following:

Code:
Button bSend = new Button("Send");
bSend.addActionListener(new SendActionListener());
Button bBrowse = new Button("Browse");
bBrowse.addActionListener(new BrowseActionListener());
Button bQuit = new Button("Quit");
bQuit.addActionListener(new QuitActionListener());

and then add the inner classes like you would normally add a function, according to the following scheme:

Code:
public class rvtDaemon
{
  public rvtDaemon
  {
    // your constructor
  }

  // other functions

  protected class SendActionListener implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {
      // send
    }
  }

  protected class BrowseActionListener implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {
      // browse
    }
  }

  protected class QuitActionListener implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {
      // quit
    }
  }
}

Hope I did help a little...
 
I want to thank everyone who helped me with this. I really appreciate it.

I usually program in perl or VB, so making the jump to java has been a little tough for me. It's good to know that there are people willing to help.

Thanks,

Mike
Raytheon Co.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top