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

  • SEARCH
  • Home
  • 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 9132973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:24:20+00:00 2026-06-17T08:24:20+00:00

I have a JFrame which looks like a typical chat box. To connect and

  • 0

I have a JFrame which looks like a typical chat box. To connect and disconnect from the server I implemented a JPopupMenu and added a MouseListener to the main window.
However the popup menu does not appear.

Various JComponents in the main window:

  • JTextArea within a JScrollPane
  • JTextField to enter the message
  • Send button
  • JScrollPane and an ArrayList to display the users
  • The popup is supposed to appear no matter where you right click. Be it on the JTextArea or the field to enter your message.
    To which all components do I add listeners and what listener do I add ?

    Code

    Variables:

        private static A_Chat_Client chatClient;
        public static String userName = "Anonymous";
    
    //------------------------------------------------------------------------------
        public static JFrame mainFrame = new JFrame();
        public static JTextArea textArea = new JTextArea(30,30);
        public static JScrollPane pane = new JScrollPane(textArea);
        public static JTextField message = new JTextField(10);
        public static JButton send = new JButton("Send");
    
        public static JPopupMenu popup = new JPopupMenu();
        public static JMenuItem connect = new JMenuItem("Connect");
        public static JMenuItem disconnect = new JMenuItem("Disconnect");
        public static JMenuItem help = new JMenuItem("Help");
    
        public static JList usersOnline = new JList();
        public static JScrollPane userPane = new JScrollPane(usersOnline);  
    

    Main method

    public static void main(String[] args) {
            buildMainWindow();
            initialize();
            addListeners();
            popup.show(mainFrame, 0, 0); //forcefully popup
        }  
    

    addListeners()

    public static void addListeners(){
    
            mainFrame.addMouseListener(new MouseHandler());
    
            send.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e){
                    actionSend();
                }
            });
            connect.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e){
                    actionConnect();
                }
            });
            disconnect.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e){
                    actionDisconnect();
                }
            });
            help.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e){
                    actionHelp();
                }
            });
        }  
    

    class MouseHandler

    private static class MouseHandler extends MouseAdapter{
            @Override
            public void mouseClicked(MouseEvent e){
                if(e.isPopupTrigger()){
                    popup.show(mainFrame, e.getX(), e.getY());
                }
            }
        }
    
    • 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-06-17T08:24:20+00:00Added an answer on June 17, 2026 at 8:24 am

      You could attach an AWTEventListener to the main event queue using Toolkit#addAWTEventListener

      You would then need to check the type of event your are receiving and check to see if it’s popup event.

      enter image description here

      public class TestGlobalPopup {
      
          public static void main(String[] args) {
              new TestGlobalPopup();
          }
      
          public TestGlobalPopup() {
              EventQueue.invokeLater(new Runnable() {
                  @Override
                  public void run() {
                      try {
                          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                      } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                      }
      
                      JFrame frame = new JFrame();
                      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                      frame.setLayout(new BorderLayout());
                      frame.add(new TestPane());
                      frame.pack();
                      frame.setLocationRelativeTo(null);
                      frame.setVisible(true);
                  }
              });
          }
      
          public class TestPane extends JPanel {
      
              public TestPane() {
                  Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {
                      @Override
                      public void eventDispatched(AWTEvent event) {
                          if (event instanceof MouseEvent) {
                              MouseEvent me = (MouseEvent) event;
                              if (me.isPopupTrigger()) {
                                  Component component = me.getComponent();
                                  JPopupMenu popup = new JPopupMenu();
                                  popup.add(new JLabel("Clicked on " + component.getClass().getName()));
                                  popup.show(component, me.getX(), me.getY());
                              }
                          }
                      }
                  }, AWTEvent.MOUSE_EVENT_MASK);
      
                  setLayout(new GridBagLayout());
                  GridBagConstraints gbc = new GridBagConstraints();
                  gbc.gridx = 0;
                  gbc.gridy = 0;
                  add(new JLabel("Label: "), gbc);
                  gbc.gridx++;
                  add(new JTextField(10), gbc);
      
                  gbc.gridy++;
                  gbc.gridx = 0;
                  gbc.gridwidth = 2;
                  add(new JScrollPane(new JTextArea(5, 20)), gbc);
              }
      
          }
      
      }
      
      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
        • Report

    Sidebar

    Related Questions

    I have created jframe in which jpanels are added dynamically what i can't do
    I have a JFrame(frm) in which I've added a JPanel (pnl1)..which in turn has
    I have a MyFrame class which extends JFrame . I added components(Controls i.e buttons)
    I have a JFrame on which I want to simulate a countdown (like a
    I have a JFrame . I also have a Box class which extends Component
    I have a JFrame which on instantiation calls a custom JDialog(Login). If login is
    I have a JFrame which consists only of a JList , the JList has
    i'm developing a java application in which i have a JFrame with a JDesktopPane
    I have written a java applet which opens a JFrame (so when run in
    I'm creating an about JFrame for my program. I have an icon which I

    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.