Exporting items from a list box to an HTML file

colormehappy
colormehappy used Ask the Experts™
on
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)");
      pnlEnterName.add(lblEnterName,BorderLayout.NORTH);
      txtEnterName = new TextField();
      pnlEnterName.add(txtEnterName,BorderLayout.SOUTH);
       
        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");
            pnlActions.add(cmdAddName);
      cmdCreateHtml = new Button("Create HTML");
            pnlActions.add(cmdCreateHtml);
      cmdExit = new Button("EXIT");
            pnlActions.add(cmdExit);

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

      setUpListeners();
}

//Section 2 - Extend Window Adapter and implement ActionListener
void setUpListeners() {
      cmdAddName.addActionListener(this);
      cmdCreateHtml.addActionListener(this);
      cmdExit.addActionListener(this);
      frameWriteNames.addWindowListener(this);
}

public void WindowClosing(WindowEvent event) {
      System.exit(0);
}

public void actionPerformed(ActionEvent event) {
      String cmd = event.getActionCommand();
      System.out.println(cmd);
      if (cmd == "EXIT")
            System.exit(0);
         else if (cmd == "Create HTML")
            createHtml();
      else if (cmd == "Add Name")
            addName();
      }

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

      lstDisplayName.add(input);
      txtEnterName.setText("");
      txtEnterName.requestFocus();
}

//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("<html>");
                  out.println("<head>");
                  out.println("<title>Homework Example 3</title>");
                  out.println("</head>");

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

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

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

                  //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("<tr>");
                        out.println("<td>" + lastName + "</td>");
                        out.println("<td>" + firstName + "</td>");
                        out.println("<td>");
                              name = lstDisplayName.getItem(counter);
                        out.println (name);

                        out.println("</td>");
                        out.println("</tr>");
                  }

                  out.println("</table>");


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


                  out.println("</body>");
                  out.println("</html>");

                  out.close();

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


      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;
    processName();
  }

  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();
      else
        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);
      }
      else
        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);
    setLastName();
    setFirstName();
    setMiddleName();
  }

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

Do more with

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

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


vemul

Commented:
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..

vemul
Mick BarryJava Developer
Top Expert 2010

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

    if (cmd == "EXIT")
          System.exit(0);
       else if (cmd == "Create HTML")
          createHtml();
     else if (cmd == "Add Name")
          addName();
     }


Instead use equals() method:

    if ("EXIT".equals(cmd))
          System.exit(0);
       else if ("Create HTML".equals(cmd))
          createHtml();
     else if ("Add Name".equals(cmd))
          addName();
     }

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