Results 1 to 10 of 10
- 05-23-2012, 04:25 AM #1
Member
- Join Date
- May 2012
- Posts
- 4
- Rep Power
- 0
Help....add actionListener to buttons
Hi all, I have been trying to figure this out for a while and am having some trouble with it. This is my 6th attempt at working the code to make it work. What I am trying to do is add actionListeners to my buttons to do various things. This is where I am having problems. Here is what I need to do:
On my first frame (listFrame):
JButton addBut - this should open new JFrame (contactFrame)
JButton viewBut - this should read from a file and put information into columns (not sure about this one)
JButton exit - closes the program (this one I have)
on my second frame (contactFrame):
JButton save - this should write information from jtextfields to a file (not sure about this one)
JButton reset - this should clear form (this one I have)
JButton close - this should close this frame (I think I have this one)
With the code as it is right now, these are the error messages that I get:Java Code://import packages import java.awt.*; import java.awt.event.*; import java.text.*; import java.io.*; import java.util.*; import javax.swing.*; import javax.swing.text.*; //create class public class Contact_Information { //create main frame public void list() { JFrame listFrame = new JFrame("Contact Information Program"); listFrame.setVisible(true); listFrame.setSize(300, 500); listFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel buttons1 = new JPanel(); JPanel columnHeaders = new JPanel(); JPanel contactInfo = new JPanel(); JButton addBut = new JButton("Add New Contact"); JButton viewBut = new JButton("View Contacts"); JButton exit = new JButton("Exit"); JLabel name = new JLabel("Contact Name"); name.setFont(new Font("Times New Roman", Font.BOLD, 12)); JLabel old = new JLabel("Age"); old.setFont(new Font("Times New Roman", Font.BOLD, 12)); JLabel cell = new JLabel("Cell Phone"); cell.setFont(new Font("Times New Roman", Font.BOLD, 12)); JLabel emailA = new JLabel("Email Address"); emailA.setFont(new Font("Times New Roman", Font.BOLD, 12)); JLabel contactList = new JLabel("Contact Information:"); listFrame.add(buttons1); buttons1.add(addBut); buttons1.add(viewBut); buttons1.add(exit); listFrame.add(columnHeaders); columnHeaders.add(name); columnHeaders.add(old); columnHeaders.add(cell); columnHeaders.add(emailA); listFrame.add(contactInfo); contactInfo.add(contactList); //add action listeners //opens new JFrame addBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent aci) { if (aci.getSource() == addBut) { contactForm().show(); } } }); /* //reads from file viewBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent vcl) { //reads from file } }); */ //exits program exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ep) { System.exit(0); } }); } //end public void list frame //create second frame public void contact() { JFrame contactFrame = new JFrame("Add New Contact"); list.setVisible(true); list.setSize(300, 500); list.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); Font bold = new Font(Font.BOLD); MaskFormatter mask = null; try { mask = new MaskFormatter("(###) ###-####"); mask.setPlaceholderCharacter('_'); } catch (ParseException e) { e.printStackTrace(); } JPanel buttons2 = new JPanel(); JPanel contactForm = new JPanel(); JButton save = new JButton("Save Contact"); JButton reset = new JButton("Reset Form"); JButton close = new JButton("Close Contact Form"); JLabel fName = new JLabel("First Name:"); JLabel lName = new JLabel("Last Name:"); JLabel cAge = new JLabel("Age:"); JLabel cPhone = new JLabel("Cell Phone:"); JLabel eAddress = new JLabel("Email Address:"); JTextField firstName = new JTextField(10); JTextField lastName = new JTextField(10); JTextField age = new JTextField(3); JFormattedTextField phoneNumb = new JFormattedTextField(mask); JTextField email = new JTextField(40); contactFrame.add(buttons2); buttons2.add(save); buttons2.add(reset); buttons2.add(close); contactFrame.add(contactForm); contactForm.add(fName); contactForm.add(firstName); contactForm.add(lName); contactForm.add(lastName); contactForm.add(cAge); contactForm.add(age); contactForm.add(cPhone); contactForm.add(phoneNumb); contactForm.add(eAddress); contactForm.add(email); //add action listeners //writes to file save.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent sci) { String name = new String(firstName.getText(), lastName.getText()); String ageC = new String(age.getText()); String cell = new String(phoneNumb.getText()); String emailA = new String(email.getText()); String saveTo = new String(name, ageC, cell, emailA); try { File saveTo = new File("contacts.txt"); FileOutputStream fileStream = new FileOutputStream(saveTo); write(fileStream); } catch (IOException ioe) { System.out.println("Could not save contact to file"); } } void write(FileOutputStream stream, String output) throws IOEception { output = output + newline; byte[] data = output.getBytes(); strea.write(data, 0, data.length); } }); //clears form reset.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent rcf) { contactFrame.firstName.setText(null); contactFrame.lastName.setText(null); contactFrame.age.setText(null); contactFrame.phoneNumb.setText(null); contactFrame.email.setText(null); } }); //close 2nd JFrame close.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ccl) { contactFrame.dispose(); } }); } //end public void contact frame } //end public class Contact_Information
Contact_Information.java:55: error: local variable addBut is accessed from within inner class; needs to be declared final
if (aci.getSource() == addBut) {
^
Contact_Information.java:56: error: cannot find symbol
contactForm().show();
^
symbol: method contactForm()
Contact_Information.java:83: error: cannot find symbol
list.setVisible(true);
^
symbol: variable list
location: class Contact_Information
Contact_Information.java:84: error: cannot find symbol
list.setSize(300, 500);
^
symbol: variable list
location: class Contact_Information
Contact_Information.java:85: error: cannot find symbol
list.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE );
^
symbol: variable list
location: class Contact_Information
Contact_Information.java:86: error: no suitable constructor found for Font(int)
Font bold = new Font(Font.BOLD);
^
constructor Font.Font(Font) is not applicable
(actual argument int cannot be converted to Font by method invocation conversion)
constructor Font.Font(Map<? extends Attribute,?>) is not applicable
(actual argument int cannot be converted to Map<? extends Attribute,?> by method invocation conversion)
constructor Font.Font(AttributeValues,String,int,boolean,Font2 DHandle) is not applicable
(actual and formal argument lists differ in length)
constructor Font.Font(File,int,boolean,CreatedFontTracker) is not applicable
(actual and formal argument lists differ in length)
constructor Font.Font(String,int,float,boolean,Font2DHandle) is not applicable
(actual and formal argument lists differ in length)
constructor Font.Font(String,int,float) is not applicable
(actual and formal argument lists differ in length)
constructor Font.Font(String,int,int) is not applicable
(actual and formal argument lists differ in length)
Contact_Information.java:156: error: cannot find symbol
throws IOEception {
^
symbol: class IOEception
Contact_Information.java:138: error: local variable firstName is accessed from within inner class; needs to be declared final
String name = new String(firstName.getText(), lastName.getText());
^
Contact_Information.java:138: error: local variable lastName is accessed from within inner class; needs to be declared final
String name = new String(firstName.getText(), lastName.getText());
^
Contact_Information.java:138: error: no suitable constructor found for String(String,String)
String name = new String(firstName.getText(), lastName.getText());
^
constructor String.String(int,int,char[]) is not applicable
(actual and formal argument lists differ in length)
constructor String.String(StringBuilder) is not applicable
(actual and formal argument lists differ in length)
constructor String.String(StringBuffer) is not applicable
(actual and formal argument lists differ in length)
constructor String.String(byte[]) is not applicable
(actual and formal argument lists differ in length)
constructor String.String(byte[],int,int) is not applicable
(actual and formal argument lists differ in length)
constructor String.String(byte[],Charset) is not applicable
(actual argument String cannot be converted to byte[] by method invocation conversion)
constructor String.String(byte[],String) is not applicable
(actual argument String cannot be converted to byte[] by method invocation conversion)
constructor String.String(byte[],int,int,Charset) is not applicable
(actual and formal argument lists differ in length)
constructor String.String(byte[],int,int,String) is not applicable
(actual and formal argument lists differ in length)
constructor String.String(byte[],int) is not applicable
(actual argument String cannot be converted to byte[] by method invocation conversion)
constructor String.String(byte[],int,int,int) is not applicable
(actual and formal argument lists differ in length)
constructor String.String(int[],int,int) is not applicable
(actual and formal argument lists differ in length)
constructor String.String(char[],int,int) is not applicable
(actual and formal argument lists differ in length)
constructor String.String(char[]) is not applicable
(actual and formal argument lists differ in length)
constructor String.String(String) is not applicable
(actual and formal argument lists differ in length)
constructor String.String() is not applicable
(actual and formal argument lists differ in length)
Contact_Information.java:139: error: local variable age is accessed from within inner class; needs to be declared final
String ageC = new String(age.getText());
^
Contact_Information.java:140: error: local variable phoneNumb is accessed from within inner class; needs to be declared final
String cell = new String(phoneNumb.getText());
^
Contact_Information.java:141: error: local variable email is accessed from within inner class; needs to be declared final
String emailA = new String(email.getText());
^
Contact_Information.java:143: error: no suitable constructor found for String(String,String,String,String)
String saveTo = new String(name, ageC, cell, emailA);
^
constructor String.String(int,int,char[]) is not applicable
(actual and formal argument lists differ in length)
constructor String.String(StringBuilder) is not applicable
(actual and formal argument lists differ in length)
constructor String.String(StringBuffer) is not applicable
(actual and formal argument lists differ in length)
constructor String.String(byte[]) is not applicable
(actual and formal argument lists differ in length)
constructor String.String(byte[],int,int) is not applicable
(actual and formal argument lists differ in length)
constructor String.String(byte[],Charset) is not applicable
(actual and formal argument lists differ in length)
constructor String.String(byte[],String) is not applicable
(actual and formal argument lists differ in length)
constructor String.String(byte[],int,int,Charset) is not applicable
(actual argument String cannot be converted to byte[] by method invocation conversion)
constructor String.String(byte[],int,int,String) is not applicable
(actual argument String cannot be converted to byte[] by method invocation conversion)
constructor String.String(byte[],int) is not applicable
(actual and formal argument lists differ in length)
constructor String.String(byte[],int,int,int) is not applicable
(actual argument String cannot be converted to byte[] by method invocation conversion)
constructor String.String(int[],int,int) is not applicable
(actual and formal argument lists differ in length)
constructor String.String(char[],int,int) is not applicable
(actual and formal argument lists differ in length)
constructor String.String(char[]) is not applicable
(actual and formal argument lists differ in length)
constructor String.String(String) is not applicable
(actual and formal argument lists differ in length)
constructor String.String() is not applicable
(actual and formal argument lists differ in length)
Contact_Information.java:146: error: saveTo is already defined in actionPerformed(ActionEvent)
File saveTo = new File("contacts.txt");
^
Contact_Information.java:148: error: method write in class <anonymous ActionListener> cannot be applied to given types;
write(fileStream);
^
required: FileOutputStream,String
found: FileOutputStream
reason: actual and formal argument lists differ in length
Contact_Information.java:157: error: cannot find symbol
output = output + newline;
^
symbol: variable newline
Contact_Information.java:159: error: cannot find symbol
strea.write(data, 0, data.length);
^
symbol: variable strea
Contact_Information.java:168: error: local variable contactFrame is accessed from within inner class; needs to be declared final
contactFrame.firstName.setText(null);
^
Contact_Information.java:168: error: cannot find symbol
contactFrame.firstName.setText(null);
^
symbol: variable firstName
location: variable contactFrame of type JFrame
Contact_Information.java:169: error: local variable contactFrame is accessed from within inner class; needs to be declared final
contactFrame.lastName.setText(null);
^
Contact_Information.java:169: error: cannot find symbol
contactFrame.lastName.setText(null);
^
symbol: variable lastName
location: variable contactFrame of type JFrame
Contact_Information.java:170: error: local variable contactFrame is accessed from within inner class; needs to be declared final
contactFrame.age.setText(null);
^
Contact_Information.java:170: error: cannot find symbol
contactFrame.age.setText(null);
^
symbol: variable age
location: variable contactFrame of type JFrame
Contact_Information.java:171: error: local variable contactFrame is accessed from within inner class; needs to be declared final
contactFrame.phoneNumb.setText(null);
^
Contact_Information.java:171: error: cannot find symbol
contactFrame.phoneNumb.setText(null);
^
symbol: variable phoneNumb
location: variable contactFrame of type JFrame
Contact_Information.java:172: error: local variable contactFrame is accessed from within inner class; needs to be declared final
contactFrame.email.setText(null);
^
Contact_Information.java:172: error: cannot find symbol
contactFrame.email.setText(null);
^
symbol: variable email
location: variable contactFrame of type JFrame
Contact_Information.java:179: error: local variable contactFrame is accessed from within inner class; needs to be declared final
contactFrame.dispose();
^
29 errors
- 05-23-2012, 04:35 AM #2
Re: Help....add actionListener to buttons
"local variable addBut is accessed from within inner class; needs to be declared final"
What don't you understand about that? It clearly states that you need to declare your variable as final.
- 05-23-2012, 05:25 AM #3
Member
- Join Date
- May 2012
- Posts
- 11
- Rep Power
- 0
Re: Help....add actionListener to buttons
A few things
First if you can afford it, and you will need it throughout the program... make a Swing component outside of the class it is being used - makes it usable in an ActionListener (in otherwords, if you create addBut outside of the list() function)
Second never make a new actionlistener check if the source is the ONLY thing it is attached to... (E.G. if(aci.getSource() == addBut))
Third, in your save button action listener you tried to concatinate string with commas (String name = new String(firstName.getText(), lastName.getText())) what you want to do is add them together with a space in the middle (String name = new String(firstName.getText() + " " + lastName.getText()))
fourth check you spelling. (E.G. void write(FileOutputStream stream, String output) throws IOEception) (you spelled Exception wrong, and stram in that same exception piece of code)
fifth, do not forget escape characters. newline is \n in coding and remember to surround it with quotes (E.G. "\n")
sixth, Swing pieces are not like array - you do not need to refer to the uppermost layer... (in other words...) contactFrame.firstName.setText(null); needs to only be firstName.setText(null);
Seventh, some of your things you renamed halfway through - keep naming consistant.
Other than the line "Font Bold = new Font(Font.BOLD);" everything is corrected :)
Java Code://import packages import java.awt.*; import java.awt.event.*; import java.text.*; import java.io.*; import java.util.*; import javax.swing.*; import javax.swing.text.*; //create class public class Contact_Information { JFrame listFrame = new JFrame("Contact Information Program"); JPanel buttons1 = new JPanel(); JPanel columnHeaders = new JPanel(); JPanel contactInfo = new JPanel(); JButton addBut = new JButton("Add New Contact"); JButton viewBut = new JButton("View Contacts"); JButton exit = new JButton("Exit"); JLabel name = new JLabel("Contact Name"); JLabel old = new JLabel("Age"); JLabel cell = new JLabel("Cell Phone"); JLabel emailA = new JLabel("Email Address"); JLabel contactList = new JLabel("Contact Information:"); JFrame contactFrame = new JFrame("Add New Contact"); JPanel buttons2 = new JPanel(); JPanel contactForm = new JPanel(); JButton save = new JButton("Save Contact"); JButton reset = new JButton("Reset Form"); JButton close = new JButton("Close Contact Form"); JLabel fName = new JLabel("First Name:"); JLabel lName = new JLabel("Last Name:"); JLabel cAge = new JLabel("Age:"); JLabel cPhone = new JLabel("Cell Phone:"); JLabel eAddress = new JLabel("Email Address:"); JTextField firstName = new JTextField(10); JTextField lastName = new JTextField(10); JTextField age = new JTextField(3); MaskFormatter mask = null; JFormattedTextField phoneNumb = new JFormattedTextField(mask); JTextField email = new JTextField(40); public void list() { listFrame.setVisible(true); listFrame.setSize(300, 500); listFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); name.setFont(new Font("Times New Roman", Font.BOLD, 12)); old.setFont(new Font("Times New Roman", Font.BOLD, 12)); cell.setFont(new Font("Times New Roman", Font.BOLD, 12)); emailA.setFont(new Font("Times New Roman", Font.BOLD, 12)); listFrame.add(buttons1); buttons1.add(addBut); buttons1.add(viewBut); buttons1.add(exit); listFrame.add(columnHeaders); columnHeaders.add(name); columnHeaders.add(old); columnHeaders.add(cell); columnHeaders.add(emailA); listFrame.add(contactInfo); contactInfo.add(contactList); // add action listeners // opens new JFrame addBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent aci) { if (aci.getSource() == addBut) { contactForm(); } } }); /*//reads from file * viewBut.addActionListener(new ActionListener() { * public void actionPerformed(ActionEvent vcl) { * //reads from file * } * }); */ // exits program exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ep) { System.exit(0); } }); } // end public void list frame // create second frame public void contactForm() { listFrame.setVisible(true); listFrame.setSize(300, 500); listFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); Font bold = new Font(Font.BOLD); MaskFormatter mask = null; try { mask = new MaskFormatter("(###) ###-####"); mask.setPlaceholderCharacter('_'); } catch (ParseException e) { e.printStackTrace(); } contactFrame.add(buttons2); buttons2.add(save); buttons2.add(reset); buttons2.add(close); contactFrame.add(contactForm); contactForm.add(fName); contactForm.add(firstName); contactForm.add(lName); contactForm.add(lastName); contactForm.add(cAge); contactForm.add(age); contactForm.add(cPhone); contactForm.add(phoneNumb); contactForm.add(eAddress); contactForm.add(email); // add action listeners // writes to file save.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent sci) { String name = new String(firstName.getText() + " " + lastName.getText()); String ageC = new String(age.getText()); String cell = new String(phoneNumb.getText()); String emailA = new String(email.getText()); String saveing = new String(name + " " + ageC + " " + cell + " " + emailA); try { File saveTo = new File("contacts.txt"); FileOutputStream fileStream = new FileOutputStream(saveTo); write(fileStream, saveing); } catch (IOException ioe) { System.out.println("Could not save contact to file"); } } void write(FileOutputStream stream, String output) throws IOException { output = output + "\n"; byte[] data = output.getBytes(); stream.write(data, 0, data.length); } }); // clears form reset.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent rcf) { firstName.setText(null); lastName.setText(null); age.setText(null); phoneNumb.setText(null); email.setText(null); } }); // close 2nd JFrame close.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ccl) { contactFrame.dispose(); } }); } // end public void contact frame } // end public class Contact_Information
- 05-23-2012, 06:34 AM #4
Re: Help....add actionListener to buttons
Why do they call it rush hour when nothing moves? - Robin Williams
- 05-23-2012, 11:34 AM #5
Member
- Join Date
- May 2012
- Posts
- 4
- Rep Power
- 0
Re: Help....add actionListener to buttons
Thanks so much for helping. I have made the changes that you suggest and I came up with the following 7 errors:
Contact_Program.java:152: error: illegal start of expression
void write(FileOutputStream stream, String output) throws IOException {
^
Contact_Program.java:152: error: ';' expected
void write(FileOutputStream stream, String output) throws IOException {
^
Contact_Program.java:152: error: ';' expected
void write(FileOutputStream stream, String output) throws IOException {
^
Contact_Program.java:152: error: not a statement
void write(FileOutputStream stream, String output) throws IOException {
^
Contact_Program.java:152: error: ';' expected
void write(FileOutputStream stream, String output) throws IOException {
^
Contact_Program.java:152: error: not a statement
void write(FileOutputStream stream, String output) throws IOException {
^
Contact_Program.java:152: error: ';' expected
void write(FileOutputStream stream, String output) throws IOException {
^
7 errors
Here is my revised code:
Also, I'm having troup figuring out how to read from the contact.txt file and having it show up in my JTextArea of my listFrame. I created the column headers in a seperete JPanel and was thinking either finding a way to column-ize the JTextArea or using a springlayout compactgrid to give me the columns.Java Code://import packages import java.awt.*; import java.awt.event.*; import java.text.*; import java.io.*; import java.util.*; import javax.swing.*; import javax.swing.text.*; //create class public class Contact_Program { //create variables for listFrame JFrame listFrame = new JFrame("Contact Information Program"); JPanel buttons1 = new JPanel(); JPanel columnHeaders = new JPanel(); JPanel contactInfo = new JPanel(); JButton addButton = new JButton("Add New Contact"); JButton viewButton = new JButton("View Contact List"); JButton exitButton = new JButton("Exit"); JLabel name = new JLabel("Full Name"); JLabel old = new JLabel("Age"); JLabel cell = new JLabel("Cell Phone"); JLabel emailA = new JLabel("Email Address"); JTextArea info = new JTextArea(10, 300); //create variables for contactFrame JFrame contactFrame = new JFrame("Add New Contact"); JPanel buttons2 = new JPanel(); JPanel form = new JPanel(); JButton saveButton = new JButton("Save Contact"); JButton resetButton = new JButton("Clear Form"); JButton closeButton = new JButton("Close Contact Form"); JLabel fName = new JLabel("First Name:"); JLabel lName = new JLabel("Last Name:"); JLabel cAge = new JLabel("Age:"); JLabel cPhone = new JLabel("Cell Phone:"); JLabel eAddress = new JLabel("Email Address:"); JTextField firstName = new JTextField(10); JTextField lastName = new JTextField(10); JTextField age = new JTextField(3); MaskFormatter mask = null; JFormattedTextField cellPhone = new JFormattedTextField(mask); JTextField email = new JTextField(40); //create and add to list frame public void list() { listFrame.setVisible(true); listFrame.setSize(200, 500); listFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); name.setFont(new Font("Times New Roman", Font.BOLD, 12)); old.setFont(new Font("Times New Roman", Font.BOLD, 12)); cell.setFont(new Font("Times New Roman", Font.BOLD, 12)); emailA.setFont(new Font("Times New Roman", Font.BOLD, 12)); listFrame.add(buttons1); buttons1.add(addButton); buttons1.add(viewButton); buttons1.add(exitButton); listFrame.add(columnHeaders); columnHeaders.add(name); columnHeaders.add(old); columnHeaders.add(cell); columnHeaders.add(emailA); listFrame.add(contactInfo); contactInfo.add(info); //add action listeners to buttons //open new JFrame addButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent aci) { if(aci.getSource() == addButton) { contactFrame(); } } }); //reads from file viewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent vcl) { //read from file } }); //exits program exitButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ep) { System.exit(0); } }); } //end public void list() //create and add to contact frame public void contactForm() { contactFrame.setVisible(true); contactFrame.setSize(300, 500); contactFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); MaskFormatter mask = null; try { mask = new MaskFormatter("(###) ###-####"); mask.setPlaceholderCharacter('_'); } catch(ParseException e) { e.printStackTrace(); } contactFrame.add(buttons2); buttons2.add(saveButton); buttons2.add(resetButton); buttons2.add(closeButton); contactFrame.add(form); form.add(fName); form.add(firstName); form.add(lName); form.add(lastName); form.add(cAge); form.add(age); form.add(cPhone); form.add(cellPhone); form.add(eAddress); form.add(email); //add action listeners to buttons //write to file saveButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent sci) { String name = new String(firstName.getText() + "" + lastName.getText()); String ageC = new String(age.getText()); String cellP = new String(cellPhone.getText()); String emailA = new String(email.getText()); String saving = new String(name + "" + ageC + "" + cellP + "" + emailA); try { File saveTo = new File("contacts.txt"); FileOutputStream fileStream = new FileOutputStream(saveTo); write(fileStream, saving); } catch (IOException ioe) { System.out.println("Could not save contact to file"); } void write(FileOutputStream stream, String output) throws IOException { output = output + "\n"; byte[] data = output.getBytes(); stream.write(data, 0, data.length); } } }); //reset form resetButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent rcf) { firstName.setText(null); lastName.setText(null); age.setText(null); cellPhone.setText(null); email.setText(null); } }); //close JFrame closeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ccl) { contactFrame.dispose(); } }); } //end public void contactForm() } //end public class contact program
- 05-23-2012, 04:46 PM #6
Re: Help....add actionListener to buttons
You can't nest methods. That's not legal Java syntax.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-23-2012, 06:42 PM #7
Member
- Join Date
- May 2012
- Posts
- 11
- Rep Power
- 0
Re: Help....add actionListener to buttons
To fix the problem, move your void write function into the class portion
Also in line 80 you still have the actionlistener chech to see if it is addbut. DO NOT DO THAT!Java Code:class Contact_Program { public void list() ... public void contactForm() ... private void write() }
You created an ActionListener that ONLY listenes to addbut. That action listener does not listen to anything else, therefore it is checking to make sure that it gets it's information from the only piece it can get information from. It is kind of like telling a computer to make sure that the mouse moved because the mouse moved.
- 05-23-2012, 07:19 PM #8
Member
- Join Date
- May 2012
- Posts
- 4
- Rep Power
- 0
Re: Help....add actionListener to buttons
Alright. I fixed everything so that there is no errors. I added in my main method right below my write() method:
When I ran it, no windows popped. So, there are only 2 more problems that I can see at the moment....Java Code:private void write(FileOutputStream stream, String output) throws IOException { output = output + "\n"; byte[] data = output.getBytes(); stream.write(data, 0, data.length); } public static void main(String [] args){ Contact_Program cp = new Contact_Program(); }
(1) when I run the program, the initial window list() needs to pop
(2) figure out how to read from the file that was created and display it in my JTextArea of the initial window.
- 05-23-2012, 10:32 PM #9
Member
- Join Date
- May 2012
- Posts
- 5
- Rep Power
- 0
Re: Help....add actionListener to buttons
1) Contact_Program doesn't seem to have a constructor, so that call to new Contact_Program() will just use the default constructor, which just calls the super-constructor (I think). Now, if you want the list() method called every time you create a new Contact_Program Object, you should create a constructor and make a call to the list() method. Otherwise, just call cp.list(); in your main after you created the new Contact_Program Object.
- 05-24-2012, 01:11 AM #10
Member
- Join Date
- May 2012
- Posts
- 4
- Rep Power
- 0
Re: Help....add actionListener to buttons
Alright. So that code workes: cp.list(); and my main frame pops. I can click on "Add New Contact" button and my second frame pops as well. I noticed that my MaskFormatter doesn't carry over to my second frame when I added it to my panel. I tried moving around the code:
but I kep getting errors with the try and catch statements.Java Code:MaskFormatter mask = null; try { //below will create a mask formatter for accepting phone numbers mask = new MaskFormatter("(###) ###-####"); mask.setPlaceholderCharacter('_'); } //end try catch (ParseException e) { e.printStackTrace(); } //end catch
Also, I need to read from the file that I created and have it displayed in my JTextArea of my main frame (list()). Not sure how to do this and could use some help.
Similar Threads
-
Actionlistener q
By stuckonjava in forum New To JavaReplies: 2Last Post: 05-16-2012, 08:17 PM -
ActionListener Help
By rakosky in forum AWT / SwingReplies: 4Last Post: 04-06-2012, 03:59 PM -
Please help with actionlistener
By ADustedEwok in forum New To JavaReplies: 5Last Post: 12-08-2011, 10:04 PM -
ActionListener
By jaylimix in forum Java AppletsReplies: 5Last Post: 11-06-2011, 06:05 PM -
Please Help With ActionListener
By Daman12 in forum New To JavaReplies: 29Last Post: 10-26-2011, 07:43 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks