De Huaste

Greenhorn
+ Follow
since Nov 18, 2012
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
8
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by De Huaste

Campbell Ritchie wrote:

De Huaste wrote: . . . I'll research and commit to memory those which are "core" and those that are swing only. . . .

Don’t do that. There are something like 4500 classes in the standard Oracle/Sun Java API, so unless you expect to learn them all off by heart, that will be a useless task. What you need to know is how to find those classes in the API. Look here, and you will see it is says

java.awt.event
Interface ActionListener

So now you know to import it in the package java.awt.event. You ought to bookmark the index to the API documentation because you will use it all the time.

If you import something you don’t use, 99% of the time this is what will happen: Nothing.
You can occasionally have problems. Try importing java.util.* and javax.swing.* and using the Timer class
When you have worked out what went wrong, you will find out why many people prefer to import individual classes rather than on‑demand imports. More about imports here.

I would have known you had the wrong import, too, had you said it was a compiler error. But you didn’t, so I looked at your code wondering what the logic error was and couldn’t find it.



As soon as I posted, I knew it was an overstatement ;)

Thanks for all the advice, I have bookmarked the API reference. Apologies for not being more specific with my error, I appreciate you taking the time to look at the code. This is an outstanding board!
11 years ago

Rob Spoor wrote:I didn't say you had to remove the javax.swing.event import. I must admit it's a bit confusing, with some event listeners being in java.awt.event and others in javax.swing.event. The distinction is that those in java.awt.event are the "core" event listeners that are also used by AWT, and those in javax.swing.event are for Swing only.



Thanks for the explanation, it now compiles and behaves as expected

I'll research and commit to memory those which are "core" and those that are swing only. For now is there any harm including them both in my default import when building GUIs in Java?
11 years ago

Rob Spoor wrote:ActionListener and ActionEvent are in package java.awt.event, not javax.swing.event.



Thanks Rob, I think I changed this based on a tutorial somewhere. When I import this package, it then sends my Changelistener into a frenzy.

SoundLevels.java:105: cannot find symbol
symbol : class ChangeListener
location: class SoundLevels
private class SliderListener implements ChangeListener
^
SoundLevels.java:107: cannot find symbol
symbol : class ChangeEvent
location: class SoundLevels.SliderListener
public void stateChanged(ChangeEvent e)
^
SoundLevels.java:51: addChangeListener(javax.swing.event.ChangeListener) in javax.swing.JSlider cannot be applied to (SoundLevels.SliderListener)
slideLeft.addChangeListener(new SliderListener());
^
SoundLevels.java:56: addChangeListener(javax.swing.event.ChangeListener) in javax.swing.JSlider cannot be applied to (SoundLevels.SliderListener)
slideRight.addChangeListener(new SliderListener());
^
4 errors

----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.
11 years ago

Campbell Ritchie wrote:And what goes wrong when you try it?



I get these ominous errors...

SoundLevels.java:119: cannot find symbol
symbol : class ActionListener
location: class SoundLevels
private class ResetButtonListener implements ActionListener
^
SoundLevels.java:121: cannot find symbol
symbol : class ActionEvent
location: class SoundLevels.ResetButtonListener
public void actionPerformed(ActionEvent e)
^
SoundLevels.java:84: addActionListener(java.awt.event.ActionListener) in javax.swing.AbstractButton cannot be applied to (SoundLevels.ResetButtonListener)
reset.addActionListener(new ResetButtonListener());
^
3 errors

----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.
11 years ago
Hey Ranchers,

I'm having an issue identifying the problem with this ActionListener. The event is to fire when the reset button is clicked and reset the value of my sliders to zero. Thank you in advance for pointing out my novice errors.

11 years ago
Michael. thanks for going over this. Needless to say, your advice worked. Maneesh, thanks for moving to the appropriate forum
11 years ago
Hey Gang,

This is my first GUI attempt, unless you count JOptionPane. It's a little assignment for class that compiles fine, but there's something going on with the logic. I need to set the background color of the buttons (to red, orange, yellow respectively) in the button panel and create an action event that changes the messageLabel's text color when that of the corresponding button (green, blue, cyan) is clicked.

To the best of my knowledge, it's all there but the program acts otherwise. Help is appreciated


11 years ago
Thank you all! Campbell and Carey, points noted. Jeff, this makes perfect sense. Moving on to bigger and more interesting problems ;)
11 years ago

Jeff Verdegan wrote:

De Huaste wrote:

Aj Prieto wrote:Instead of


Try


Also, please use code tags when posting code



Thank you very much. And will do



Much more important than solving this particular issue is the question: Do you understand the difference, and why one does what you want but not the other?



I agree and to be honest, I need to re-read my assignment operators. But if you're offering wisdom, I'll certainly listen.
11 years ago

Aj Prieto wrote:Instead of


Try


Also, please use code tags when posting code



Thank you very much. And will do
11 years ago
Hello CR! First timer here. Looking for help getting my code to accumulate a total rather than just getting the last number.

This is a snippet of the main method...

public class PickingBerries
{
public static void main(String [] args)
{
System.out.println("Out in the forest, some friends were picking blackberries...");

Bucket alan = new Bucket(); // Alan has an empty bucket
Bucket lisa = new Bucket(); // Lisa has an empty bucket
Bucket ian = new Bucket();

System.out.println("Alan...");

alan.report(); // Prints to console: "This bucket has 0 berries."
alan.pick(10); // 10 in bucket now
alan.pick(2); // now up to 12
alan.report(); // Prints to console: "This bucket has 12 berries."

This is a snippet of the second class with the pick method. As it stands, when I run the program I'm only getting the last number picked (2) instead of the total (12).

class Bucket
{
//keeps tracks of the number of blackberries
//that a person has picked and placed in their bucket

private static int bucket;
private boolean status = true;

public void pick(int berries)
{
bucket =+ berries;
}

Help is appreciated
11 years ago