-1
if("switch".equals(action))
{
    changeLunar();
    count++;
    if (count % 2 == 0)
    {
        lunarButton = new JButton(new ImageIcon("assets/sun.png"));
    }
    else
    {
        lunarButton = new JButton(new ImageIcon("assets/moon.png"));
    }
}

So I am trying to make it where when you click a button, the button changes to another image, and when you click it again it goes back to the first image. But when I compile it says:

lunarButton can't be resolved to a variable

What am I to do?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jakkie Chan
  • 317
  • 4
  • 14
  • 1
    An actual [runnable example that demonstrates your problem](https://stackoverflow.com/help/mcve) would involve less guess work and better responses. It sounds like you defined the original `lunarButton` as a local variable, but I'm just guessing – MadProgrammer May 15 '14 at 05:03
  • agree with @MadProgrammer...and you may need to declare a type for lunarButton (`JButton lunarButton = ...`) – sfletche May 15 '14 at 05:04
  • @sfletche "assuming" that a `lunarButton` already exists, for something to be pressed, the OP should be able to use `JButton#setIcon` to change the icons...but I'm still only guessing :P – MadProgrammer May 15 '14 at 05:07
  • About "in actionPerformed", is this code fragment the "actionPerformed"? – blueygh2 May 15 '14 at 05:14
  • yes blue it is. an di have the JButton lunarButton declared in init() – Jakkie Chan May 15 '14 at 05:15
  • @user3427042 then, *assuming* you already have a `lunarButton`, why do you create a new button instead of changing the icon of the existing button? – blueygh2 May 15 '14 at 05:19
  • Use a `JToggleButton` and set the default and selected icons for it. Job done. BTW - By the time of deployment, those resources will likely become an [tag:embedded-resource]. That being the case, the resource must be accessed by `URL` instead of `File`. See the [info page](http://stackoverflow.com/tags/embedded-resource/info) for the tag, for a way to form an `URL`. – Andrew Thompson May 15 '14 at 05:20
  • `JToggleBtton` .. E.G. as seen in [this answer](http://stackoverflow.com/q/7359906/418556) (which is an MCVE, as you were advised to post by @MadProgrammer over 30 minutes ago). – Andrew Thompson May 15 '14 at 05:36
  • 1
    @AndrewThompson I like the idea of the `JToggleButton`, but more context would be wonderful – MadProgrammer May 15 '14 at 05:39
  • 1
    @MadProgrammer Yes, the suggestion '`JToggleButton`' hardly speaks to the source of the compiler error. I decided to vote to close for lack of a reproducible example. – Andrew Thompson May 15 '14 at 05:43
  • @user3427042 Please share the code. – Braj May 15 '14 at 06:16

1 Answers1

0

Error Message: Cannot Find Symbol

When a Java program is being compiled the compiler creates a list of all the identifiers in use. If it can't find what an identifier refers to (e.g., there is no declaration statement for a variable) it cannot complete the compilation. This is what the cannot find symbol error message is saying, it doesn't have enough information to piece together what the Java code wants to execute.

Please validate the variable lunarButton declared in your code and it must be accessible as well.

May be you have created it in changeLunar() method as local variable that can't be accessed outside the method instead use instance variable or just return the value from the changeLunar() method.

Instead of creating a new JButton every time you can simply change its icon using JButton#setIcon() method.

Braj
  • 46,415
  • 5
  • 60
  • 76