The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Homework - Java to HTML, enable user to enter a number of menu items
Discuss Java to HTML, enable user to enter a number of menu items in the Java Help forum on Dev Shed. Java to HTML, enable user to enter a number of menu items Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 30th, 2012, 07:55 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 3
Time spent in forums: 25 m 35 sec
Reputation Power: 0
|
|
|
Homework - Java to HTML, enable user to enter a number of menu items
I have this code:
Code:
Java
package sitecreator;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class MainForm extends javax.swing.JFrame {
public MainForm() {
initComponents();
}
String getData()
{
FileReader fr = null;
StringBuilder html = new StringBuilder();
try
{
fr = new FileReader("template.html");
int b;
while ((b = fr.read()) != -1) {
html.append(Character.toString((char) b));
}
fr.close();
}
catch(IOException ex) { JOptionPane.showMessageDialog(epContent, "Error loading template!"); }
return html.toString();
}
String insertData(String template)
{
String[] meniPrvi = {"###menu1Link###", "###menu1Title###","###menu2Link###","###menu2Title###",
"###menu3Link###", "###menu3Title###", "###menu4Link###", "###menu4Title###",
"###menu5Link###", "###menu5Title###", "###menu6Link###","###menu6Title###",
"###image1###", "###image2###", "###content###", "###footer###", "#FF9900", "#FF9901"};
String[] meniDrugi = {tbMenu1Link.getText(),tbMenu1Title.getText(),tbMenu2Link.getText(),tbMenu2Title.getText(),
tbMenu3Link.getText(),tbMenu3Title.getText(),tbMenu4Link.getText(), tbMenu4Title.getText(),
tbMenu5Link.getText(), tbMenu5Title.getText(), tbMenu6Link.getText(),tbMenu6Title.getText(),
tbImage1.getText(), tbImage2.getText(), epContent.getText(), epFooter.getText(),
menuColor.getText(), footerColor.getText()};
for (int i = 0; i<meniPrvi.length; i++){
template = template.replace(meniPrvi[i], meniDrugi[i]);
}
return template;
}
void saveData(String data)
{
JFileChooser jfc = new JFileChooser();
jfc.setVisible(true);
jfc.setFileFilter(new javax.swing.filechooser.FileFilter() {
@Override
public boolean accept(File f) {
if (f.isDirectory())
return true;
String fn = f.getName();
String e="";
int i = fn.lastIndexOf('.');
if (i > 0 && i < fn.length() - 1) {
e = fn.substring(i+1).toLowerCase();
}
if(e.equals("html"))
return true;
else
return false;
}
@Override
public String getDescription() {
return "html files";
}
});
int res = jfc.showSaveDialog(this);
if(res!=JFileChooser.CANCEL_OPTION&&jfc.getSelectedFile()!=null)
{
FileWriter fw = null;
try {
fw = new FileWriter(jfc.getSelectedFile());
fw.write(data);
fw.close();
JOptionPane.showMessageDialog(epContent, "Dokumenat je uspješno sačuvan.");
} catch (IOException ex) { }
finally {
try { fw.close(); } catch (IOException ex) { }
}
}
else JOptionPane.showMessageDialog(rootPane, "File not selected!");
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
tbMenu1Title = new javax.swing.JTextField();
tbMenu1Link = new javax.swing.JTextField();
tbMenu2Title = new javax.swing.JTextField();
tbMenu3Title = new javax.swing.JTextField();
tbMenu2Link = new javax.swing.JTextField();
tbMenu3Link = new javax.swing.JTextField();
tbMenu4Title = new javax.swing.JTextField();
tbMenu5Title = new javax.swing.JTextField();
tbMenu6Title = new javax.swing.JTextField();
tbMenu4Link = new javax.swing.JTextField();
tbMenu5Link = new javax.swing.JTextField();
tbMenu6Link = new javax.swing.JTextField();
jScrollPane1 = new javax.swing.JScrollPane();
epContent = new javax.swing.JEditorPane();
jScrollPane2 = new javax.swing.JScrollPane();
epFooter = new javax.swing.JEditorPane();
Generate = new javax.swing.JButton();
tbImage1 = new javax.swing.JButton();
tbImage2 = new javax.swing.JButton();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
menuColor = new javax.swing.JTextField();
footerColor = new javax.swing.JTextField();
addFiled = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Menu options ");
jLabel2.setText("Content text");
jLabel3.setText("Footer text");
jLabel4.setText("Images");
jScrollPane1.setViewportView(epContent);
jScrollPane2.setViewportView(epFooter);
Generate.setText("Generate");
Generate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
GenerateActionPerformed(evt);
}
});
tbImage1.setText("Slika 1");
tbImage1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tbImage1ActionPerformed(evt);
}
});
tbImage2.setText("Slika 2");
tbImage2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tbImage2ActionPerformed(evt);
}
});
jLabel5.setText("Colors");
jLabel6.setText("Menu bg color:");
jLabel7.setText("Footer bg color:");
addFiled.setText("Add field");
addFiled.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
addFiledActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addContainerGap(400, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addContainerGap(405, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel3)
.addContainerGap(412, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel4)
.addContainerGap(432, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 447, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 447, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(tbMenu6Title, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tbMenu5Title, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tbMenu4Title, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tbMenu1Title, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
.addComponent(tbMenu2Title, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tbMenu3Title, javax.swing.GroupLayout.Alignment.LEADING))
.addGap(48, 48, 48)
.addComponent(addFiled)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 68, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(tbMenu2Link)
.addComponent(tbMenu3Link)
.addComponent(tbMenu1Link, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
.addComponent(tbMenu4Link)
.addComponent(tbMenu5Link)
.addComponent(tbMenu6Link)))
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addGap(153, 153, 153)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(menuColor, javax.swing.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)
.addComponent(tbImage2, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(footerColor, javax.swing.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE))))
.addGap(20, 20, 20))
.addGroup(layout.createSequentialGroup()
.addComponent(tbImage1, javax.swing.GroupLayout.PREFERRED_SIZE, 101, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(Generate)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel5)
.addContainerGap(437, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel6)
.addContainerGap(396, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel7)
.addContainerGap(390, Short.MAX_VALUE))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tbMenu1Title, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tbMenu1Link, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tbMenu2Title, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tbMenu2Link, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tbMenu3Title, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tbMenu3Link, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tbMenu4Title, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tbMenu4Link, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tbMenu5Title, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tbMenu5Link, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tbMenu6Title, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(tbMenu6Link, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(addFiled))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jLabel4)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(tbImage1)
.addComponent(tbImage2))
.addGap(18, 18, 18)
.addComponent(jLabel5)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(menuColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel7)
.addComponent(footerColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
.addComponent(Generate)
.addContainerGap())
);
pack();
}// </editor-fold>
private void GenerateActionPerformed(java.awt.event.ActionEvent evt) {
saveData(insertData(getData()));
}
private void tbImage1ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser jfc = new JFileChooser();
jfc.setFileFilter(new javax.swing.filechooser.FileFilter() {
@Override
public boolean accept(File f) {
if (f.isDirectory())
return true;
String fn = f.getName();
String e="";
int i = fn.lastIndexOf('.');
if (i > 0 && i < fn.length() - 1) {
e = fn.substring(i+1).toLowerCase();
}
if(e.equals("jpg")|| e.equals("gif")|| e.equals("png")
|| e.equals("tif")|| e.equals("jpeg")|| e.equals("tiff"))
return true;
else
return false;
}
@Override
public String getDescription() {
return "Image files";
}
});
int status = jfc.showOpenDialog(null);
String s= "";
if (status == JFileChooser.APPROVE_OPTION){
s =(jfc.getSelectedFile()).toString();
tbImage1.setText(s); }
else if (status ==JFileChooser.CANCEL_OPTION)
tbImage1.setText("Please select image file!");
}
private void tbImage2ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser jfc = new JFileChooser();
jfc.setFileFilter(new javax.swing.filechooser.FileFilter() {
@Override
public boolean accept(File f) {
if (f.isDirectory())
return true;
String fn = f.getName();
String e="";
int i = fn.lastIndexOf('.');
if (i > 0 && i < fn.length() - 1) {
e = fn.substring(i+1).toLowerCase();
}
if(e.equals("jpg")|| e.equals("gif")|| e.equals("png")
|| e.equals("tif")|| e.equals("jpeg")|| e.equals("tiff"))
return true;
else
return false;
}
@Override
public String getDescription() {
return "Image files";
}
});
int status = jfc.showOpenDialog(null);
String s= "";
if (status == JFileChooser.APPROVE_OPTION){
s =(jfc.getSelectedFile()).toString();
tbImage2.setText(s); }
else if (status ==JFileChooser.CANCEL_OPTION)
tbImage2.setText("Please select image file!");
}
private void addFiledActionPerformed(java.awt.event.ActionEvent evt) {
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MainForm().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton Generate;
private javax.swing.JButton addFiled;
private javax.swing.JEditorPane epContent;
private javax.swing.JEditorPane epFooter;
private javax.swing.JTextField footerColor;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextField menuColor;
private javax.swing.JButton tbImage1;
private javax.swing.JButton tbImage2;
private javax.swing.JTextField tbMenu1Link;
private javax.swing.JTextField tbMenu1Title;
private javax.swing.JTextField tbMenu2Link;
private javax.swing.JTextField tbMenu2Title;
private javax.swing.JTextField tbMenu3Link;
private javax.swing.JTextField tbMenu3Title;
private javax.swing.JTextField tbMenu4Link;
private javax.swing.JTextField tbMenu4Title;
private javax.swing.JTextField tbMenu5Link;
private javax.swing.JTextField tbMenu5Title;
private javax.swing.JTextField tbMenu6Link;
private javax.swing.JTextField tbMenu6Title;
// End of variables declaration
}
Here is the problem: I need to use button "Add field" to enable user to add two more text fields every time the button is clicked. The left field represents link name and the right text field represents link address. After those fields are entered the app uses these values to generate html document (based on a template). With the current code user can enter only six menu links (and their addresses). I need to leave the possibility for the user to use "Add field" button to add more than six menu links.
|

November 30th, 2012, 02:05 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|