Link to home
Start Free TrialLog in
Avatar of Drop_of_Rain
Drop_of_Rain

asked on

Changing action command by changing text in the button

I know part of this is possible and only seems reasonable since the text of the button is what is used to decide if an action command happened. But i am new to this and want to get your expert advice.

Is it possible to have button change using set.text then using an action command checking the text and have a different action command work? Can someone show me some examples?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Please try to tell us more directly what your requirement is. Along the lines of 'i want to change the text on button A to "blah" and have an ActionListener ...'
Avatar of Drop_of_Rain
Drop_of_Rain

ASKER

'Iwant to change the text on fastButton  change from "Fast" to "Reset" and have an action Listener and use the ActionCommand that gets the text and then compares  the text before taking action. This way i can use the same buttons to do more things in the GUI.
Wouldn't it be better to have it go from something like Fast to Normal? Fast what btw?
Sorry I made a mistake in my example: Fast is for the speed of setting the clock of timer, The record and Play Buttons are what could be changed for the timer mode.
Sorry - i don't quite understand
What I need to know is that this can be done, can it?

This is the class for it, you will be able to see what is being done here so far:


import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JLabel;
import my.test.GuiPanel.Mode;


class AdvanceModeAction extends AbstractAction
{
    private final GuiPanel guiPanel;

    public AdvanceModeAction(GuiPanel guiPanel, String name)
    {
        super(name);
        this.guiPanel = guiPanel;
    }

    public void actionPerformed(ActionEvent e)
    {
        this.guiPanel.advanceMode();
        this.guiPanel.modeDisplay.setText(this.guiPanel.currentMode.toString());

        this.guiPanel.enableAllButtons();

        if (this.guiPanel.currentMode == Mode.Alarm)
        {
            this.guiPanel.recordButton.setEnabled(false);
            this.guiPanel.playButton.setEnabled(false);
            this.guiPanel.stopButton.setEnabled(false);
            this.guiPanel.fastButton.setEnabled(false);

            this.guiPanel.alarmTempDisplay = new JLabel(this.guiPanel.timeDisplay.getText());
            this.guiPanel.alarmStarted = false;
            this.guiPanel.dispProv.setDispListener(this.guiPanel.alarmTempDisplay);
        }
        else            if (this.guiPanel.currentMode == Mode.Audio)
            {
                this.guiPanel.hourButton.setEnabled(false);
                this.guiPanel.minutesButton.setEnabled(false);

            }
            else                if (this.guiPanel.currentMode == Mode.Clock)
                {
                    this.guiPanel.recordButton.setEnabled(false);
                    this.guiPanel.playButton.setEnabled(false);
                    this.guiPanel.stopButton.setEnabled(false);
                    this.guiPanel.dispProv.setDispListener(this.guiPanel.timeDisplay);
                }
                else                    if (this.guiPanel.currentMode == Mode.Date)
                    {
                        this.guiPanel.recordButton.setEnabled(false);
                        this.guiPanel.playButton.setEnabled(false);
                        this.guiPanel.stopButton.setEnabled(false);
                    }
                    else                        if (this.guiPanel.currentMode == Mode.Timer)
                        {
                            this.guiPanel.recordButton.setEnabled(false);
                            this.guiPanel.playButton.setEnabled(false);
                            this.guiPanel.stopButton.setEnabled(false);
                           
                        }


    }
}
I don't see any play mode there. btw, your code will be much cleaner if you use a switch statement rather than chaining ifs
There is an audio mode to handle the recording and the playing as well as storing the files.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Now that's tidied up can you explain what you want to do again?
Yes that is much better. I will be able to take care of this part. I just needed to know it could be done.  Other chages will have to happen later whrn the classes are done. You could give me an example of how to call up a class in one of these cases like the audio.class in audio  then I can go from there.

                       recordButton.setEnabled(false);
        stButton.setText("Reset");
        fastButton.setEnabled(true);  // changes like these
        fastButton.setText("Reset");  // changes like these
                        playButton.setEnabled(false);
                        stopButton.setEnabled(false);
I will need to pass the GUI into the audio class, will I need to pass this one as well?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Could you explain that a little better. where i am lost iwith the audio.doDefault();

I need to keep them all as different classes right?
I really like being able to do this, it makes the gui unit much smaller and still have many functions, it was a good idea. Thanks for all the help! You have been a great sport working with me. Sorry i am so new at this. I have been told i am a natural, I get the concepts part really well, it is the writting the code and understand what does what that will take the time for me.
You know you have supplied answers for questions I have asked without even knowing it. Can you look at my questions like this one.
https://www.experts-exchange.com/questions/22689088/Mouse-location-over-imagine.html  you helped on that on as well.
>>
Could you explain that a little better. where i am lost iwith the audio.doDefault();
>>

That's just an example of invoking a method on an imaginary 'Audio' class that will do something

>>
I need to keep them all as different classes right?
>>

Generally yes
>>Can you look at my questions like this one.

Be careful - i think you're beginning to lose track of your questions ;-) Don't try to multitask too much, you're not a bit of advanced Intel silicon

You posted a new question on that one. I made a remark in it, adn you were going to think about it a bit more before reposting
It was the audio. the dot in the middle that had me confussed. Is that a way of calling a method out of the class?
Yes. 'audio' is a reference to a class of type Audio and doDefault one of its methods
Thanks so much for your support and help. I have a new question up also. Something up your line
:-)