Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Java Swing - Right Click and JPopupMenu in JTable

Java Swing - Right Click and JPopupMenu in JTable

Scheduled Pinned Locked Moved Java
helpquestionjava
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    djDash
    wrote on last edited by
    #1

    I want a different popup on each row of JTable. So I thought of setting the row on right click and then add popmenu to JTable. The following code will help you'll to understand the problem...

    private synchronized void HandlePopup(java.awt.event.MouseEvent evt) {
    jTable1.setComponentPopupMenu(null);
    if(evt.getButton()==MouseEvent.BUTTON3){
    int y = evt.getY();
    int row = Math.round(y / jTable1.getRowHeight());
    jTable1.setRowSelectionInterval(row, row);
    System.out.println("Row Selected = " + row);
    jTable1.setComponentPopupMenu(popupMenu);
    }
    else jTable1.setComponentPopupMenu(null);
    }

    private void CreatePopup() {
        // Create popup menu, attach popup menu listener
        popupMenu = new JPopupMenu("Title");
    
        // Cut
        JMenuItem cutMenuItem = new JMenuItem("Cut");
        popupMenu.add(cutMenuItem);
    
        // Copy
        JMenuItem copyMenuItem = new JMenuItem("Copy");
        popupMenu.add(copyMenuItem);
        // Paste
        JMenuItem pasteMenuItem = new JMenuItem("Paste");
        pasteMenuItem.setEnabled(false);
        popupMenu.add(pasteMenuItem);
    
        // Separator
        popupMenu.addSeparator();
    
        // Find
        JMenuItem findMenuItem = new JMenuItem("Find");
        popupMenu.add(findMenuItem);        
    }
    

    :) Here for the first right click the row gets selected.... then only the popup works and then there is no row selection. the pop is always the same...coz new row gets never selected... So how can I handle both popup with right selection...??? Please help me with this...

    J 1 Reply Last reply
    0
    • D djDash

      I want a different popup on each row of JTable. So I thought of setting the row on right click and then add popmenu to JTable. The following code will help you'll to understand the problem...

      private synchronized void HandlePopup(java.awt.event.MouseEvent evt) {
      jTable1.setComponentPopupMenu(null);
      if(evt.getButton()==MouseEvent.BUTTON3){
      int y = evt.getY();
      int row = Math.round(y / jTable1.getRowHeight());
      jTable1.setRowSelectionInterval(row, row);
      System.out.println("Row Selected = " + row);
      jTable1.setComponentPopupMenu(popupMenu);
      }
      else jTable1.setComponentPopupMenu(null);
      }

      private void CreatePopup() {
          // Create popup menu, attach popup menu listener
          popupMenu = new JPopupMenu("Title");
      
          // Cut
          JMenuItem cutMenuItem = new JMenuItem("Cut");
          popupMenu.add(cutMenuItem);
      
          // Copy
          JMenuItem copyMenuItem = new JMenuItem("Copy");
          popupMenu.add(copyMenuItem);
          // Paste
          JMenuItem pasteMenuItem = new JMenuItem("Paste");
          pasteMenuItem.setEnabled(false);
          popupMenu.add(pasteMenuItem);
      
          // Separator
          popupMenu.addSeparator();
      
          // Find
          JMenuItem findMenuItem = new JMenuItem("Find");
          popupMenu.add(findMenuItem);        
      }
      

      :) Here for the first right click the row gets selected.... then only the popup works and then there is no row selection. the pop is always the same...coz new row gets never selected... So how can I handle both popup with right selection...??? Please help me with this...

      J Offline
      J Offline
      Java John
      wrote on last edited by
      #2

      Where are you determining if the mouse event is a right click or not? Also - where are you attaching your pop up menu listener? Your question is pretty poorly explained: it makes it harder to help you.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups