Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2005

Tip: Looking for answers? Try searching our database.

Having trouble with JpopupMenu highliting

Thread view: 
bl8n8r - 15 Mar 2005 13:42 GMT
Hello All.

I'm trying to get a JpopupMenu to work here [1].  The menu comes up
easily enough, however the only time the menu item is hilighted, is for
a brief moment when the mouse button is released.

I was kinda hoping JpopupMenu would magically hilite each item as the
mouse hovered over it, but perhaps I need to implement that part
myself?

Thanks,
bl8n8r

[1] ------------------- sample code -------------------------
public class test_menu_2 extends javax.swing.JFrame
{

  /** Creates new form test_menu_2 */
  public test_menu_2 ()
  {
     initComponents ();
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  private void initComponents()//GEN-BEGIN:initComponents
  {
     jPopupMenu1 = new javax.swing.JPopupMenu();
     jMenuItem1 = new javax.swing.JMenuItem();
     jMenuItem2 = new javax.swing.JMenuItem();
     jMenuItem3 = new javax.swing.JMenuItem();
     jRadioButtonMenuItem1 = new javax.swing.JRadioButtonMenuItem();

     jMenuItem1.setText("Item");
     jPopupMenu1.add(jMenuItem1);

     jMenuItem2.setText("Item");
     jPopupMenu1.add(jMenuItem2);

     jMenuItem3.setText("Item");
     jPopupMenu1.add(jMenuItem3);

     jRadioButtonMenuItem1.setText("RadioButton");
     jPopupMenu1.add(jRadioButtonMenuItem1);

     addMouseListener(new java.awt.event.MouseAdapter()
     {
        public void mouseClicked(java.awt.event.MouseEvent evt)
        {
           formMouseClicked(evt);
        }
     });
     addWindowListener(new java.awt.event.WindowAdapter()
     {
        public void windowClosing(java.awt.event.WindowEvent evt)
        {
           exitForm(evt);
        }
     });

     pack();
  }//GEN-END:initComponents

  private void formMouseClicked (java.awt.event.MouseEvent
evt)//GEN-FIRST:event_formMouseClicked
  {//GEN-HEADEREND:event_formMouseClicked
     // Add your handling code here:
     System.out.println ("form clicked");
     java.awt.Point pt = new java.awt.Point();
     java.awt.Point spt = new java.awt.Point();

     // get x and y loc of parent component
     spt.x = evt.getComponent ().getX();
     spt.y = evt.getComponent ().getY();
     pt    = evt.getPoint ();

     jPopupMenu1.setLocation (pt.x + spt.x, pt.y + spt.y);
     jPopupMenu1.setVisible (true);
  }//GEN-LAST:event_formMouseClicked

  /** Exit the Application */
  private void exitForm (java.awt.event.WindowEvent
evt)//GEN-FIRST:event_exitForm
  {
     System.exit (0);
  }//GEN-LAST:event_exitForm

  /**
   * @param args the command line arguments
   */
  public static void main (String args[])
  {
     new test_menu_2 ().show ();
  }

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JMenuItem jMenuItem1;
  private javax.swing.JMenuItem jMenuItem2;
  private javax.swing.JMenuItem jMenuItem3;
  private javax.swing.JPopupMenu jPopupMenu1;
  private javax.swing.JRadioButtonMenuItem jRadioButtonMenuItem1;
  // End of variables declaration//GEN-END:variables
 
}
bl8n8r - 16 Mar 2005 13:45 GMT
In case anyone else needs it, the solution is (below).

cheers,
bl8n8r

---------------------------------------------------------------
public class test_menu_2 extends javax.swing.JFrame
{
  boolean MenuVisible = false;
  Color MenuBgNormal;
  Color MenuBgHilite = Color.YELLOW;

...

  // hilite/unhilite passed component
  public void ToggleHilite (Component c, boolean state)
  {
     if (state == true)
     {
        c.setBackground (MenuBgHilite);
     }
     else
     {
        c.setBackground (MenuBgNormal);
     }
  }

...

  private void jMenuItem1MouseExited (java.awt.event.MouseEvent evt)
  {
     // Add your handling code here:
     ToggleHilite (evt.getComponent (), false);
  }

  private void jMenuItem1MouseEntered (java.awt.event.MouseEvent evt)
  {
     // Add your handling code here:
     ToggleHilite (evt.getComponent (), true);
  }

...

  private void formMouseClicked (java.awt.event.MouseEvent evt)
  {
     // Add your handling code here:
     System.out.println ("form clicked");

     // if MenuVisible then hide it
     if (MenuVisible == false)
     {
        java.awt.Point pt = new java.awt.Point();
        java.awt.Point spt = new java.awt.Point();

        // get x and y loc of parent component
        spt.x = evt.getComponent ().getX();
        spt.y = evt.getComponent ().getY();
        pt    = evt.getPoint ();

        jPopupMenu1.setLocation (pt.x + spt.x, pt.y + spt.y);
        jPopupMenu1.setVisible (true);
        jPopupMenu1.requestFocus ();

        // save bgcolor for menu hiliting
        MenuBgNormal = jPopupMenu1.getBackground ();

        MenuVisible = true;
     }
     else
     {
        jPopupMenu1.setVisible (false);
        MenuVisible = false;
     }
  }


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.