Page tree
Skip to end of metadata
Go to start of metadata

Getting Started

Required Skills

The following skills are required prior to working with this tutorial:

  • Basic Java programming with the Swing library
Required Tutorials Completed

The following tutorials should be completed before attempting this tutorial:

Tutorial

Step 1. Create an Instance of EditLive! for Java Swing in a Frame and Set the Body

As shown in the Setting the Body Tutorial, create an instance of EditLive! for Java Swing in a JFrame and set the <BODY>.

 
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.ephox.editlive.*;
 
 
public class GetBody {
       /** html content to appear in the instance of EditLive! for Java */
       private static final String INITIAL_HTML = "<p>Original <i>HTML</i> loaded into EditLive!</p>";
 
       /** Base class for EditLive! for Java */
       private ELJBean editLiveBean = new ELJBean(INITIAL_HTML, "", 700, 570, getClass().getResource("sample_eljconfig.xml"));
 
       public GetBody() throws Exception {
               super("Tutorial - Getting the <BODY> of the EditLive! Document");
               this.getContentPane().setLayout(new FlowLayout());
 
               SwingUtilities.invokeAndWait(new Runnable() {
                       public void run() {
                               // initialize EditLive!
                               editLiveBean.init();
                       }
               });        
 
               // Create a JPanel to hold the ELJBean
               JPanel editorHolder = new JPanel(new FlowLayout());
               editorHolder.add(editLiveBean);
               // Add the JPanel to the JFrame
               this.getContentPane().add(editorHolder);
 
               // Display the JFrame.
               this.setSize(new Dimension(710, 620));
               this.setVisible(true);
 
               // Adding a listener to detect if the JFrame is closing, to close the application if needed.
               addWindowListener(new WindowAdapter() {
                       public void windowClosing(WindowEvent e) {
                               System.exit(0);
                       }
               });
       }
 
       /** Sets up the application and begins its execution
        *
        * @param args the command line arguments - ignored
        */
       public static void main(String args[]) throws Exception {
               try {
                       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
               } catch(Exception e) {
                       System.out.println("Unable to locate UIManager class");
                       e.printStackTrace();
               }
               new GetBody();
       }
}

Step 2. Create a JTextArea and a JButton and Add them to the JFrame

As seen in the Setting the Body Tutorial, create a JTextArea and a JButton on the page. The purpose of the JButton in this tutorial will be to copy the contents of the <BODY> element in EditLive! for Java Swing into the JTextArea.

 
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.ephox.editlive.*;
 
 
public class GetBody {
       /**        Buttons used to get html contents of  EditLive and set into JTextArea */
       private JButton bodyButton = new JButton("Set <BODY>");
 
       /** Allows users to enter html content to insert into EditLive!, or holds a copy of the html contents of EditLive! */
       private JTextArea source = new JTextArea(10, 30);
 
       /** html content to appear in the instance of EditLive! for Java */
       private static final String INITIAL_HTML = "<p>Original <i>HTML</i> loaded into EditLive!</p>";
 
       /** Base class for EditLive! for Java */
       private ELJBean editLiveBean = new ELJBean(INITIAL_HTML, "", 700, 570, getClass().getResource("sample_eljconfig.xml"));
 
       public GetBody() throws Exception {
               super("Tutorial - Getting the <BODY> of the EditLive! Document");
               this.getContentPane().setLayout(new GridLayout(2, 1));
 
               SwingUtilities.invokeAndWait(new Runnable() {
                       public void run() {
                               // initialize EditLive!
                               editLiveBean.init();
                       }
               });        
 
               // Create a JPanel to hold the ELJBean
               JPanel editorHolder = new JPanel(new FlowLayout());
               editorHolder.add(editLiveBean);
               // Add the JPanel to the JFrame
               this.getContentPane().add(editorHolder);
 
               // Create a JPanel to hold the button and the text area
               JPanel buttonAndText = new JPanel(new BorderLayout());
 
               // create a JPanel to hold the button
               JPanel buttonHolder = new JPanel(new FlowLayout());
               buttonHolder.add(bodyButton);
 
               // add button holding panel
               buttonAndText.add(buttonHolder, BorderLayout.NORTH);
 
               // create scrollable pane to hold text area
               JScrollPane textAreaHolder = new JScrollPane(source);
               buttonAndText.add(textAreaHolder);
 
               // add button and textarea to frame
               this.getContentPane().add(buttonAndText, BorderLayout.CENTER);
 
               // Display the JFrame.
               this.setSize(new Dimension(710, 620));
               this.setVisible(true);
 
               // Adding a listener to detect if the JFrame is closing, to close the application if needed.
               addWindowListener(new WindowAdapter() {
                       public void windowClosing(WindowEvent e) {
                               System.exit(0);
                       }
               });
       }
 
       /** Sets up the application and begins its execution
        *
        * @param args the command line arguments - ignored
        */
       public static void main(String args[]) throws Exception {
               try {
                       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
               } catch(Exception e) {
                       System.out.println("Unable to locate UIManager class");
                       e.printStackTrace();
               }
               new GetBody();
       }
}

Step 3. Extend the ActionListener and Add an ActionListener to the JButton

By implementing the ActionListener interface this class can listen to ActionEvents. Add this class as an ActionListener to the JButton.

 
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.ephox.editlive.*;
 
 
public class GetBody implements ActionListener {
       /**        Buttons used to get html contents of  EditLive and set into JTextArea */
       private JButton bodyButton = new JButton("Set <BODY>");
 
       /** Allows users to enter html content to insert into EditLive!, or holds a copy of the html contents of EditLive! */
       private JTextArea source = new JTextArea(10, 30);
 
       /** html content to appear in the instance of EditLive! for Java */
       private static final String INITIAL_HTML = "<p>Original <i>HTML</i> loaded into EditLive!</p>";
 
       /** Base class for EditLive! for Java */
       private ELJBean editLiveBean = new ELJBean(INITIAL_HTML, "", 700, 570, getClass().getResource("sample_eljconfig.xml"));
 
       public GetBody() throws Exception {
               super("Tutorial - Getting the <BODY> of the EditLive! Document");
               this.getContentPane().setLayout(new GridLayout(2, 1));
 
               SwingUtilities.invokeAndWait(new Runnable() {
                       public void run() {
                               // initialize EditLive!
                               editLiveBean.init();
                       }
               });        
 
               // Create a JPanel to hold the ELJBean
               JPanel editorHolder = new JPanel(new FlowLayout());
               editorHolder.add(editLiveBean);
               // Add the JPanel to the JFrame
               this.getContentPane().add(editorHolder);
 
               // Create a JPanel to hold the button and the text area
               JPanel buttonAndText = new JPanel(new BorderLayout());
 
               // create a JPanel to hold the button
               JPanel buttonHolder = new JPanel(new FlowLayout());
               buttonHolder.add(bodyButton);
               // add listener to button
               bodyButton.addActionListener(this);
 
               // add button holding panel
               buttonAndText.add(buttonHolder, BorderLayout.NORTH);
 
               // create scrollable pane to hold text area
               JScrollPane textAreaHolder = new JScrollPane(source);
               buttonAndText.add(textAreaHolder);
 
               // add button and textarea to frame
               this.getContentPane().add(buttonAndText, BorderLayout.CENTER);
 
               // Display the JFrame.
               this.setSize(new Dimension(710, 620));
               this.setVisible(true);
 
               // Adding a listener to detect if the JFrame is closing, to close the application if needed.
               addWindowListener(new WindowAdapter() {
                       public void windowClosing(WindowEvent e) {
                               System.exit(0);
                       }
               });
       }
 
       /** Sets up the application and begins its execution
        *
        * @param args the command line arguments - ignored
        */
       public static void main(String args[]) throws Exception {
               try {
                       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
               } catch(Exception e) {
                       System.out.println("Unable to locate UIManager class");
                       e.printStackTrace();
               }
               new GetBody();
       }
}

Step 4. Implementing the actionPerformed Method

Finally, to catch events fired by the JButton, implement the actionPerformed method.

In this method, the getDocument() method of the ELJBean is used to set the contents of the JTextArea with the EditLive! for Java Swing <BODY> HTML. This method is invoked on the Swing Thread to ensure no threading problems can occur between the ELJBean and the rest of the application.

 
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.ephox.editlive.*;
 
 
public class GetBody implements ActionListener {
       /**        Buttons used to get html contents of  EditLive and set into JTextArea */
       private JButton bodyButton = new JButton("Set <BODY>");
 
       /** Allows users to enter html content to insert into EditLive!, or holds a copy of the html contents of EditLive! */
       private JTextArea source = new JTextArea(10, 30);
 
       /** html content to appear in the instance of EditLive! for Java */
       private static final String INITIAL_HTML = "<p>Original <i>HTML</i> loaded into EditLive!</p>";
 
       /** Base class for EditLive! for Java */
       private ELJBean editLiveBean = new ELJBean(INITIAL_HTML, "", 700, 570, getClass().getResource("sample_eljconfig.xml"));
 
       public GetBody() throws Exception {
               super("Tutorial - Getting the <BODY> of the EditLive! Document");
               this.getContentPane().setLayout(new GridLayout(2, 1));
 
               SwingUtilities.invokeAndWait(new Runnable() {
                       public void run() {
                               // initialize EditLive!
                               editLiveBean.init();
                       }
               });        
 
               // Create a JPanel to hold the ELJBean
               JPanel editorHolder = new JPanel(new FlowLayout());
               editorHolder.add(editLiveBean);
               // Add the JPanel to the JFrame
               this.getContentPane().add(editorHolder);
 
               // Create a JPanel to hold the button and the text area
               JPanel buttonAndText = new JPanel(new BorderLayout());
 
               // create a JPanel to hold the button
               JPanel buttonHolder = new JPanel(new FlowLayout());
               buttonHolder.add(bodyButton);
               // add listener to button
               bodyButton.addActionListener(this);
 
               // add button holding panel
               buttonAndText.add(buttonHolder, BorderLayout.NORTH);
 
               // create scrollable pane to hold text area
               JScrollPane textAreaHolder = new JScrollPane(source);
               buttonAndText.add(textAreaHolder);
 
               // add button and textarea to frame
               this.getContentPane().add(buttonAndText, BorderLayout.CENTER);
 
               // Display the JFrame.
               this.setSize(new Dimension(710, 620));
               this.setVisible(true);
 
               // Adding a listener to detect if the JFrame is closing, to close the application if needed.
               addWindowListener(new WindowAdapter() {
                       public void windowClosing(WindowEvent e) {
                               System.exit(0);
                       }
               });
       }
 
       /** ActionListener for JButtons on the JFrame
        *
        * @param e ActionEvent sent by JButton
        */
       public void actionPerformed(ActionEvent e) {                        
               if (e.getSource() == bodyButton) {
                       try  {
                               SwingUtilities.invokeAndWait(new Runnable() {
                                       public void run() {
                                               source.setText(editLiveBean.getBody());
                                       }
                               });                                                
                       } catch (Exception exception) {
                               exception.printStackTrace();
                       }
               }
       }
 
       /** Sets up the application and begins its execution
        *
        * @param args the command line arguments - ignored
        */
       public static void main(String args[]) throws Exception {
               try {
                       UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
               } catch(Exception e) {
                       System.out.println("Unable to locate UIManager class");
                       e.printStackTrace();
               }
               new GetBody();
       }
}

See Also

  • No labels