aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Can javahelp be invoked on JPanels? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Can javahelp be invoked on JPanels?" Watch "Can javahelp be invoked on JPanels?" New topic
Author

Can javahelp be invoked on JPanels?

Jon Swanson
Ranch Hand

Joined: Oct 10, 2011
Posts: 200
I have an application consisting of five components. Each one extends JPanel. If I want to press F1 in my application and bring up help, I can do the following:



where parent is the top level of the application (extends JFrame)

What I would like is if JPanel 1 has focus, pressing F1 would bring up JavaHelp at the TOC entry for panel 1. Likewise, F1 would bring up the appropriate help entry if pressed in the other four panels.

I've tried to go through the Oracle documentation, but it makes no sense to me. I tried this:



where parent.getPanel1() returns the instance of my Panel1 extended JPanel class. I get an error: java.lang.IllegalArgumentException: Invalid Component

I get no error when I use:



from within the class (also extends JPanel) that these commands are included in. But F1 does nothing, so not much of an improvement.

Has anyone tried this, and if so, could you give me some pointers as to how it is supposed to work?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
KeyBindings with the correct inputmap should do what you want
Jon Swanson
Ranch Hand

Joined: Oct 10, 2011
Posts: 200
I read this from a java book

"abstract interface javax.help.HelpBroker

When F1 is pressed while that component, or one of its children, has the focus, the JavaHelp viewer will appear displaying the appropriate help topic. If a child component has the focus and has an associated help ID assigned, the help viewer will display the help topic specific to that component. Otherwise the default topic (specified as "MyTopicID" above) will be displayed."

So my understanding was that I had to do three things:

1. create a help broker

2. assign a help ID to a child component

3. enable the help key on the root pane



But I must be wrong about that, because for step 2, I get-

java.lang.IllegalArgumentException: Invalid Component

enableHelp:

enableHelp(java.awt.Component comp, java.lang.String id, HelpSet hs)
Enables help for a component.

JPanel:

java.lang.Object
java.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.JPanel

so I am not getting why it is not working on my instance of JPanel. I assume I either have the wrong method for assigning a help id to a component or I am not specifying the component correctly, but I'm at a loss to know which. I've not found a example, just snippets that usually left out step 2.

I also tried-

hb.enableHelpKey(myComponent, "componentTopic", hs);

but I get the same error.

and I tried:

CSH.setHelpIDString(myComponent,"componentTopic");

but I get the same error. I think that method is supposed to be used for buttons and menu items.

I've not found any discussion on assigning a help ID that actually includes what method to use and the discussions usually just repeat the same unhelpful recipe almost verbatim from one to the next.

So could someone with some javahelp expertise set me straight?

Jon Swanson
Ranch Hand

Joined: Oct 10, 2011
Posts: 200
This seems to work OK, though I had to add a mouse listener to give the panel focus when I clicked in it, other than in the text fields. I didn't include the help set, but "top", "left" and "right" are defined in the help.jhm file of the helpset.

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4548
    
    5

Jon Swanson wrote:I had to add a mouse listener to give the panel focus when I clicked in it

Just guessing here, but possibly all you needed was setFocusable(true). A JPanel isn't focusable by default.


luck, db
There are no new questions, but there may be new answers.
Jon Swanson
Ranch Hand

Joined: Oct 10, 2011
Posts: 200
I gave that a try. I replaced:


with


same for the other two panels. I could not click in the panel and get focus on the panel and so the wrong help comes up. I actually seem to need to request focus.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can javahelp be invoked on JPanels?