Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7021247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:25:30+00:00 2026-05-27T23:25:30+00:00

I am making an mp3 player, with several JLists in my JFrame. When I

  • 0

I am making an mp3 player, with several JLists in my JFrame. When I right click on a JList item, a popup with some options for that song appears. But when this popup is visible, and I minimise my JFrame, this popup stays visible! Also, when the popup is visible, and I drag my JFrame to somewhere else on the screen, the popup stays on it’s original position (so it does not stay on the same position relative to the JFrame)… Can someone please help me out with this? I tried to strip down this class as much as possible 🙂

I would be really grateful if someone could help me out !!

Joe

public class playListPanel extends JPanel implements MouseListener {
    private DefaultListModel model;
    private Interface interFace;
    private JList list;
    private boolean emptyPlaylist;
    private ArrayList<Song> currentPlayList;
    private Song rightClickedSong;
    private JPopupMenu popup;
    private Point panelLocation;

    public playListPanel(Interface interFace) // Interface extends JFrame,
                                                // playListPanel is a part of
                                                // this JFrame.
    {
        this.interFace = interFace;
        this.panelLocation = new Point(559, 146);
        setBackground(SystemColor.controlHighlight);
        setBorder(new TitledBorder(null, "", TitledBorder.LEADING,
                TitledBorder.TOP, null, null));
        setBounds((int) panelLocation.getX(), (int) panelLocation.getY(), 698,
                368);
        setLayout(null);

        currentPlayList = new ArrayList<Song>();

        model = new DefaultListModel();
        list = new JList(model);
        list.setVisible(true);
        list.addMouseListener(this);

        JScrollPane scrollPane = new JScrollPane(list);
        scrollPane.setBounds(5, 5, 688, 357);
        add(scrollPane);

        emptyPlaylist = true;
    }

    private void openMenuPopup(Point point)
    {
        removePopup();
        popup = new JPopupMenu();
        int x = (int) point.getX();
        int y = (int) point.getY();
        popup.setLocation((int) (x+panelLocation.getX()),(int) (y+panelLocation.getY()));
        //popup.setLabel("popup voor playlist");
        JMenuItem removeSong;
        popup.add(removeSong = new JMenuItem("Remove Song from Playlist", new ImageIcon("image.jpg")));

        ActionListener menuListener = new ActionListener()
        {
              public void actionPerformed(ActionEvent event) 
              {
                if(event.getActionCommand().equals("Remove Song from Playlist"))
                {
                    System.out.println("Remove Song from Playlist");
                    interFace.getPlaylistManager().removeOneSong(rightClickedSong);
                    removePopup();
                }

        };

        //ADD THE LISTENERS TO THE MENU ITEMS
        removeSong.addActionListener(menuListener);
        popup.setVisible(true);
    }

    public void removePopup()
    {
        if(popup!==null)
        {
            popup.setVisible(false);
            System.out.println("popup removed");
        }
    }

    private int getRow(Point point) {
        return list.locationToIndex(point);
    }

    public void refreshPlayList(ArrayList<Song> playlist) {
        this.currentPlayList = playlist;

        model.clear();
        for (Song song : playlist) {
            model.add(model.getSize(), song.getPlaylistString());
        }
        list.setVisible(true);
    }

    public void highlightSong(int index) {
        list.setSelectedIndex(index);
    }

    public int getRowOfList(Point point) {
        return list.locationToIndex(point);
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        interFace.getPlaylistManager().doubleClickOnPlaylist(e);
    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
    }

    @Override
    public void mouseExited(MouseEvent arg0) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
        if (SwingUtilities.isRightMouseButton(e)) {
            rightClickedSong = currentPlayList.get(getRow(e.getPoint()));
            openMenuPopup(e.getPoint());
            System.out.println("should open popup at "
                    + e.getPoint().toString());
        }

    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:25:31+00:00Added an answer on May 27, 2026 at 11:25 pm

    There are some basic flaws in the way you are handling click for showing popup.
    It is not advisable to call popup.setVisible in simple scenarios like this. Instead, you may rely on its default behavior. Also, better to use e.isPopupTrigger() than to check SwingUtilities.isRightMouseButton(e) to show popup.

    You may do something like the following :

    //at classlevel,
    private JPopupMenu popup = new JPopupMenu();
    //create a Popuplistener
    PopupListener pl = new PopupListener();
    list.addMouseListener(pl);
    
    //Implementation of your popuplistener
      class PopupListener extends MouseAdapter {
        public void mousePressed(MouseEvent e) {
          maybeShowPopup(e);
        }
    
        public void mouseReleased(MouseEvent e) {
          maybeShowPopup(e);
        }
    
        private void maybeShowPopup(MouseEvent e) {
          if (e.isPopupTrigger())
            //e.getSource - and construct your popup as required.
            //and then.
            popup.show(((JApplet) e.getComponent()).getContentPane(), e
                .getX(), e.getY());
        }
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making my own crude MP3 player, and I now have a JList
I'm making a jQuery MP3 player. The song structure is first generated (including the
I am currently making an MP3 player in WPF, and I want to make
I am making a Music player for my web app. After user upload some
I am making an android application that will play an mp3 file once a
I'm making a little open source mp3 player for people to see the code,
I'm making some progress on taking a compressed (mp3) sound and saving it as
how do file converters work? IE: Making a web application that will convert mp3
I used the jPlayerscript to create a mp3-player for a website that I am
Making an adobe flex ui in which data that is calculated must use proprietary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.