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 Document tutorial, create an instance of EditLive! for Java Swing in a JFrame and set the Document.
import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import com.ephox.editlive.*; public class GetDocument { /** html content to appear in the instance of EditLive! for Java */ private static final String INITIAL_HTML = "<html><head><title>Default Document Title</title></head><body><p>Original <i>HTML Document</i> loaded into EditLive!</p></body></html>"; /** Base class for EditLive! for Java */ private ELJBean editLiveBean = new ELJBean(INITIAL_HTML, "", 700, 570, getClass().getResource("sample_eljconfig.xml")); public GetDocument() throws Exception { super("Tutorial - Getting 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 GetDocument(); } }
Step 2. Create a JTextArea and a JButton and Add them to the JFrame
As seen in the Setting the Document 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 HTML Document 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 GetDocument { /** Buttons used to get html contents of EditLive and set into JTextArea */ private JButton documentButton = new JButton("Set Document"); /** 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 = "<html><head><title>Default Document Title</title></head><body><p>Original <i>HTML Document</i> loaded into EditLive!</p></body></html>"; /** Base class for EditLive! for Java */ private ELJBean editLiveBean = new ELJBean(INITIAL_HTML, "", 700, 570, getClass().getResource("sample_eljconfig.xml")); public GetDocument() throws Exception { super("Tutorial - Getting 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(documentButton); // 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 GetDocument(); } }
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 GetDocument implements ActionListener { /** Buttons used to get html contents of EditLive and set into JTextArea */ private JButton documentButton = new JButton("Set Document"); /** 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 = "<html><head><title>Default Document Title</title></head><body><p>Original <i>HTML Document</i> loaded into EditLive!</p></body></html>"; /** Base class for EditLive! for Java */ private ELJBean editLiveBean = new ELJBean(INITIAL_HTML, "", 700, 570, getClass().getResource("sample_eljconfig.xml")); public GetDocument() throws Exception { super("Tutorial - Getting 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(documentButton); // add listener to button documentButton.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 GetDocument(); } }
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 HTML Document. 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 GetDocument implements ActionListener { /** Buttons used to get html contents of EditLive and set into JTextArea */ private JButton documentButton = 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 = "<html><head><title>Default Document Title</title></head><body><p>Original <i>HTML Document</i> loaded into EditLive!</p></body></html>"; /** Base class for EditLive! for Java */ private ELJBean editLiveBean = new ELJBean(INITIAL_HTML, "", 700, 570, getClass().getResource("sample_eljconfig.xml")); public GetDocument() throws Exception { super("Tutorial - Getting 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(documentButton); // add listener to button documentButton.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() == documentButton) { try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { source.setText(editLiveBean.getDocument()); } }); } catch (Exception exception) { // TODO Auto-generated catch block 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 GetDocument(); } }