<index> Tutorial about the help system. </index>
<article desc="button 1"> Article on first button. </article>
<article desc="button 2"> Article on second button. </article>
<article desc="several components"> <title title="third button" /> The third button. <title title="text field" /> The text field. <title title="panel" /> The panel. </article>
articles.zip
at the same level as the output directory. This file contains the Help content generated with the content of our wiki[1]
articles resources -- articles.xml -- index.html -- README.txtThe
articles.xml
contains the names and the paths of all the articles of the wiki, which will be used by the Help System.
public class SwingJavaHelp extends JFrame { public SwingJavaHelp { super(); this.setTitle("DocGenerator Help Tutorial"); createLayout(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void createContent(Container cont) { JPanel areaPanel = new JPanel(); areaPanel.setLayout(new BorderLayout()); areaPanel.setBorder(new EmptyBorder(10, 10, 10, 10)); JTextArea area = new JTextArea(); area.setText("my area content"); areaPanel.add(area, BorderLayout.CENTER); cont.add(areaPanel, BorderLayout.CENTER); } private void createMenu() { JMenuBar menuBar = new JMenuBar(); this.setJMenuBar(menuBar); JMenu menu = new JMenu("Help"); menuBar.add(menu); AbstractAction helpAction = new AbstractAction("Help Content") { @Override public void actionPerformed(ActionEvent e) { } }; menu.add(helpAction); } private void createLayout() { Container cont = this.getContentPane(); cont.setLayout(new BorderLayout()); createContent(cont); createMenu(); } public static void main(String[] args) { SwingJavaHelp helper = new SwingJavaHelp(); helper.setSize(400, 400); helper.setVisible(true); } }
articles.zip
alongside our application. First we need to initialize our Help factory:public SwingJavaHelp() { super(); this.setTitle("DocGenerator Help Tutorial"); createFactory(); createLayout(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void createFactory() { URL url = this.getClass().getResource("resources/articles.zip"); factory = new JavaHelpFactory(url); try { factory.create(); } catch (IOException | SAXException ex) { ex.printStackTrace(); } }Now we will create a
SwingHelpContentViewer
, install the Help content on this viewer, and create our HelpWindow:private void createLayout() { Container cont = this.getContentPane(); cont.setLayout(new BorderLayout()); try { viewer = new SwingHelpContentViewer(); factory.install(viewer); viewer.getHelpWindow(this, "Help Content", 600, 700); } catch (IOException ex) { ex.printStackTrace(); } createContent(cont); }The last step is to wire our help item to the help content:
private void createMenu() { JMenuBar menuBar = new JMenuBar(); this.setJMenuBar(menuBar); JMenu menu = new JMenu("Help"); menuBar.add(menu); AbstractAction helpAction = new AbstractAction("Help Content") { @Override public void actionPerformed(ActionEvent e) { viewer.showHelpDialog(menu); } }; menu.add(helpAction); }See the full source code for the example
JavaHelpFactory
:
JavaHelpFactory factory = new JavaHelpFactory(url); factory.create();
SwingHelpContentViewer
and install the model on this viewer:
SwingHelpContentViewer viewer = new SwingHelpContentViewer(); factory.installModel(viewer);
viewer.getHelpWindow(this, "Help Content", 600, 700)
but we did not need in this case to use this Window directly. It is performed by the viewer.showHelpDialog(menu)
code. We could have use the Help window or directly get the Help component by viewer.getHelpComponent()
docJGenerator Copyright (c) 2016-2023 Herve Girod. All rights reserved.