Is it possible to use file creation code inside actionPerformed class? - java

I am new to JAVA. I am trying to make gui for login and registration form. at the moment, I am focusing on registration. What I want is to save data created inside actionPerformed class into text form. For this, I put the file creation code inside actionPerformed class
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == sub) {
if (term.isSelected()) {
String data1;
String data
= "Name : "
+ tname.getText() + "\n"
+ "Mobile : "
+ tmno.getText() + "\n";
if (male.isSelected())
data1 = "Gender : Male"
+ "\n";
else
data1 = "Gender : Female"
+ "\n";
String data2
= "DOB : "
+ (String)date.getSelectedItem()
+ "/" + (String)month.getSelectedItem()
+ "/" + (String)year.getSelectedItem()
+ "\n";
String data3 = "Address : " + tadd.getText();
tout.setText(data + data1 + data2 + data3);
tout.setEditable(false);
res.setText("Registration Successfully..");
String txt_path = "C:\\FMP\\Users.txt";
File file = new File(txt_path);
FileWriter filewriter = new FileWriter(file, true);
filewriter.write(data + data1 + data2 + data3);
filewriter.flush();
filewriter.close();
}
else {
tout.setText("");
resadd.setText("");
res.setText("Please accept the"
+ " terms & conditions..");
}
}
I run this code, and I got nothing by clicking sub(which is button), it's supposed to be saved in .txt (C:\FMP\Users.txt)). the data is shown on Text, but not saved.

Related

JOptionPane is getting trunkcated on only one JOptionPane but not the other

I have two buttons that are supposed to open JOptionPanes. One of them does not format to the right size. The code looks the same to me. What am I doing wrong here?
This one does not display all the text in the JTextArea:
public static void listAllTransactions(){
DecimalFormat fmt = new DecimalFormat("0.00");
String message = "List All Transactions \n";
message += "Name: " + ca.getName() + "\n\n";
message += "ID\tType\tAmount\n";
for (Transaction t : ca.transactions){
message += t.getTransNumber() + "\t" +
HelperTransactionIds.getTransactionName(t.getTransIdType()) + "\t$" +
fmt.format(t.getTransAmount()) + "\n";
}
JTextArea jTextArea = new JTextArea(message);
jTextArea.setEditable(false);
jTextArea.setBackground(Color.LIGHT_GRAY);
JOptionPane.showMessageDialog(null,jTextArea);
}
The other one works as intended.
The code looks the same to me but I could be missing something:
public static void listChecks(){
DecimalFormat fmt = new DecimalFormat("0.00");
String message = "List All Checks \n";
message += "Name: " + ca.getName() + "\n\n";
message += "ID\tCheck\tAmount\n";
for (Transaction t : ca.transactions){
if (t.getTransIdType() == HelperTransactionIds.CHECK){
Check myCheck = (Check) t;
message += t.getTransNumber() + "\t" +myCheck.getNumber() + "\t$" + fmt.format(t.getTransAmount()) + "\n";
}
}
JTextArea jTextArea = new JTextArea(message);
jTextArea.setEditable(false);
jTextArea.setBackground(Color.LIGHT_GRAY);
JOptionPane.showMessageDialog(null, jTextArea);
}

How do I output data from a text file to a JTable and have it refresh when the text file is edited?

This is where the data is coming from:
private void btnPlaceOrderActionPerformed(java.awt.event.ActionEvent evt) {
String name = txtClientName.getText();
String address = txtClientAddress.getText();
String date = txtDeliveryDate.getText();
String contact = txtContactInfo.getText();
String small = txtSmall.getText();
String medium = txtMedium.getText();
String large = txtLarge.getText();
BufferedWriter buf;
try{
buf = new BufferedWriter(new FileWriter("orders.txt", true));
buf.write(name + " " + address + " " + small + " " + medium + " " + large + " " + date + " " + contact);
buf.newLine();
buf.close();
JOptionPane.showMessageDialog(this, "Order sent");
} catch (Exception e){
}
}
How would I display the data that gets collected from this into a JTable that updates whenever new data is introduced to the text file?

How to loop writer .csv?

I want every data to be retrieved once looping will occur in the CSV writer, how do I make the loop occur?
thanks!
Data from Firebase
Output writer
*sorry, i can't display images 'cause it must have 10 reputations :(
try {
File root = new File(Environment.getExternalStorageDirectory() + "/StockCount/");
if (!root.exists()) {
root.mkdirs();
}
File myCSV = new File(root, currentDate + " DataStockCount.csv");
myCSV.createNewFile();
FileWriter writer = new FileWriter(myCSV);
writer.append("Date : " + getDate + "\n");
writer.append("Inspector : " + getInspector + "\n");
writer.append("Location : " + getLocation + "\n");
writer.append("Product Name : " + getProductName + "\n");
writer.append("Price : " + getPrice + "\n");
writer.append("Quantity : " + getAmount + "\n");
writer.append("Barcode : " + getCode + "\n");
writer.flush();
writer.close();
Toast.makeText(this, "File Saved Successfully!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Error : " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
Why not using an existing library like FastCSV?

how to generate QR code using velocity template

I am using velocity template with java to create email content . i need to add a QR code in my email template, How do i generate QR code using velocity template ?
Velocity generates text. A QR Code is an image. Basically, Velocity can generate an <img src=...> tag, but not the QR code itself.
You will have to use a QR code generator like QRGen to generate the QR code.
String Cus_Name = (String) cmbCus_NameAdd.getSelectedItem();
String Cus_Id = lblCus_IDAdd2.getText();
String Odr_No = lblOrderNoAdd2.getText();
String Matir = lblMeterialAdd2.getText();
String amount = txtNoOfProductAdd.getText();
String date = lblDateAdd2.getText();
String time = lblTimeAdd2.getText();
String place = (String) cmbPlaceAdd.getSelectedItem();
String newLine = System.getProperty("line.separator");
String Details = "Customer Name - " + Cus_Name + "" + newLine + " Customer ID - " + Cus_Id + "" + newLine + " Order No - " + Odr_No + "" + newLine + " Material - " + Matir + "" + newLine + " Amount - " + amount + "" + newLine + " Date - " + date + "" + newLine + " Time - " + time + "" + newLine + " Place - " + place + "";
ByteArrayOutputStream out = QRCode.from(Details).to(ImageType.JPG).stream(); // this line creates QR code
File f = new File("C:\\Users\\Pulasthi Dinusha\\Desktop\\MainGui\\lib\\QR_Generator_Libs\\" + Odr_No + ".jpg");
FileOutputStream fos;
try {
fos = new FileOutputStream(f);
fos.write(out.toByteArray());
fos.flush();
} catch (FileNotFoundException ex) {
Logger.getLogger(test2.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(test2.class.getName()).log(Level.SEVERE, null, ex);
}
ImageIcon iconLogo = new ImageIcon("C:\\Users\\Pulasthi Dinusha\\Desktop\\MainGui\\lib\\QR_Generator_Libs\\" + Odr_No + ".jpg");
// In init() method write this code
lblQRcodeAdd.setIcon(iconLogo);

Generate vCard from Contact Data by java

I would like to be able to generate a vCard given the data contained within person_contact.
Here is my code.But I am unable to generate the vcard properly.
private void m1() throws IOException {
Contact contact = buildContact();
File f = new File("contact.vcf");
FileOutputStream fop = new FileOutputStream(f);
String outputImageFilePath = "contact.vcf";
createDirectoryIfNotExists(outputImageFilePath);
if (f.exists()) {
String str = "BEGIN:VCARD\n" + "VERSION:4.0\n" + "N:" + contact.getFull_name() + ";;;\n"
+ "FN:" + contact.getFull_name() + "\n" + "ORG:" + contact.getOrganization_name()
+ "\n" + "TITLE:" + contact.getTitle() + "\n" + "TEL;TYPE="
+ contact.getPhone_2_type() + ";VALUE=uri:" + contact.getPhone_2() + "\n"
+ "TEL;TYPE=" + contact.getPhone_3_type() + ",voice;VALUE=uri:tel:"
+ contact.getPhone_3() + "\n" + "EMAIL:" + contact.getEmail() + "\n" + "FAX:"
+ contact.getFax() + "\n" + "STREET1:" + contact.getStreet1() + "\n" + "STREET2:"
+ contact.getStreet2() + "\n" + "CITY:" + contact.getCity() + "\n" + "STATE:"
+ contact.getState() + "\n"
+ "END:VCARD";
fop.write(str.getBytes());
// Now read the content of the vCard after writing data into it
BufferedReader br = null;
String sCurrentLine;
br = new BufferedReader(new FileReader("contact.vcf"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
System.out.println(f.getAbsolutePath());
}
// close the output stream and buffer reader
fop.flush();
fop.close();
System.out.println("The data has been written");
}
else
System.out.println("This file does not exist");
}
private static Contact buildContact() {
Contact contact = new Contact();
contact.setCity("xxxxxx");
contact.setCountry("xxxxxx");
contact.setEmail("test#xxxxxx.com");
contact.setFax("123456789");
contact.setFull_name("Dummy Name");
contact.setOrganization_name("Dummy-technologies");
contact.setPhone("123456789");
contact.setPhone_type("work");
contact.setPhone_2("987456321");
contact.setPhone_2_type("home");
contact.setPhone_3("564789451236");
contact.setPhone_3_type("work2");
contact.setPostal_code("500000");
contact.setState("state");
contact.setStreet1("street1");
contact.setStreet2("stree2");
contact.setTitle("company");
contact.setWebsite_url("www.text.com");
return contact;
}
It is not generating the vcard properly.Can any one help me plz.

Categories