I created a database in xml and I want to create a register form in Jframe to add object to the db. So I build up the code, but every time I click the button Registra, it overwrites me that file. How can I keep the file and simply add elements without deleting the others? Here's the code:
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.*;
import java.text.Format;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
public class Registrati extends JFrame {
private JPanel contentPane;
private static JTextField textField;
private static JTextField textField_1;
private static JTextField textField_2;
private static JTextField textField_3;
private static JTextField textField_4;
private static JTextField textField_5;
private static JTextField textField_6;
private static JPasswordField passwordField;
private static JTextField textField_7;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Registrati frame = new Registrati();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Registrati() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 463, 460);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblInserisciITuoi = new JLabel("Inserisci i tuoi dati per registrarti");
lblInserisciITuoi.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblInserisciITuoi.setBounds(156, 23, 215, 14);
contentPane.add(lblInserisciITuoi);
JLabel lblNome = new JLabel("Nome");
lblNome.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblNome.setBounds(38, 58, 46, 14);
contentPane.add(lblNome);
textField = new JTextField();
textField.setBounds(127, 56, 229, 20);
contentPane.add(textField);
textField.setColumns(10);
JLabel lblCognome = new JLabel("Cognome");
lblCognome.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblCognome.setBounds(38, 89, 64, 14);
contentPane.add(lblCognome);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(127, 87, 229, 20);
contentPane.add(textField_1);
JLabel lblDataDiNascita = new JLabel("Data di Nascita");
lblDataDiNascita.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblDataDiNascita.setBounds(38, 120, 98, 14);
contentPane.add(lblDataDiNascita);
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(127, 118, 229, 20);
contentPane.add(textField_2);
JLabel lblAltezza = new JLabel("Altezza");
lblAltezza.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblAltezza.setBounds(38, 171, 64, 14);
contentPane.add(lblAltezza);
textField_3 = new JTextField();
textField_3.setColumns(10);
textField_3.setBounds(127, 169, 229, 20);
contentPane.add(textField_3);
textField_4 = new JTextField();
textField_4.setColumns(10);
textField_4.setBounds(127, 200, 229, 20);
contentPane.add(textField_4);
JLabel lblPeso = new JLabel("Peso");
lblPeso.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblPeso.setBounds(38, 203, 46, 14);
contentPane.add(lblPeso);
JLabel lblSesso = new JLabel("Sesso");
lblSesso.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblSesso.setBounds(38, 233, 46, 14);
contentPane.add(lblSesso);
textField_5 = new JTextField();
textField_5.setColumns(10);
textField_5.setBounds(127, 231, 229, 20);
contentPane.add(textField_5);
JLabel lblEmail = new JLabel("email");
lblEmail.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblEmail.setBounds(38, 283, 46, 14);
contentPane.add(lblEmail);
textField_6 = new JTextField();
textField_6.setColumns(10);
textField_6.setBounds(127, 281, 229, 20);
contentPane.add(textField_6);
JLabel lblPassword = new JLabel("Password");
lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblPassword.setBounds(38, 343, 64, 14);
contentPane.add(lblPassword);
passwordField = new JPasswordField();
passwordField.setBounds(127, 341, 229, 20);
contentPane.add(passwordField);
textField_7 = new JTextField();
textField_7.setColumns(10);
textField_7.setBounds(127, 312, 229, 20);
contentPane.add(textField_7);
JLabel lblUsername = new JLabel("Username");
lblUsername.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblUsername.setBounds(38, 314, 64, 14);
contentPane.add(lblUsername);
JButton btnRegistrami = new JButton("Registrami");
btnRegistrami.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Atleta atleta= new Atleta();
try {
Writer(atleta);
} catch (JDOMException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
btnRegistrami.setBounds(186, 387, 110, 23);
contentPane.add(btnRegistrami);
}
public static void Writer(Atleta atleta) throws JDOMException, IOException {
Document document = null;
Element root = null;
File xmlFile = new File("db.xml");
if(xmlFile.exists()) {
// try to load document from xml file if it exist
// create a file input stream
FileInputStream fis = new FileInputStream(xmlFile);
// create a sax builder to parse the document
SAXBuilder sb = new SAXBuilder();
// parse the xml content provided by the file input stream and create a Document object
document = sb.build(fis);
// get the root element of the document
root = document.getRootElement();
fis.close();
} else {
// if it does not exist create a new document and new root
document = new Document();
root = new Element("Ercole");
}
String nome = textField.getText();
String cognome = textField_1.getText();
#SuppressWarnings("deprecation")
String password = passwordField.getText();
String username = textField_7.getText();
String email = textField_6.getText();
String nascita = textField_2.getText();
String peso = textField_4.getText();
String sesso = textField_5.getText();
String altezza = textField_3.getText();
Element child = new Element("Atleta");
child.addContent(new Element("Password").setText(password));
child.addContent(new Element("Username").setText(username));
child.addContent(new Element("email").setText(email));
child.addContent(new Element("Nome").setText(nome));
child.addContent(new Element("Cognome").setText(cognome));
child.addContent(new Element("ID").setText("da modificare!"));
child.addContent(new Element("Nascita").setText(nascita));
child.addContent(new Element("Peso").setText(peso));
child.addContent(new Element("Sesso").setText(sesso));
child.addContent(new Element("Altezza").setText(altezza));
root.addContent(child);
document.setContent(root);
try {
FileWriter writer = new FileWriter("c:\\db.xml");
XMLOutputter outputter = new XMLOutputter();
//outputter.setFormat(Format.getPrettyFormat());
outputter.output(document, writer);
outputter.output(document, System.out);
writer.close(); // close writer
} catch (IOException e) {
e.printStackTrace();
}
}
}
I think the file you are reading is not the file you are writing
You read "db.xml"
You write "c:\\db.xml"
The first one probably never exists, so it has no content to append your new registration to, so you just end up writing your new registration to the c:\db.xml file
Related
I'm creating a little program for my self to learn Java now I did make a GUI with 12 JTextFields and now I want to use the text that's in the text fields in another class where I print them out.
When I press the button class print is successful, summand I get the last System.out.println("Test if class is triggerd") in my console but I don't get the JTextFields output in the console
Class Frame1
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class Frame1 {
public JFrame frmCratingRecipieGenerator;
public JTextField txtSlot;
public JTextField textField_1;
public JTextField textField_2;
public JTextField textField_3;
public JTextField textField_4;
public JTextField textField_5;
public JTextField textField_6;
public JTextField textField_7;
public JTextField textField_8;
public JTextField textField_9;
public JTextField textField_10;
public JTextField textField_11;
public JLabel lblNewLabel;
public String slot1;
public String slot2;
public String slot3;
public String slot4;
public String slot5;
public String slot6;
public String slot7;
public String slot8;
public String slot9;
public String output;
public String output_count;
public String file_num;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Frame1 window = new Frame1();
window.frmCratingRecipieGenerator.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Frame1() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
public void initialize() {
frmCratingRecipieGenerator = new JFrame();
frmCratingRecipieGenerator.setResizable(false);
frmCratingRecipieGenerator.setTitle("Crating Recipie Generator Alpha 1");
frmCratingRecipieGenerator.getContentPane().setBackground(new Color(192, 192, 192));
frmCratingRecipieGenerator.setBounds(100, 100, 452, 248);
frmCratingRecipieGenerator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmCratingRecipieGenerator.getContentPane().setLayout(null);
txtSlot = new JTextField();
txtSlot.setBounds(10, 19, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(txtSlot);
txtSlot.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(106, 19, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(202, 19, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(10, 50, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_3);
textField_3.setColumns(10);
textField_4 = new JTextField();
textField_4.setBounds(106, 50, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_4);
textField_4.setColumns(10);
textField_5 = new JTextField();
textField_5.setBounds(202, 50, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_5);
textField_5.setColumns(10);
textField_6 = new JTextField();
textField_6.setBounds(10, 81, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_6);
textField_6.setColumns(10);
textField_7 = new JTextField();
textField_7.setBounds(106, 81, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_7);
textField_7.setColumns(10);
textField_8 = new JTextField();
textField_8.setBounds(202, 81, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_8);
textField_8.setColumns(10);
textField_9 = new JTextField();
textField_9.setBounds(351, 19, 86, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_9);
textField_9.setColumns(10);
textField_10 = new JTextField();
textField_10.setBounds(351, 81, 46, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_10);
textField_10.setColumns(10);
textField_11 = new JTextField();
textField_11.setText("1");
textField_11.setBounds(351, 50, 46, 20);
frmCratingRecipieGenerator.getContentPane().add(textField_11);
textField_11.setColumns(10);
JLabel lblFileId = new JLabel("File ID");
lblFileId.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblFileId.setHorizontalAlignment(SwingConstants.LEFT);
lblFileId.setBounds(298, 82, 86, 14);
frmCratingRecipieGenerator.getContentPane().add(lblFileId);
JLabel lblOutput = new JLabel("Output");
lblOutput.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblOutput.setBounds(298, 20, 58, 14);
frmCratingRecipieGenerator.getContentPane().add(lblOutput);
JLabel lblCount = new JLabel("Count");
lblCount.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblCount.setBounds(298, 51, 52, 14);
frmCratingRecipieGenerator.getContentPane().add(lblCount);
JLabel lblThisJavaProgram = new JLabel("This Java program is coded by Jason");
lblThisJavaProgram.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblThisJavaProgram.setBounds(10, 179, 270, 20);
frmCratingRecipieGenerator.getContentPane().add(lblThisJavaProgram);
JLabel TestLabel = new JLabel("");
TestLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
TestLabel.setBounds(391, 179, 46, 19);
frmCratingRecipieGenerator.getContentPane().add(TestLabel);
JButton btnNewButton = new JButton("Generate the McFunction File's");
btnNewButton.setBounds(10, 108, 427, 60);
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
TestLabel.setText(textField_10.getText());
print name = new print();
name.Print();
}
});
frmCratingRecipieGenerator.getContentPane().add(btnNewButton);
lblNewLabel = new JLabel("Last File num");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 16));
lblNewLabel.setBounds(287, 182, 110, 14);
frmCratingRecipieGenerator.getContentPane().add(lblNewLabel);
this.slot1 = txtSlot.getText();
this.slot2 = textField_1.getText();
this.slot3 = textField_2.getText();;
this.slot4 = textField_3.getText();;
this.slot5 = textField_4.getText();;
this.slot6 = textField_5.getText();;
this.slot7 = textField_6.getText();;
this.slot8 = textField_7.getText();;
this.slot9 = textField_8.getText();;
this.output = textField_9.getText();
this.output_count = textField_11.getText();
this.file_num = textField_10.getText();
}
}
Class print
package example;
public class print {
public void Print() {
Frame1 slots = new Frame1();
String slot1 = slots.slot1;
String slot2 = slots.slot2;
String slot3 = slots.slot3;
String slot4 = slots.slot4;
String slot5 = slots.slot5;
String slot6 = slots.slot6;
String slot7 = slots.slot7;
String slot8 = slots.slot8;
String slot9 = slots.slot9;
String output = slots.output;
String output_count = slots.output_count;
String file_num = slots.file_num;
System.out.println(slot1);
System.out.println(slot2);
System.out.println(slot3);
System.out.println(slot4);
System.out.println(slot5);
System.out.println(slot6);
System.out.println(slot7);
System.out.println(slot8);
System.out.println(slot9);
System.out.println(output);
System.out.println(output_count);
System.out.println(file_num);
System.out.println("Test if class is triggerd");
}
}
This is happening because you create on the print class a new Object of frame1 so the variables by default are null. You can add on the Print method a list parameter that stores all the Strings in that list.
public void Print(List<String> slots) {
System.out.println(slots.get(0));
System.out.println(slots.get(1));
System.out.println(slots.get(2));
System.out.println(slots.get(3));
System.out.println(slots.get(4));
System.out.println(slots.get(5));
System.out.println(slots.get(6));
System.out.println(slots.get(7));
System.out.println(slots.get(8));
System.out.println(slots.get(9));
System.out.println(slots.get(10));
System.out.println(slots.get(11));
}
and redefine before the method call all the variables and store them in the list.
You can learn some things about Java's lists here
If you are struggling finding the code I made it for you
slot1 = txtSlot.getText();
slot2 = textField_1.getText();
slot3 = textField_2.getText();;
slot4 = textField_3.getText();;
slot5 = textField_4.getText();;
slot6 = textField_5.getText();;
slot7 = textField_6.getText();;
slot8 = textField_7.getText();;
slot9 = textField_8.getText();;
output = textField_9.getText();
List<String> slots = new ArrayList<String>();
slots.add(slot1);
slots.add(slot2);
slots.add(slot3);
slots.add(slot4);
slots.add(slot5);
slots.add(slot6);
slots.add(slot7);
slots.add(slot8);
slots.add(slot9);
slots.add(output);
slots.add(output_count);
slots.add(file_num);
output_count = textField_11.getText();
file_num = textField_10.getText();
TestLabel.setText(textField_10.getText());
print name = new print();
name.Print(slots);
and Print class:
public void Print(List<String> slots) {
System.out.println(slots.get(0));
System.out.println(slots.get(1));
System.out.println(slots.get(2));
System.out.println(slots.get(3));
System.out.println(slots.get(4));
System.out.println(slots.get(5));
System.out.println(slots.get(6));
System.out.println(slots.get(7));
System.out.println(slots.get(8));
System.out.println(slots.get(9));
System.out.println(slots.get(10));
System.out.println(slots.get(11));
System.out.println("Test if class is triggerd");
}
I made an application that has some TextFields. When I click to search an item it popups JTable that contains a list of items. But the problem is how we can do it. I have tried many times but I failed please help in my project.
this is my code that i have tried
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FocusTraversalPolicy;
import javax.swing.JFrame;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.SwingConstants;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import com.alee.laf.WebLookAndFeel;
import com.toedter.calendar.JDateChooser;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.CardLayout;
import java.awt.Component;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
public class DailySales3 {
private JFrame frame;
private JTextField textField;
private JTextField textField_1;
private JTextField ItemID;
private JTextField ItemName;
private JTextField Stock;
private JTextField Qnty;
private JTextField UnitPrice;
private JTextField Tax;
private JTextField Amount;
private JTextField Discount;
private Connection con;
private String query,auto;
private PreparedStatement PStat;
private ResultSet res;
private DefaultTableModel model,model1;
private JTable table_2;
private JScrollPane scrollPane;
private double AmountGet=0.0,AmountCal=0.0,AmountResult=0.0;
private JTable table;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
WebLookAndFeel.install();
DailySales3 window = new DailySales3();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public DailySales3() {
initialize();
con=Database.Database();
}
public void AmountCalculate()
{
AmountGet=Double.parseDouble(UnitPrice.getText());
AmountCal=Double.parseDouble(Qnty.getText());
AmountResult=AmountCal*AmountGet;
Amount.setText(Double.toString(AmountResult));
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setResizable(false);
frame.setBounds(100, 100, 1229, 658);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel panel = new JPanel();
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(panel, GroupLayout.DEFAULT_SIZE, 1213, Short.MAX_VALUE)
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addComponent(panel, GroupLayout.DEFAULT_SIZE, 630, Short.MAX_VALUE)
);
panel.setLayout(null);
JLabel label = new JLabel("Bill No");
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font("Georgia", Font.BOLD, 12));
label.setBounds(10, 11, 73, 35);
panel.add(label);
textField = new JTextField();
textField.setFont(new Font("Georgia", Font.BOLD, 15));
textField.setEditable(false);
textField.setColumns(10);
textField.setBounds(82, 11, 132, 35);
panel.add(textField);
JLabel label_1 = new JLabel("Bill Date");
label_1.setHorizontalAlignment(SwingConstants.CENTER);
label_1.setFont(new Font("Georgia", Font.BOLD, 12));
label_1.setBounds(224, 11, 73, 35);
panel.add(label_1);
JDateChooser dateChooser = new JDateChooser();
dateChooser.setFont(new Font("Georgia", Font.BOLD, 15));
dateChooser.setBounds(296, 11, 132, 35);
panel.add(dateChooser);
JLabel label_2 = new JLabel("Sales Type");
label_2.setHorizontalAlignment(SwingConstants.CENTER);
label_2.setFont(new Font("Georgia", Font.BOLD, 12));
label_2.setBounds(438, 11, 73, 35);
panel.add(label_2);
JComboBox comboBox = new JComboBox();
comboBox.setFont(new Font("Georgia", Font.BOLD, 15));
comboBox.setBounds(524, 10, 190, 35);
panel.add(comboBox);
JLabel label_3 = new JLabel("Customers");
label_3.setHorizontalAlignment(SwingConstants.CENTER);
label_3.setFont(new Font("Georgia", Font.BOLD, 12));
label_3.setBounds(724, 11, 73, 35);
panel.add(label_3);
textField_1 = new JTextField();
textField_1.setFont(new Font("Georgia", Font.BOLD, 15));
textField_1.setColumns(10);
textField_1.setBounds(797, 12, 210, 35);
panel.add(textField_1);
JButton button = new JButton("");
button.setBounds(1016, 12, 38, 35);
panel.add(button);
JPanel panel_1 = new JPanel();
panel_1.setBounds(10, 68, 997, 43);
panel.add(panel_1);
JLabel lblItemId = new JLabel("Item ID");
lblItemId.setHorizontalAlignment(SwingConstants.CENTER);
lblItemId.setPreferredSize(new Dimension(91, 35));
lblItemId.setFont(new Font("Georgia", Font.BOLD, 12));
panel_1.add(lblItemId);
JLabel lblItemName = new JLabel("Item Name");
lblItemName.setPreferredSize(new Dimension(273, 35));
lblItemName.setHorizontalAlignment(SwingConstants.CENTER);
lblItemName.setFont(new Font("Georgia", Font.BOLD, 12));
panel_1.add(lblItemName);
JLabel lblStock = new JLabel("Stock");
lblStock.setPreferredSize(new Dimension(86, 35));
lblStock.setHorizontalAlignment(SwingConstants.CENTER);
lblStock.setFont(new Font("Georgia", Font.BOLD, 12));
panel_1.add(lblStock);
JLabel lblQnty = new JLabel("Qnty");
lblQnty.setPreferredSize(new Dimension(86, 35));
lblQnty.setHorizontalAlignment(SwingConstants.CENTER);
lblQnty.setFont(new Font("Georgia", Font.BOLD, 12));
panel_1.add(lblQnty);
JLabel lblUnitPrice = new JLabel("Unit Price");
lblUnitPrice.setPreferredSize(new Dimension(123, 35));
lblUnitPrice.setHorizontalAlignment(SwingConstants.CENTER);
lblUnitPrice.setFont(new Font("Georgia", Font.BOLD, 12));
panel_1.add(lblUnitPrice);
JLabel lblTax = new JLabel("Tax (%)");
lblTax.setPreferredSize(new Dimension(86, 35));
lblTax.setHorizontalAlignment(SwingConstants.CENTER);
lblTax.setFont(new Font("Georgia", Font.BOLD, 12));
panel_1.add(lblTax);
JLabel lblDiscount = new JLabel("Discount (%)");
lblDiscount.setPreferredSize(new Dimension(86, 35));
lblDiscount.setHorizontalAlignment(SwingConstants.CENTER);
lblDiscount.setFont(new Font("Georgia", Font.BOLD, 12));
panel_1.add(lblDiscount);
JLabel lblAmount = new JLabel("Amount");
lblAmount.setPreferredSize(new Dimension(123, 35));
lblAmount.setHorizontalAlignment(SwingConstants.CENTER);
lblAmount.setFont(new Font("Georgia", Font.BOLD, 12));
panel_1.add(lblAmount);
JPanel panel_2 = new JPanel();
panel_2.setBounds(10, 112, 997, 43);
panel.add(panel_2);
panel_2.setLayout(null);
ItemID = new JTextField();
ItemID.setBounds(5, 5, 89, 34);
ItemID.setFont(new Font("Georgia", Font.BOLD, 15));
ItemID.setColumns(10);
panel_2.add(ItemID);
ItemName = new JTextField();
ItemName.addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent e) {
if(ItemName.getText().equals(""))
{
while(table_2.getRowCount()>0)
{
model.removeRow(0);
}
}
else if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
table_2.requestFocus();
table_2.changeSelection(0, 0, false, false);
}
else
{
try {
while(table_2.getRowCount()>0)
{
model.removeRow(0);
}
query="Select Item_No,Item_Name,Current_Stock,Unit_Price,Tax,Discount from StockEntry where Item_Name like '"+ItemName.getText().trim()+"%'";
PStat=con.prepareStatement(query);
res=PStat.executeQuery();
while(res.next())
{
String ItemNo=res.getString("Item_No");
String ItemName=res.getString("Item_Name");
double CStock=res.getDouble("Current_Stock");
double PRate=res.getDouble("Unit_Price");
double tax=res.getDouble("Tax");
double discount=res.getDouble("Discount");
Object row[]= {ItemNo,ItemName,CStock,PRate,tax,discount};
model.addRow(row);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
});
ItemName.setBounds(98, 5, 276, 34);
ItemName.setFont(new Font("Georgia", Font.BOLD, 15));
ItemName.setColumns(10);
panel_2.add(ItemName);
Stock = new JTextField();
Stock.setBounds(376, 5, 89, 34);
Stock.setFont(new Font("Georgia", Font.BOLD, 15));
Stock.setColumns(10);
panel_2.add(Stock);
Qnty = new JTextField();
Qnty.addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
model1=(DefaultTableModel)table.getModel();
String itemno=ItemID.getText();
String itemname=ItemName.getText();
String qnty=Qnty.getText();
String unitprice=UnitPrice.getText();
String tax=Tax.getText();
String discount=Discount.getText();
String amount=Amount.getText();
Object rows[]= {itemno,itemname,qnty,unitprice,tax,discount,amount};
model1.addRow(rows);
}
}
});
Qnty.setBounds(469, 5, 89, 34);
Qnty.setFont(new Font("Georgia", Font.BOLD, 15));
Qnty.getDocument().addDocumentListener(new DocumentListener()
{
#Override
public void changedUpdate(DocumentEvent arg0) {
AmountCalculate();
}
#Override
public void insertUpdate(DocumentEvent arg0) {
AmountCalculate();
}
#Override
public void removeUpdate(DocumentEvent arg0) {
}
});
Qnty.setColumns(10);
panel_2.add(Qnty);
UnitPrice = new JTextField();
UnitPrice.setBounds(561, 5, 122, 34);
UnitPrice.setFont(new Font("Georgia", Font.BOLD, 15));
UnitPrice.setColumns(10);
panel_2.add(UnitPrice);
Tax = new JTextField();
Tax.setBounds(687, 5, 89, 34);
Tax.setFont(new Font("Georgia", Font.BOLD, 15));
Tax.setColumns(10);
panel_2.add(Tax);
Discount = new JTextField();
Discount.setBounds(778, 5, 89, 34);
Discount.setFont(new Font("Georgia", Font.BOLD, 15));
Discount.setColumns(10);
panel_2.add(Discount);
Amount = new JTextField();
Amount.setBounds(869, 5, 122, 34);
Amount.setFont(new Font("Georgia", Font.BOLD, 15));
Amount.setColumns(10);
panel_2.add(Amount);
scrollPane = new JScrollPane();
scrollPane.setBounds(10, 156, 997, 412);
panel.add(scrollPane);
table_2 = new JTable();
table_2.addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
try
{
int row=table_2.getSelectedRow();
String TableClicked=(table_2.getModel().getValueAt(row, 0).toString());
query="Select * from StockEntry where Item_No='"+TableClicked+"'";
PStat=con.prepareStatement(query);
res=PStat.executeQuery();
if(res.next())
{
String Itemno=res.getString("Item_No");
ItemID.setText(Itemno);
String itemName=res.getString("Item_Name");
ItemName.setText(itemName);
String PurchaseRate=Double.toString(res.getDouble("Unit_Price"));
UnitPrice.setText(PurchaseRate);
String stock=Double.toString(res.getDouble("Current_Stock"));
Stock.setText(stock);
String tax=Double.toString(res.getDouble("Tax"));
Tax.setText(tax);
String disc=Double.toString(res.getDouble("Discount"));
Discount.setText(disc);
}
}
catch(Exception e1)
{
e1.printStackTrace();
}
finally
{
try
{
PStat.close();
res.close();
}
catch(Exception e2)
{
e2.printStackTrace();
}
}
while(table_2.getRowCount()>0)
{
model.removeRow(0);
}
Qnty.requestFocus();
}
}
});
model=(DefaultTableModel)table_2.getModel();
model.addColumn("Item ID");
model.addColumn("Item Name");
model.addColumn("Qnty");
model.addColumn("Unit Price");
model.addColumn("Tax (%)");
model.addColumn("Discount (%)");
model.addColumn("Amount");
table_2.setRowHeight(30);
table_2.getTableHeader().setFont(new Font("Georgia", Font.BOLD, 14));
table_2.setFont(new Font("Georgia", Font.BOLD, 17));
scrollPane.setViewportView(table_2);
JScrollPane scrollPane_1 = new JScrollPane();
scrollPane_1.setBounds(10, 156, 997, 412);
panel.add(scrollPane_1);
table = new JTable();
model1=(DefaultTableModel)table.getModel();
model1.addColumn("Item ID");
model1.addColumn("Item Name");
model1.addColumn("Qnty");
model1.addColumn("Unit Price");
model1.addColumn("Tax (%)");
model1.addColumn("Discount (%)");
model1.addColumn("Amount");
table.setRowHeight(30);
table.setFont(new Font("Georgia", Font.BOLD, 17));
scrollPane_1.setViewportView(table);
frame.getContentPane().setLayout(groupLayout);
}
}
When I try to add some function to get data from jtext field under public void actionPerformed(ActionEvent arg0) My GUI goes blank I have attached the images below I can't figure out what's wrong with it.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Register extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JLabel lblNewLabel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Register frame = new Register();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Register() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblName = new JLabel("Name");
lblName.setBounds(56, 56, 56, 16);
contentPane.add(lblName);
JLabel lblUsername = new JLabel("Username");
lblUsername.setBounds(56, 85, 77, 16);
contentPane.add(lblUsername);
JLabel lblPassword = new JLabel("Password");
lblPassword.setBounds(56, 114, 56, 16);
contentPane.add(lblPassword);
JLabel lblAge = new JLabel("Age");
lblAge.setBounds(56, 143, 56, 16);
contentPane.add(lblAge);
JLabel lblGender = new JLabel("Gender");
lblGender.setBounds(56, 172, 56, 16);
contentPane.add(lblGender);
textField = new JTextField();
textField.setBounds(130, 53, 116, 19);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(130, 82, 116, 22);
contentPane.add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(130, 111, 116, 22);
contentPane.add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(130, 140, 116, 22);
contentPane.add(textField_3);
textField_3.setColumns(10);
textField_4 = new JTextField();
textField_4.setBounds(130, 169, 116, 22);
contentPane.add(textField_4);
textField_4.setColumns(10);
JButton btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
btnSubmit.setBounds(169, 215, 97, 25);
contentPane.add(btnSubmit);
lblNewLabel = new JLabel("New label");
lblNewLabel.setBounds(330, 56, 56, 16);
contentPane.add(lblNewLabel);
}
}
EDIT 1: Added String name to action just to get name.
JButton btnSubmit = new JButton("Submit");
btnSubmit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String Name;
}
});
and This happened
GUI went blank:
You're using an absolute layout. Since you can't pack() the window, you need to at least validate() its contents.
Register frame = new Register();
frame.validate();
frame.setVisible(true);
Now you can see the real problem: you picked bounds for lblPassword and lblNewLabel that cut off the text on my computer. You really need to use a layout, maybe like this.
i write a convert temperature code using radiobutton, but when i use actionListener to print the value to a label, it show "Cannot refer to the non-final local variable valueto defined in an enclosing scope"
can some one show me the problem and how to fix it? (also sorry for my English)
the Radiobutton
JRadioButton Cbutton = new JRadioButton("Celsius");
Fbutton.setFont(new Font("Tahoma", Font.PLAIN, 12));
Fbutton.setBounds(10, 40, 109, 23);
contentPane.add(Cbutton);
JRadioButton Fbutton2 = new JRadioButton("Fahrenheit");
Fbutton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText(String.valueOf(valueto));
}
});
i use this to convert
if(Fbutton.isSelected()&&Cbutton2.isSelected()){
value=F;
valueto=(F-32)/1.8;
}else if(Fbutton.isSelected()&&Kbutton2.isSelected()){
value=F;
valueto=(F+459.67)*5/9;
}else if(Cbutton.isSelected()&&Fbutton2.isSelected()){
value=C;
valueto=C*1.8+32;
}else if(Cbutton.isSelected()&&Kbutton2.isSelected()){
value=C;
valueto=C+273.15;
}else if(Kbutton.isSelected()&&Cbutton2.isSelected()){
value=K;
valueto=K-273.15;
}else if(Kbutton.isSelected()&&Fbutton2.isSelected()){
value=K;
valueto=K*9.5-459.67;
}
Edit : i declare valueto at the bottom of the class
double value = 0;
double valueto = 0;
double F = 0, C = 0, K = 0;
it ask me to add final to "double valueto", but when i add it the valueto in "if" become error
The Whole code
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
public class CtoKtoF extends JFrame {
private JPanel contentPane;
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CtoKtoF frame = new CtoKtoF();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
* #return
*/
public CtoKtoF() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 303);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
double value = 0 ;
double valueto = 0;
double F = 0, C = 0, K = 0;
Border border = LineBorder.createGrayLineBorder();//make border for Jlabel
final JLabel label = new JLabel("New label");
label.setFont(new Font("Tahoma", Font.PLAIN, 12));
label.setBounds(10, 228, 220, 22);
label.setBorder(border);
contentPane.add(label);
JLabel lblNewLabel = new JLabel("Convert from");
lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblNewLabel.setBounds(10, 11, 92, 22);
contentPane.add(lblNewLabel);
JRadioButton Fbutton = new JRadioButton("Fahrenheit");
Fbutton.setFont(new Font("Tahoma", Font.PLAIN, 12));
Fbutton.setBounds(10, 40, 109, 23);
//Fbutton.setActionCommand("F");
contentPane.add(Fbutton);
JRadioButton Cbutton = new JRadioButton("Celcius");
Cbutton.setFont(new Font("Tahoma", Font.PLAIN, 12));
Cbutton.setBounds(121, 40, 109, 23);
//Cbutton.setActionCommand("C");
contentPane.add(Cbutton);
JRadioButton Kbutton = new JRadioButton("Kelvin");
Kbutton.setFont(new Font("Tahoma", Font.PLAIN, 12));
Kbutton.setBounds(232, 40, 109, 23);
//Kbutton.setActionCommand("K");
contentPane.add(Kbutton);
JLabel lblEnterNumericTemperature = new JLabel("Enter Numeric Temperature");
lblEnterNumericTemperature.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblEnterNumericTemperature.setBounds(10, 70, 238, 22);
contentPane.add(lblEnterNumericTemperature);
textField = new JTextField();
textField.setFont(new Font("Tahoma", Font.PLAIN, 12));
textField.setBounds(10, 103, 220, 22);
textField.setText(String.valueOf(value));
contentPane.add(textField);
textField.setColumns(10);
JRadioButton Fbutton2 = new JRadioButton("Fahrenheit");
Fbutton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText(String.valueOf(valueto));
}
});
Fbutton2.setFont(new Font("Tahoma", Font.PLAIN, 12));
Fbutton2.setBounds(10, 165, 109, 23);
//Fbutton2.setActionCommand("F2");
contentPane.add(Fbutton2);
JRadioButton Cbutton2 = new JRadioButton("Celcius");
Cbutton2.setFont(new Font("Tahoma", Font.PLAIN, 12));
Cbutton2.setBounds(121, 165, 109, 23);
//Cbutton2.setActionCommand("C2");
contentPane.add(Cbutton2);
JRadioButton Kbutton2 = new JRadioButton("Kelvin");
Kbutton2.setFont(new Font("Tahoma", Font.PLAIN, 12));
Kbutton2.setBounds(232, 165, 109, 23);
//Kbutton2.setActionCommand("K2");
contentPane.add(Kbutton2);
ButtonGroup group1 = new ButtonGroup();
group1.add(Fbutton);
group1.add(Kbutton);
group1.add(Cbutton);
ButtonGroup group2 = new ButtonGroup();
group2.add(Fbutton2);
group2.add(Cbutton2);
group2.add(Kbutton2);
/*Fbutton.addActionListener(this);
Cbutton.addActionListener(this);
Kbutton.addActionListener(this);
Fbutton2.addActionListener(this);
Cbutton2.addActionListener(this);
Kbutton2.addActionListener(this);*/
//boolean checked = Fbutton.getState();
JLabel lblConvertTo = new JLabel("Convert to");
lblConvertTo.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblConvertTo.setBounds(10, 136, 92, 22);
contentPane.add(lblConvertTo);
JLabel lblComparableTemperatureIs = new JLabel("Comparable Temperature is");
lblComparableTemperatureIs.setFont(new Font("Tahoma", Font.PLAIN, 12));
lblComparableTemperatureIs.setBounds(10, 195, 238, 22);
contentPane.add(lblComparableTemperatureIs);
if(Fbutton.isSelected()&&Cbutton2.isSelected()){
value=F;
valueto=(F-32)/1.8;
}else if(Fbutton.isSelected()&&Kbutton2.isSelected()){
value=F;
valueto=(F+459.67)*5/9;
}else if(Cbutton.isSelected()&&Fbutton2.isSelected()){
value=C;
valueto=C*1.8+32;
}else if(Cbutton.isSelected()&&Kbutton2.isSelected()){
value=C;
valueto=C+273.15;
}else if(Kbutton.isSelected()&&Cbutton2.isSelected()){
value=K;
valueto=K-273.15;
}else if(Kbutton.isSelected()&&Fbutton2.isSelected()){
value=K;
valueto=K*9.5-459.67;
}
}
}
I am making a program for my friend. I ran into an error while trying to create a write and save text into a .txt file. I should note this is the first time I have ever created something in Java Swing.
The error is marked with an asterisk.
package com.laganstoop.me;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Scanner;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
// Created by: Laganstoop (David L. Perez)
// Do Not Distribute!
public class Main implements ActionListener {
private JFrame frame;
public static String infoBox(String infoMessage, String titleBar)
{
JOptionPane.showMessageDialog(null, infoMessage, "" + titleBar, JOptionPane.INFORMATION_MESSAGE);
return "";
}
public static void errorMessage(String errorMessage, String titleBar)
{
JOptionPane.showMessageDialog(null, errorMessage, "" + titleBar, JOptionPane.INFORMATION_MESSAGE);
}
public static final int HEIGHT = 800;
public static final int WIDTH = 600;
public static final int nHEIGHT = 400;
public static final int nWIDTH = 300;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main window = new Main();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Main() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.getContentPane().setBackground(Color.LIGHT_GRAY);
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(HEIGHT, WIDTH);
JButton btnNewButton = new JButton("Click To Add New");
btnNewButton.setBounds(10, 11, 132, 23);
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
} catch (InstantiationException e1) {
e1.printStackTrace();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
} catch (UnsupportedLookAndFeelException e1) {
e1.printStackTrace();
}
frame.getContentPane().setLayout(null);
frame.getContentPane().add(btnNewButton);
JPanel panel = new JPanel();
panel.setBounds(10, 11, 764, 181);
panel.setBackground(Color.WHITE);
panel.setForeground(Color.WHITE);
frame.getContentPane().add(panel);
panel.setLayout(null);
panel.hide();
JLabel lblName = new JLabel("Name:");
lblName.setFont(new Font("Yu Gothic", Font.BOLD, 13));
lblName.setBounds(10, 11, 46, 14);
panel.add(lblName);
JLabel lblDate = new JLabel("Date:");
lblDate.setFont(new Font("Yu Gothic", Font.BOLD, 13));
lblDate.setBounds(10, 36, 46, 14);
panel.add(lblDate);
JLabel lblLocation = new JLabel("Time:\r\n");
lblLocation.setFont(new Font("Yu Gothic", Font.BOLD, 13));
lblLocation.setBounds(10, 61, 60, 14);
panel.add(lblLocation);
JLabel lblLocation_1 = new JLabel("Location:");
lblLocation_1.setFont(new Font("Yu Gothic", Font.BOLD, 13));
lblLocation_1.setBounds(10, 86, 60, 14);
panel.add(lblLocation_1);
JLabel lblRe = new JLabel("Referee");
lblRe.setFont(new Font("Yu Gothic", Font.BOLD, 13));
lblRe.setBounds(10, 111, 60, 14);
panel.add(lblRe);
textField = new JTextField();
textField.setBounds(76, 8, 127, 20);
panel.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(76, 33, 127, 20);
panel.add(textField_1);
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(76, 58, 127, 20);
panel.add(textField_2);
textField_3 = new JTextField();
textField_3.setColumns(10);
textField_3.setBounds(76, 86, 127, 20);
panel.add(textField_3);
textField_4 = new JTextField();
textField_4.setColumns(10);
textField_4.setBounds(76, 108, 127, 20);
panel.add(textField_4);
JCheckBox checkBox = new JCheckBox("");
checkBox.setBounds(209, 7, 21, 23);
panel.add(checkBox);
JCheckBox checkBox_1 = new JCheckBox("");
checkBox_1.setBounds(209, 32, 21, 23);
panel.add(checkBox_1);
JCheckBox checkBox_2 = new JCheckBox("");
checkBox_2.setBounds(209, 57, 21, 23);
panel.add(checkBox_2);
JCheckBox checkBox_3 = new JCheckBox("");
checkBox_3.setBounds(209, 82, 21, 23);
panel.add(checkBox_3);
JCheckBox checkBox_4 = new JCheckBox("");
checkBox_4.setBounds(209, 107, 21, 23);
panel.add(checkBox_4);
JPanel panel_2 = new JPanel();
panel_2.setBounds(20, 290, 358, 210);
panel_2.setLayout(null);
JButton btnNewNote = new JButton("New Note");
btnNewNote.setFont(new Font("Yu Gothic", Font.BOLD, 16));
btnNewNote.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFrame noteFrame = new JFrame();
noteFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
noteFrame.setLocationRelativeTo(panel);
noteFrame.pack();
noteFrame.setSize(nHEIGHT, nWIDTH);
noteFrame.getContentPane().add(panel_2);
noteFrame.setVisible(true);
}
});
btnNewNote.setBounds(293, 16, 461, 151);
panel.add(btnNewNote);
JLabel lblNotes = new JLabel("Notes:");
lblNotes.setBounds(10, 0, 46, 23);
panel_2.add(lblNotes);
lblNotes.setFont(new Font("Yu Gothic", Font.BOLD, 13));
JButton btnSaveCreate = new JButton("Create & Save");
btnSaveCreate.setBounds(237, 177, 131, 23);
panel_2.add(btnSaveCreate);
JCheckBox checkBox_5 = new JCheckBox("");
checkBox_5.setBounds(210, 177, 21, 23);
panel_2.add(checkBox_5);
btnSaveCreate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String NAME = textField.getText();
String DATE = textField_1.getText();
String TIME = textField_2.getText();
String LOCATION = textField_3.getText();
String Referee = textField_4.getText();
if(checkBox.isSelected() && checkBox_1.isSelected() && checkBox_2.isSelected() && checkBox_2.isSelected() && checkBox_3.isSelected() && checkBox_4.isSelected() && checkBox_5.isSelected())
{
infoBox("Met with " + NAME + " on " + DATE + " #" + TIME + ", and went to " + LOCATION + ", then I was introduced by " + Referee, "Paragraph");
}
else {
errorMessage("Error: Please validate entries!", "Error!");
}
}
});
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
btnNewButton.hide();
panel.setVisible(true);
}
});
try {
String content = textField_5.getText();
File file = new File("/users/David/filename.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content); //step2: write it
bw.close();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace(); **** *****
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
}
}
Any Help is greatly appreciated!
You just forgot to add the } in this statement:
catch (IOException e) {
e.printStackTrace();
}
Just as Andrew Thompson said: simple indentation would have made this error a lot easier to spot if done properly, and should be used as common practice.