Exporting items from a list box to an HTML file

colormehappy used Ask the Experts™
I have created a GUI where a user can enter a name, click 'Add Name' and it is then displayed in a listbox. However, I need to now export each item from the listbox, parse it, and display it within a table in an HTML file.

I am having difficult passing the data into/out of variables.

**How can I parse each item, and then display in a table within an HTML file?**  See code below: ***I've included a Names class for parsing. Trouble is passing data***

import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Homework3 extends WindowAdapter implements ActionListener {

Frame frameWriteNames;

Panel pnlEnterName;
Label lblEnterName;
TextField txtEnterName;

Panel pnlDisplayName;
Label lblDisplayName;
List lstDisplayName;

Panel pnlActions;
Button cmdAddName;
Button cmdCreateHtml;
Button cmdExit;

//*Section 1 - Create a GUI
public Homework3 () {
      frameWriteNames = new Frame ("Manage Contacts");
      frameWriteNames.setLayout(new BorderLayout());
      pnlEnterName = new Panel();
      pnlEnterName.setLayout(new BorderLayout());
      lblEnterName = new Label("Enter full name (Smith, John Jacob)");
      txtEnterName = new TextField();
        pnlDisplayName = new Panel();
      pnlDisplayName.setLayout(new BorderLayout());
      lblDisplayName = new Label("Contacts:");
      pnlDisplayName.add(lblDisplayName, BorderLayout.NORTH);
      lstDisplayName = new List(10, false);
      pnlDisplayName.add(lstDisplayName, BorderLayout.SOUTH);

      pnlActions = new Panel();
      pnlActions.setLayout(new FlowLayout());
      cmdAddName = new Button("Add Name");
      cmdCreateHtml = new Button("Create HTML");
      cmdExit = new Button("EXIT");

      frameWriteNames.add(pnlEnterName, BorderLayout.NORTH);
      frameWriteNames.add(pnlDisplayName, BorderLayout.CENTER);
      frameWriteNames.add(pnlActions, BorderLayout.SOUTH);


//Section 2 - Extend Window Adapter and implement ActionListener
void setUpListeners() {

public void WindowClosing(WindowEvent event) {

public void actionPerformed(ActionEvent event) {
      String cmd = event.getActionCommand();
      if (cmd == "EXIT")
         else if (cmd == "Create HTML")
      else if (cmd == "Add Name")

//Section 3 - Create method to add a name
void addName() {
      String input = txtEnterName.getText();
      if (input.length() == 0) {


//Section 4 - Create HTML file
void createHtml() {
            String name;
            String lastName;
            String firstName;
            String middleName;
            String email;
            int counter;

            try {
                  File outputFile;
                  outputFile = new File("contacts.html");
                  FileWriter charOutput;
                  charOutput = new FileWriter(outputFile);
                  PrintWriter out;
                  out = new PrintWriter(charOutput);

                  out.println("<title>Homework Example 3</title>");

                  out.println("<body bgcolor='#FFFFFF'>");
                  out.println("<p> </p>");

                  out.println("<p>Message Contacts</p>");
                  out.println("<p> </p>");

                  out.println("<td>Last Name</td>");
                  out.println("<td>First Name</td>");
                  out.println("<td>Middle Name</td>");

                  //Moses: loop through the list, in each loop, process each list item
                  for (counter = 0; counter < lstDisplayName.getItemCount(); counter ++)

            //parse the name

                        name = Name.initialCaps(lstDisplayName.getItem(counter));
                        lastName = Name.getLastName(name);
                        firstName = Name.getFirstName(name);
                        middleName = Name.getMiddleName(name);

                        out.println("<td>" + lastName + "</td>");
                        out.println("<td>" + firstName + "</td>");
                              name = lstDisplayName.getItem(counter);
                        out.println (name);



      //missing format to html page
      //need to display names within html file



                  System.out.println("File Created");
            }      catch (IOException e) {

      public static void main(String[] args) {
            Homework3 layout = new Homework3();


import java.util.*;
import java.io.*;

public class Name implements Serializable {

  private String fullName;
  private StringTokenizer inWorkToken;
  private String inWorkString;
  private String firstName=null;
  private String middleName=null;
  private String lastName=null;

  public Name() {

  public String getFullName() {return fullName;}

  public void setFullName(String fn) {
    fullName = fn;

  private void setFirstName() {
    //We'll now pull out the first name.
    if (inWorkToken.hasMoreTokens()) {
      firstName = inWorkToken.nextToken().trim();
      firstName = initialCaps(firstName);
      //Now we'll get what's left after the first name
      if (inWorkToken.hasMoreTokens())
        inWorkString = inWorkToken.nextToken("\n").trim();
        inWorkString = null;

  public String getFirstName() {return firstName;}

  private void setLastName() {
    //Get the Last name (up to the comma)
    if (inWorkToken.hasMoreTokens()) {
      lastName = inWorkToken.nextToken(",").trim();
      lastName = initialCaps(lastName);
      //Next, we get the rest of the line and put it in inWorkString
      //We must use the comma again so it pulls it out of the string,
      //otherwise it would leave it in.
      if (inWorkToken.hasMoreTokens()) {
        inWorkString = inWorkToken.nextToken(",").trim();
        inWorkToken = new StringTokenizer(inWorkString);
        inWorkString = null;

  public String getLastName() {return lastName;}

  private void setMiddleName() {
    //All that's left is the middle name.
    middleName = initialCaps(inWorkString);

  public String getMiddleName() {return middleName;}

  public void processName() {
    if (fullName == null || fullName.length() ==0) return;
    inWorkToken = new StringTokenizer(fullName);

  private String initialCaps(String data) {
     if (data != null) {
       data = data.toUpperCase();
       data = data.charAt(0) + data.substring(1).toLowerCase();
     return data;
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
did u try this..

for(int i = 0; i <  lstDisplayName.getItemCount(); i++){
     out.println("<td>" + lstDisplayName.getItem(i) + "</td>");


oops.. if you don't need it in a table, u can't get rid of those html tags.. if you do need it in a table, make sure u add the appropriate <table> etc tags..

Mick BarryJava Developer
Top Expert 2010

You need to change the following as == check if two objects are the same object not whether the strings are equal:

    if (cmd == "EXIT")
       else if (cmd == "Create HTML")
     else if (cmd == "Add Name")

Instead use equals() method:

    if ("EXIT".equals(cmd))
       else if ("Create HTML".equals(cmd))
     else if ("Add Name".equals(cmd))

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial