0

I am trying to implement a play/pause button in swing. I initiate the JLabel like this:

PlayLabel = new JLabel(new ImageIcon(playPNG));
PlayLabel.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent arg0) {
                PlayLabel.setIcon(new ImageIcon(pausePNG));
            }
        });

so this works fine. After clicking on PlayLabel the imageSource gets changed. But in my pause-Method this won't work:

private void doPause()
    {
        System.out.println("PAUSED");
        player.pauseCurrentTrack();
        PlayLabel.setIcon(new ImageIcon(playPNG));
    }

this JLabel won't turn its ImageIcon back to playPNG

QKL
  • 259
  • 1
  • 3
  • 11
  • 3
    something wrong with the code you are not showing :-) Consider posting an SSCCE for better help sooner – kleopatra Nov 11 '12 at 14:02
  • 1
    `PlayLabel` should probably be a `JButton`. It has methods for setting alternate icons. 2) Please learn common [Java naming conventions](http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#73307) (specifically the case used for the names) for class, method & attribute names & use it consistently. 3) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Nov 11 '12 at 14:02
  • @kleopatra Did you forget to mention the nomenclature? ;) – Andrew Thompson Nov 11 '12 at 14:03

2 Answers2

2

JToggleButton may be more suitable in this context. The setIcon() method should work as you propose. For reference, this example updates each button in a List<JToggleButton> from a continually changing List<Icon>. It may offer some insight or form a basis of you sscce.

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

The following example shows you what you want to do, but with text rather than icon. The program toggles the image, text or whatever you display.

If the reason you used a JLabel is because you wanted a flat button, you can add the code:

    private void makeButtonFlat(final JButton makeMeAFlatButton) {
        makeMeAFlatButton.setOpaque(false);
        makeMeAFlatButton.setFocusPainted(false);
        makeMeAFlatButton.setBorderPainted(false);
        makeMeAFlatButton.setContentAreaFilled(false);
        makeMeAFlatButton.setBorder(BorderFactory.createEmptyBorder());
    }

and call this from the

    public void makeLayout() {
        makeButtonFlat(playButton);
        add(playButton);
    }

It will then look like a JLabel, but act as a button, which is more logical.

Running program

Code snippet:

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Playful {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Playful");
                JPanel playPanel = new PlayPanel();
                frame.getContentPane().add(playPanel, BorderLayout.CENTER);
                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                frame.setMinimumSize(new Dimension(400, 250));
                frame.setLocationRelativeTo(null); // Center
                frame.pack();
                frame.setVisible(true);
            }
        });
    }

    static class PlayPanel extends JPanel {

        enum State {
            PLAY,
            PAUSE;

            public State toggle() {
                switch (this) {
                    case PLAY:
                        return PAUSE;
                    case PAUSE:
                        return PLAY;
                    default:
                        throw new IllegalStateException("Unknown state " + this);
                }
            }
        }

        private State state;
        private JButton playButton;
        private ActionListener playActionListener;

        PlayPanel() {
            createComponents();
            createHandlers();
            registerHandlers();
            makeLayout();
            initComponent();
        }

        public void createComponents() {
            playButton = new JButton();
        }

        public void createHandlers() {
            playActionListener = new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    SwingUtilities.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            toggleState();
                        }
                    });
                }
            };
        }

        public void registerHandlers() {
            playButton.addActionListener(playActionListener);
        }

        public void makeLayout() {
            add(playButton);
        }

        public void initComponent() {
            state = State.PAUSE;
            updatePlayButton();
        }

        private void toggleState() {
            state = state.toggle();
            updatePlayButton();
        }

        private void updatePlayButton() {
            playButton.setText(state.name());
        }
    }
}
Skjalg
  • 763
  • 5
  • 13