Contextual help for each jComponent with question mark cursor

Most Windows users may remember that in every Properties / Preferences window in Window 98, there was a small question button next to the other window buttons:

image description

If you clicked this button, all click events were overridden with a different callback for this window. And this new callback will display the separate help text.

I would like to do the same. My idea was to do this using a class that contains all the associations JComponent

and Help

:

public interface Help {
  /** based on implementation, displays help to the used. May use
   *  JDialog, url redirection or maybe open document on the computer.**/
  public void getHelp(JComponent comp, ActionEvent evt);
}

public class HelpLibrary {
  public HashMap<JComponent, Help> helpLib;
  public void getHelp(JComponent comp, ActionEvent evt) {
    Help help = helpLib.get(comp);
    if(help!=null) {
       help.getHelp(comp, evt);
    }
  }
}

      

Writing these two classes was the easy part. It's hard:

  • How can I override all click events in a specific window and then remove the override after calling help?
  • How to ensure that the help cursor overlaps all other cursors, and again, safely remove this setting?

I don't know where to start. I really don't want to change the GUI structure or the classes used exactly because of this, so I want to keep the help and make overrides from the outside.

public class HelpLibrary {
  /** 
   * Overrides click events on the given window and displays help cursor.
   * User then may click a JComponent, such as button, to initiate
   * help callback for that element. If no help exists for that element,
   * do nothing and stop the help mode.
   * @param window the window to get help for
  **/
  public void waitForHelp(JFrame window) {
     ???
  }
}

      

+3


source to share


2 answers


You can try this:

  • Register a global MouseListener using Toolkit.getDefaultToolkit().addAWTEventListener(myListener, AWTEvent.MOUSE_EVENT_MASK)

  • Passing an Inbound Event to MouseEvent and Checking the Event Type Using the getID () Method
  • If the event is a click for a component that has help, you need to show the help, use the event, and remove that listener from the global listener list.
  • You can also override the mouseEnter / Exit event in this listener for components that have help text and set the cursor to the question / normal type (remember to use this event).


This is just an idea for you, I have not tested if it works.

+1


source


You can use GlassPane

.

Read the section from the Swing tutorial on How to use root panels . The Glass Pane demo shows you how to intercept mouse events and forward events to underlying components. YoOu will obviously modify this code to find the component under click and then display the help context.



The glass panel can be turned on / off by making it visible or not.

0


source







All Articles