codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Please can someone show me how to make a java box pop upand display personal information!  sethmtut at 01:21 on Thursday, August 16, 2007
 

Hey people, Im trying to design an organiser in java using netbeans for a project. Iv made the initial GUI which is a box that loads up and it has my logo on it and below that a lable saying Personal Information. It has a View button next to it (iv named the variable view1 and it is a JButton)

Im finding it really difficult to do. What i would like is another box that pops up allowing the user to enter their name, address, date of birth, current school year and car registration, and store the data entered if possible.

If anyone could help me with the code i would be really greatful, it would also help if you could comment next to the important code to help me understand whats going on.

I would really appreciate it guys ;) thanks

  Re: Please can someone show me how to make a java box pop upand display personal information!  sam_02 at 01:49 on Thursday, August 16, 2007
 

collecting and storing the data in variables is easy.. but how do u plan to finally store the data?? flat files (.txt, .dat) or some other database such as MS Access, etc..

  Re: Please can someone show me how to make a java box pop upand display personal information!  sethmtut at 15:08 on Thursday, August 16, 2007
 

Hi, just text files would be sufficient

thanks alot

  Re: Please can someone show me how to make a java box pop upand display personal information!  sethmtut at 14:07 on Sunday, August 19, 2007
 

ok iv come up with this code:

import java.awt.GridLayout;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Foo
{
// class attributes..
private String[] labels = new String[]
{
"Name", "Address", "City", "State", "Zip Code", "Phone", "Birthdate"
};
JTextField[] fields;

public Foo()
{
super();
fields = new JTextField[labels.length];
}

public String[] getInformation()
{
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 2, 5, 5));

for (int i = 0; i < labels.length; i++)
{
fields = new JTextField(10);
panel.add(new JLabel(labels));
panel.add(fields);
}

// allow the user to fill in the fields
if (JOptionPane.showConfirmDialog(null, panel) == JOptionPane.OK_OPTION)
{
String[] returnStr = new String[fields.length];
for (int i = 0; i < returnStr.length; i++)
{
returnStr = fields.getText();
}
return returnStr;
}
else
{
return null;
}

}

public void writeToFile(String[] info)
{
PrintWriter pw = null;
try
{
pw = new PrintWriter(new FileOutputStream(new File("/forum/data.txt")));
for (int i = 0; i < info.length; i++)
pw.println(labels + ": " + info);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
finally
{
if (pw != null) pw.close();
}
}

public static void main(String[] args)
{
Foo f = new Foo();
String[] results = f.getInformation();
if (results != null)
{
f.writeToFile(results);
}
}

}

Its all good, however Im getting a compililing error (C:\Documents and Settings\Sethils\Javafax\src\NewJFrame.java:188: illegal start of expression
public class Foo {)

this appears to be the only thing wrong. Also iv put it as the handling code for the View personal information button, so the start of it looks like this

private void view1ActionPerformed(java.awt.event.ActionEvent evt) {
public class Foo {
// class attributes..
private String[] labels = new String[]
{
"Name", "Address", "City", "State", "Zip Code", "Phone", "Birthdate"
};

could anyone tell me how i might fix the error??

thanks

  Re: Please can someone show me how to make a java box pop upand display personal information!  sam_02 at 04:22 on Monday, August 20, 2007
 

problem ..
fields = new JTextField[labels.length]; there seems to be more..
basic step, create 7 labels and 7 textfields.. 2 buttons, ok, and clear..

important methods:
private void okButtonMouseClicked(MouseEvent e){}
private void clearButtonMouseClicked(MouseEvent e){}
private void writeFile(String strFileName, String strText){}



import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.jgoodies.forms.factories.*;
import com.jgoodies.forms.layout.*;

public class form extends JFrame {
public form() {
initComponents();
}

private void okButtonMouseClicked(MouseEvent e) {
// TODO add your code here

// Error checking - return 0, -1
// 0 if no problems, -1 if missing field or invalid data

// saving data to text file - return 0, -1
// 0 if saved properly, -1 if file corrupt/creation problem

String fileName = "/forum/data.txt";
String deliminator = ":";
String rec_marker = "#"; // place at start and end for calculating no. of records
String record = rec_marker + txt_name.getText() + deliminator +
txt_address.getText() + deliminator +
txt_city.getText() + deliminator +
txt_state.getText() + deliminator +
txt_zip.getText() + deliminator +
txt_phone.getText() + deliminator +
txt_birthday.getText() + rec_marker;

writeFile(fileName, record); // Also, try to check if file exists, then append data, not like this case, it will over-write

}

private void clearButtonMouseClicked(MouseEvent e) {
// TODO add your code here

txt_name.setText("");
txt_address.setText("");
txt_city.setText("");
txt_state.setText("");
txt_zip.setText("");
txt_phone.setText("");
txt_birthday.setText("");

}

private void writeFile(String strFileName, String strText){

boolean flStatus = false;
try {
//Check if the file name is not null and not blank
if (strFileName != null && strFileName.length() > 0) {
java.io.BufferedWriter outStream = new java.io.BufferedWriter(
new java.io.FileWriter(strFileName));

// Write text contents to the file
outStream.write(strText);
outStream.close();
flStatus = true;
}
}
catch (java.io.IOException e) {
System.out.println("Error: Invalid IO operation.");
}

}

public static void main(String args[]){
new form();
}

private void initComponents() {
// Component initialization
dialogPane = new JPanel();
contentPanel = new JPanel();
lbl_name = new JLabel();
txt_name = new JTextField();
lbl_address = new JLabel();
scrollPane1 = new JScrollPane();
txt_address = new JTextArea();
lbl_city = new JLabel();
txt_city = new JTextField();
lbl_state = new JLabel();
txt_state = new JTextField();
lbl_code = new JLabel();
txt_zip = new JTextField();
lbl_phone = new JLabel();
txt_phone = new JTextField();
lbl_birthday = new JLabel();
txt_birthday = new JTextField();
buttonBar = new JPanel();
okButton = new JButton();
clearButton = new JButton();
CellConstraints cc = new CellConstraints();

//======== this ========
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());

//======== dialogPane ========
{
dialogPane.setBorder(Borders.DIALOG_BORDER);
dialogPane.setLayout(new BorderLayout());

//======== contentPanel ========
{
contentPanel.setLayout(new FormLayout(
new ColumnSpec[] {
FormFactory.DEFAULT_COLSPEC,
FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW)
},
new RowSpec[] {
FormFactory.DEFAULT_ROWSPEC,
FormFactory.LINE_GAP_ROWSPEC,
new RowSpec(RowSpec.FILL, Sizes.DEFAULT, FormSpec.DEFAULT_GROW),
FormFactory.LINE_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.LINE_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.LINE_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.LINE_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.LINE_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC
}));

//---- lbl_name ----
lbl_name.setText("Name:");
contentPanel.add(lbl_name, cc.xy(1, 1));
contentPanel.add(txt_name, cc.xy(3, 1));

//---- lbl_address ----
lbl_address.setText("Address:");
contentPanel.add(lbl_address, cc.xy(1, 3));

//======== scrollPane1 ========
{
//---- txt_address ----
txt_address.setWrapStyleWord(true);
txt_address.setLineWrap(true);
scrollPane1.setViewportView(txt_address);
}
contentPanel.add(scrollPane1, cc.xy(3, 3));

//---- lbl_city ----
lbl_city.setText("City:");
contentPanel.add(lbl_city, cc.xy(1, 5));
contentPanel.add(txt_city, cc.xy(3, 5));

//---- lbl_state ----
lbl_state.setText("State:");
contentPanel.add(lbl_state, cc.xy(1, 7));
contentPanel.add(txt_state, cc.xy(3, 7));

//---- lbl_code ----
lbl_code.setText("Zip Code:");
contentPanel.add(lbl_code, cc.xy(1, 9));
contentPanel.add(txt_zip, cc.xy(3, 9));

//---- lbl_phone ----
lbl_phone.setText("Phone:");
contentPanel.add(lbl_phone, cc.xy(1, 11));
contentPanel.add(txt_phone, cc.xy(3, 11));

//---- lbl_birthday ----
lbl_birthday.setText("Birthday:");
contentPanel.add(lbl_birthday, cc.xy(1, 13));
contentPanel.add(txt_birthday, cc.xy(3, 13));
}
dialogPane.add(contentPanel, BorderLayout.CENTER);

//======== buttonBar ========
{
buttonBar.setBorder(Borders.BUTTON_BAR_GAP_BORDER);
buttonBar.setLayout(new FormLayout(
new ColumnSpec[] {
FormFactory.GLUE_COLSPEC,
FormFactory.BUTTON_COLSPEC,
FormFactory.RELATED_GAP_COLSPEC,
FormFactory.BUTTON_COLSPEC
},
RowSpec.decodeSpecs("pref")));

//---- okButton ----
okButton.setText("OK");
okButton.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
okButtonMouseClicked(e);
}
});
buttonBar.add(okButton, cc.xy(2, 1));

//---- clearButton ----
clearButton.setText("Clear");
clearButton.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
clearButtonMouseClicked(e);
}
});
buttonBar.add(clearButton, cc.xy(4, 1));
}
dialogPane.add(buttonBar, BorderLayout.SOUTH);
}
contentPane.add(dialogPane, BorderLayout.CENTER);
pack();
setLocationRelativeTo(getOwner());
setVisible(true);
// End of component initialization
}

// Variables declaration
private JPanel dialogPane;
private JPanel contentPanel;

private JLabel lbl_name;
private JLabel lbl_address;
private JScrollPane scrollPane1;
private JLabel lbl_city;
private JLabel lbl_state;
private JLabel lbl_code;
private JLabel lbl_phone;
private JLabel lbl_birthday;

private static JTextField txt_name;
private static JTextArea txt_address;
private static JTextField txt_city;
private static JTextField txt_state;
private static JTextField txt_zip;
private static JTextField txt_phone;
private static JTextField txt_birthday;

private JPanel buttonBar;
private JButton okButton;
private JButton clearButton;
// End of variables declaration
}


Data will be stored like this:
#Mr. Jones:Someplace over in the states:New York:NY:00000:111111111:15/12/1978#

I didn't have time to check the code, but think it should work if u have the libraries needed.. of course u could also just have a look at the code for reference.. libraries can be downloaded from jgoodies..

I really don't see any error checking, and try to place all the file processing code together, it leaves a mess if u don't do it that way..


  Re: Please can someone show me how to make a java box pop upand display personal information!  sethmtut at 00:03 on Thursday, August 23, 2007
 

Hi, the code looks good but It wont compile. iv been told that the main is defined twice in the same class, there are incompatible types (e.g., trying to assign a JTextArea to a JTextField), trying to reference a non-static variable from a static context, a handful of variables that don't seem to be defined amd a few mismatched braces.

My knowledge is quite limited and would be really greatful if someone could advise me on how to remedy!

thanks


  Re: Please can someone show me how to make a java box pop upand display personal information!  sam_02 at 02:08 on Thursday, August 23, 2007
 

hmm.. the reason it won't compile is because i've used a custom library not found in original java package..


import com.jgoodies.forms.factories.*;
import com.jgoodies.forms.layout.*;


You'll need to download these libraries and add them to your classpath..

OR

You can make a gui interface similar to mine.. just remember you need 7 labels, 7 textfields, 2 buttons, use the actions I set for the buttons in your code..

private void okButtonMouseClicked(MouseEvent e){} // MOUSE ACTION EVENT
private void clearButtonMouseClicked(MouseEvent e){} // MOUSE ACTION EVENT
private void writeFile(String strFileName, String strText){} // CUSTOM METHOD

  Re: Please can someone show me how to make a java box pop upand display personal information!  sethmtut at 02:27 on Thursday, August 23, 2007
 

Hi, Iv tried both, built the GUI just as you said and labled the variables correctly. still cant get rid of that error. Is there any chance you could send me your project folder?

thanks

  Re: Please can someone show me how to make a java box pop upand display personal information!  sam_02 at 02:46 on Friday, August 24, 2007
 

PM me u're email if u want..

  Re: Please can someone show me how to make a java box pop upand display personal information!  sethmtut at 02:49 on Friday, August 24, 2007
 

ok thanks, its da7idoff@msn.com

i have msn messenger too if youv got that










CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  Re: Does anyone know to create or implement a diary into Java??
•  Re: Perl Developers Guide
•  Re: research the search - hash
•  Re: Replacing patterns a^b with pow(a,b)
•  Re: java input/output
•  Re: Beanshell web browser
•  Re: dynamic crystal report generation
•  Re: java app auto web update..
•  Re: Please can someone show me how to make a java box pop upand display personal information!


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2007