Click to See Complete Forum and Search --> : java 1.1, applets, actions on a button, and textfield values


TheLinuxDuck
08-02-2001, 05:30 PM
I feel as though I'm trying to climb a sheer rock wall with nothing but a rock hammer.

I have been working trying to understand making forms in a java applet, but it's not working like I want it to, and it's just generally being confusing.

I don't have swing capability, so I'm using awt.

I have figured out how to create the display, and line up the Labels, TextFields, and Buttons so they look nice, and have pretty colors, but I can't seem to get 'addActionListener' to work correctly, so I'm using the deprecated 'action'. I also am cornfused on how to even get the text that a user enters into a textField once they click the 'go' button.

Can someone post a simple example of this?

If I am being vague or otherwise silly, please say so!

Dru Lee Parsec
08-02-2001, 07:00 PM
Here's some code that
A) doesn't use Swing
B) sets up an action listener to a button
C) takes the text out of the top text field and puts it into the bottom label when the button is clicked.

Hope it helps LD.


import java.awt.*;
import java.awt.event.*;

// A non Swing version
public class ButtonTest extends Panel
{
private Button button = new Button("Click Me");
private Label label = new Label();
private TextField field = new TextField();

public ButtonTest(){
buildUI();
buildListeners();

}

public static void main(String args[])
{


Frame theFrame = new Frame("Java Action Test");
ButtonTest thePanel = new ButtonTest();
theFrame.add(thePanel);
theFrame.setSize(200,150);
theFrame.setVisible(true);

theFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);
}
});

}


private void buildUI(){
setLayout(new GridLayout(3,1));
add(field);
add(button);
add(label);
}

private void buildListeners(){
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
label.setText(field.getText());
}
});

}
}

Dru Lee Parsec
08-02-2001, 07:04 PM
Oh BTW. 5 minutes! Ka-Ching!

Most fun I've had coding all day. :)

For the "how to line up stuff" question, you usually need to put panels inside panels and use combinations of GridLayout, BorderLayout, and FlowLayout. (Try to avoid GridBag).

Give me a simple description and I'll try to throw an example together. But I'm leaving town in about an hour so I may not get to it until Monday.

C-Ya round LD!

P.S. You joined LNO a year after I did. How did you get 3 1/2 times more post than me??? :(

TheLinuxDuck
08-03-2001, 09:44 AM
Originally posted by Dru Lee Parsec:
<STRONG>Oh BTW. 5 minutes! Ka-Ching!

Most fun I've had coding all day. :)
</STRONG>

Qool.. (^= Glad to help you out!

<STRONG>For the "how to line up stuff" question, you usually need to put panels inside panels and use combinations of GridLayout, BorderLayout, and FlowLayout. (Try to avoid GridBag).</STRONG>

Well, the lining up thing isn't really an issue right now.. I'm simply using a null layout manager, and setBounds'ing all the items for precise locations.. I'm not concerned about resizing or anything, since it is an applet.

<STRONG>P.S. You joined LNO a year after I did. How did you get 3 1/2 times more post than me??? :(</STRONG>

Heh.. I spend a lot of time in GOT, as well as here. Besides, I post alot of meaningless stuff, whereas you tend to go for "quality not quantity".

Anyhow, thanks for the input. I *was* actually setting up the addActionHandler correctly, however, for some reason, my browser just doesn't want to load an applet that uses it. If I remove it and use the deprecated action method, it works fine.

Does that mean that the system that the browser is on is running an ever earlier version of java? How can I tell?

Just so you know what this is about, it is a simple login form. However, it will inevitibly be used for secure logins w/o SSL (thanks for jemfinch for the idea).

The trouble is just getting the action stuff to work right.

Now, I must have been drinking cough syrup or something, because the method for getting the user input'd data from a form item, at least a TextField, is easy. I didn't even see the getText (inherited from TextComponent). I was surprised (yesterday) to find that there wasn't anything like that. But, you have shown me the light.

So, I can at least use the action method, even though it's deprecated, until you get back on Monday andcan help me to solve my dilemma (or anyone else knowledgable). (^=

With that said, I am off like a light switch.

Dru Lee Parsec
08-06-2001, 04:03 PM
This code seems to work for me:
import java.awt.*;

/*
&lt;applet code="ButtonTestApplet" width="200" height="200" &gt;&lt;/applet&gt;
*/

public class ButtonTestApplet extends java.applet.Applet
{


public void init()
{
add(new ButtonTest());
}

}

This puts the former code in an aplet. Let me know if this doesn't work for you.