aspose file tools*
The moose likes Beginning Java and the fly likes Pulling out hair Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Pulling out hair" Watch "Pulling out hair" New topic
Author

Pulling out hair

Dave Morley
Ranch Hand

Joined: Jul 23, 2004
Posts: 38
I am hoping that you will be able to help me. I m placing the code within the message that I have written for a tutorial I am doing via correspondence. It is for an applet and I am sure you get what must happen. Basically as you press the button in the applet the background color must change . I have tried over and over to get the program to run , it compiles but will not run. I have only put in 2 if statements but there should be 5 one for each button.

Any help is greatly appreciated.

Many Many thanks to all you patient people.
Dave
Cape Town


/* Project: J5.48
* Programmer: Dave Morley
* Date: 29-01-05
* Program Name: TestApplet
*/

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

public class TestApplet extends Applet implements ActionListener
{
public void init()
{

// set color
setBackground(Color.orange);

//Create the layout
setLayout(new BorderLayout(5,20));

//Add buttons
add("North", new Button("red"));
add("South", new Button("yellow"));
add("East", new Button("cyan"));
add("West", new Button("magenta"));
add("Center", new Button("white"));
}

public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();

if(arg == "red")
{
setBackground(Color.red);
}

if(arg == "yellow")
{
setBackground(Color.yellow);
}


}

}


Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Have you tried

if(arg.equals("red")...
Dave Morley
Ranch Hand

Joined: Jul 23, 2004
Posts: 38
The only problem is that I have to do this in the manner with which they ask as I have to send a copy of the code to the tutor. The way I see it there isn't really anything wrong with the code, or is there???

In the description given to me it says to use the format tha I used,
surely you can use if(arg == "red") I have used it in a previous project and it worked but not here and I can't understand why!!!

Thanks Again
D
Dave Morley
Ranch Hand

Joined: Jul 23, 2004
Posts: 38
Another observation the Applet runs correctlt the only problem being when you press the relevant button, no code is carried out.

D
Damanjit Kaur
Ranch Hand

Joined: Oct 18, 2004
Posts: 346
add("North", new Button("red"));
add("South", new Button("yellow"));
add("East", new Button("cyan"));
add("West", new Button("magenta"));
add("Center", new Button("white"));


You need to add ActionListener to Button. So the code should be

Button r = new Button("red");
r.addActionListener(this);

or

add("North", new Button("red").addActionListener(this));
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

Originally posted by Dave Cape Town:
surely you can use if(arg == "red") I have used it in a previous project and it worked but not here and I can't understand why!!!


Dave, the advice in the previous post will probably solve your problem but I'd like to back Steven's advice here. Don't get into the habit of using the "==" operator to compare the values of two Strings; 99% of the time this will be the wrong way to do the comparison. Take Steven's advice and start using equals() instead.


Junilu - [How to Ask Questions] [How to Answer Questions]
Dave Morley
Ranch Hand

Joined: Jul 23, 2004
Posts: 38
thanks very much guys will try out all the advice and let you know.

D
Dave Morley
Ranch Hand

Joined: Jul 23, 2004
Posts: 38
I tried writing the following code

add("NORTH", newButton("red").addActionListener(this));

it won't compile at all. I am sure if i declared the button before the init method eg rButton and then in the intit method I added the button and then added the action listener it would work. But is must you declare the button first before the init method and then use the above code in the init method.

D
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Damanjit Kaur:
Button r = new Button("red");
r.addActionListener(this);

or

add("North", new Button("red").addActionListener(this));
I looked up addActionListener() in AbstractButton, and it has a void return type. No method chainnig for you!

Dave, you'll need to use the first method that uses a temporary variable.

Also, when I did Swing programming years ago, I just stored references to all event sources I cared about and used "e.getSource() == <button>". Is this frowned upon now? It's not like you're going to send the event over a socket to another JVM to see which event it is, so == should be fine for equality testing, right?
Dave Morley
Ranch Hand

Joined: Jul 23, 2004
Posts: 38
David thank you for your reply, I managed to get the program to run as you suggested, mant thanks.the code read something like this

Button rButton = new Button("red");

public void init.......

add(rButton, BorderLayout.SOUTH);
rButton.addActionListener(this);

as I understand what you are saying is that you need create a chain in order to specify which button carries out a seperate piece of code.

Is this right???

so it is not possible to write one line of code such as :
add("South", new Button(r Button)("OK"));
rButton.addActionListener(this);

the rButton is the argument of the component and that has to be specified.

Am i on the right track???

Many Thanks
D
Dean Fredericks
Ranch Hand

Joined: Dec 04, 2004
Posts: 60
Good to see im not the only person from Cape Town in this forum.

Ur going to need to call repaint(). You must geta reference to a Graphics object or something, and call repaint() on that. Call repaint, after setting the color.

Also, u are calling, setBackground. But the background might be transparent. You must call, setOpqaue(), or something like that to make sure its a solid color.

Also, can u even see the background? Your buttons might be sitting on top of the visible area, preventing u from seeing what is behing.

Also, trying to solve these problems with little bits of info and going to help. Get yourself a book covers applets and GUIs.

Also, this line looks strange to me, never seen it before : String arg = e.getActionCommand();
scap it and use this :
if(e.getSource() == red)
{
///change color and repaint
{

BTW , ur code where it adds a new Button, can never ever work, because u wont have a reference to add a actionlistener to, so how can u even think of listen to events.
delcare ur buttins properly, where the eniter class can see them like:
Button red = new Button(red). Delcare them in the class body - not in anymethod body.
then add the reference 'red' to the layout manger (norht south east west).
Now u will be able to listen for events.
Then add ur actionlistener.

Get the book "Java2 in easy steps". I learned to code things like this from that book

Cheers


PLEASE WATCH THIS VIDEO: <a href="http://www.glumbert.com/media/dolphin" target="_blank" rel="nofollow">http://www.glumbert.com/media/dolphin</a><br /> <br /><-- that video is no joke. Spread the word... this cant go on!!!<br /> <br />SCJP 1.4, SCBCD 1.3, SCWCD 1.4, SCMAD 1.0
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
What I meant by "method-chaining" is a pattern some classes use to shorten multiple calls on the same object instance by returning the same object from the method. StringBuffer makes use of this.It's just shorthand. If anything, it decreases performance (very very marginally), but you'd never notice.

If AbstractButton.addActionListener() had ended with "return this;" you could have used the one-line version above. Since it doesn't, you can't as you need the reference to 1) call addActionListener() and 2) call add(..., button).

Also, unless you need the reference outside of the init() method, you could declare the Button references locally inside init().But without seeing the rest of your program, I can't tell if you use the redButton reference elsewhere. As I said in my previous post, I use the references in actionPerformed().Again, I don't know if others have an aversion to this pattern, but I got it from Swing sample code (Sun's tutorial?) so it was common at one point. It's been . . . wow, five years since I've done any Swing work. Time sure does seem to go faster as you get older!
[ January 30, 2005: Message edited by: David Harkness ]
Dave Morley
Ranch Hand

Joined: Jul 23, 2004
Posts: 38
Thank you

One and All

D
Cape Town
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9043
    
  10
Dave,

Now that you have 16 posts, you've been around long enough to realize that "Cape Town" is not really an acceptable last name. Please visit this page to change your display name after you've read the JavaRanch naming policy.

Thanks.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Dave Morley
Ranch Hand

Joined: Jul 23, 2004
Posts: 38
Marilyn

It is duly done!

Thanks Everyone

D
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by DaveM:
It is duly done!
You're gonna get dinged again. Your new one doesn't satisfy the Naming Policy in two ways: no initials and space separating first and last name.

How about "Dave d'Cape"?
Dave Morley
Ranch Hand

Joined: Jul 23, 2004
Posts: 38
Sorry About that, it is now truly done properly.

Dave Morley
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Are you by any chance an X-Files fan?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pulling out hair
 
Similar Threads
see this very simple code.
for Applet
Applet not initialized
why do this canvas is covering the entire area of the applet?
Populating panel with button using loop!