Creating the User Interface for the Online Java Tutorial Application


Creating the User Interface for the Online Java Tutorial Application

The Tutorial.java file creates the user interface for the Online Java Tutorial application. Listing 7-1 shows the contents of the Tutorial.java file:

Listing 7-1: The Tutorial.java File
start example
 /*Import required swing classes*/ import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JSplitPane; import javax.swing.UIManager; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeSelectionModel; import javax.swing.event.TreeSelectionEvent; import javax.swing.event.TreeSelectionListener; /*Import required net and io classes*/ import java.net.URL; import java.io.IOException; /*Import required awt classes*/ import java.awt.Dimension; import java.awt.GridLayout; import java.lang.*; import java.awt.*; /* class Tutorial - This class is the main class of the application. Displaying the topic and content of topic. Constructor: Tutorial - This constructor creates GUI. Methods: displayURL - Used to take the book URL and display in to the text pane. initHelp - Used to display the help. createTopic - Used to create a tree for topic. main - This method creates the main window of the application and displays all the components. */ public class Tutorial extends JPanel implements TreeSelectionListener, Runnable {       /*Declare variables*/       private JEditorPane descPane;       private JTree tree;       private URL helpURL;       String selectedVoice=new String("kevin16");       String speakstring=null;       SpeakText st=null;       Thread t;       String desc=null;     public Tutorial()      {       super(new GridLayout(1,0));       DefaultMutableTreeNode top = new DefaultMutableTreeNode("The Java Series");       createTopic(top);       tree = new JTree(top);       tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);       tree.addTreeSelectionListener(this);       JScrollPane treeView = new JScrollPane(tree);       /*       Create the Description viewing pane        */       descPane = new JEditorPane();       descPane.setEditable(false);       initHelp();       JScrollPane htmlView = new JScrollPane(descPane);       JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);       splitPane.setTopComponent(treeView);       splitPane.setBottomComponent(htmlView);       Dimension minimumSize = new Dimension(300, 150);       htmlView.setMinimumSize(minimumSize);       treeView.setMinimumSize(minimumSize);       splitPane.setDividerLocation(200);        splitPane.setPreferredSize(new Dimension(700, 400));       add(splitPane);     }    /*    Required by TreeSelectionListener interface    */    public void valueChanged(TreeSelectionEvent e)    {       DefaultMutableTreeNode node = (DefaultMutableTreeNode)       tree.getLastSelectedPathComponent();       if (node == null) return;       System.out.println("Node Information in node"+node.toString());       speakstring=node.toString();       Object nodeInfo = node.getUserObject();       if (node.isLeaf())        {          TopicInfo book = (TopicInfo)nodeInfo;          displayURL(book.bookURL);       }       else       {          displayURL(helpURL);        }       desc=descPane.getText();       System.out.println("Description is "+desc);       System.out.println(nodeInfo.toString());       t=new Thread(this,"Node action");       t.start();    }    private class TopicInfo    {       public String topicName;       public URL bookURL;       public TopicInfo(String book, String filename)       {          topicName = book;          bookURL = Tutorial.class.getResource(filename);          if (bookURL == null)          {             System.err.println("Couldn't find file: "+ filename);          }       }       public String toString()       {          return topicName;       }     }     private void initHelp()     {       String s = "DemoHelp.txt";       helpURL = Tutorial.class.getResource(s);       if (helpURL == null)        {          System.err.println("Couldn't open help file: " + s);       }       displayURL(helpURL);     }    /*    This method takes the object if the URL class as an input parameter and displays it to the text    pane     */     private void displayURL(URL url)     {       try       {          if (url != null)          {             descPane.setPage(url);          }          else           {              descPane.setText("File Not Found");          }       }       catch (IOException e)       {          System.err.println("Attempted to read a bad URL: " + url);       }    }    /*    This method creates a tree structure for tutorials and their topics     */    private void createTopic(DefaultMutableTreeNode top)    {       DefaultMutableTreeNode category = null;       DefaultMutableTreeNode book = null;       category = new DefaultMutableTreeNode("Section 1");       top.add(category);       book = new DefaultMutableTreeNode(new TopicInfo       ("Topic 1",       "topic1.txt"));       category.add(book);       book = new DefaultMutableTreeNode(new TopicInfo       ("Topic 2",       "topic2.txt"));       category.add(book);       book = new DefaultMutableTreeNode(new TopicInfo       ("Topic 3",       "topic3.txt"));       category.add(book);       book = new DefaultMutableTreeNode(new TopicInfo       ("Topic 4",       "topic4.txt"));       category.add(book);       book = new DefaultMutableTreeNode(new TopicInfo       ("Topic 5", "topic5.txt"));       category.add(book);       book = new DefaultMutableTreeNode(new TopicInfo       ("Topic 6",        "topic6.txt"));       category.add(book);       category = new DefaultMutableTreeNode("Section 2");       top.add(category);       book = new DefaultMutableTreeNode(new TopicInfo       ("Topic 1",        "topic11.txt"));       category.add(book);       book = new DefaultMutableTreeNode(new TopicInfo       ("Topic 2",        "topic12.txt"));       category.add(book);    }    public void run()    {       /*       Initialize a new object of SpeakText class       */       st=new SpeakText(speakstring+desc, selectedVoice);       /*       Invoke the speakSelText() method of SpeakText class       */       st.speakSelText(this);    }    public static void main(String[] args)    {       try        {          UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");       }       catch (Exception e)        {          System.err.println("Couldn't use system look and feel.");       }       JFrame.setDefaultLookAndFeelDecorated(true);       /*Create the window.*/       JFrame frame = new JFrame("EJB Tutorial");       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       Tutorial newContentPane = new Tutorial();       newContentPane.setOpaque(false);        frame.setContentPane(newContentPane);       /*Display the window.*/       frame.pack();       frame.setVisible(true);    } } 
end example
 

Download this listing .

In the above code, the main() method creates an instance of the Tutorial.java class.

The methods defined in Listing 7-1 are:

  • createTopic (): Creates a tree-view structure for the tutorial of the Online Java Tutorial application.

  • initHelp (): Creates an instance of the URL class and passes the object of the URL class to the displayURL() method.

  • displayURL (): Shows the text corresponding to the selected topic.

  • run (): Creates an instance of the SpeakText class to read out the text corresponding to the selected topic.

The Tutorial.java file generates the main window of the Online Java Tutorial application, as shown in Figure 7-2:

click to expand: this figure shows the user interface of the online java tutorial application, which contains the tutorial of the java series in the left pane and corresponding text in the right pane.
Figure 7-2: User Interface of the Online Java Tutorial Application

Select a tutorial from the left pane to view the list of topics of the selected tutorial in the right pane. Figure 7-3 shows the number of topics present in the selected tutorial of the Java series:

click to expand: this figure shows the topics of the selected tutorial in the left pane.
Figure 7-3: Topic List

When an end user selects a topic from the Java series, the text corresponding to that topic appears in the right pane, as shown in Figure 7-4:

click to expand: this figure shows the corresponding text of the selected topic in the right pane.
Figure 7-4: Selected Topic and the Corresponding Text



Java InstantCode. Developing Applications using Java Speech API
Java InstantCode. Developing Applications using Java Speech API
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 46

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net