wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Add ActionListener to open new JFrame + error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Add ActionListener to open new JFrame + error" Watch "Add ActionListener to open new JFrame + error" New topic
Author

Add ActionListener to open new JFrame + error

Joyce Meats
Greenhorn

Joined: May 22, 2012
Posts: 1
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:
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,Font2DHandle) 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

HERE IS MY CODE SO FAR:


Hopefully I can get some help. I am really frusterated that I can't get this to work.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4531
    
    5

Start small.

Compile each time you add a few lines of code.

Fix the errors.

That way you won't be overwhelmed by 29 errors.

For now, start with the first one:
Contact_Information.java:55: error: local variable addBut is accessed from within inner class; needs to be declared final

Do that. Declare it final, before it's accessed on line 55 of Contact_Information.java.

And welcome to the Ranch!


luck, db
There are no new questions, but there may be new answers.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4531
    
    5

Please BeForthrightWhenCrossPostingToOtherSites
http://www.java-forums.org/new-java/60077-help-add-actionlistener-buttons.html
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
Welcome to the Ranch

I shall move this discussion to our GUIs forum.
Seeing getSource() == anywhere in code makes me worry. You are adding a Listener to addBut (please write the names of variables out in full: call it addButton rather than addBut), and asking whether the source is the same. If you add that Listener to that button, then it has to have the same source, so that test is redundant and unnecessary.
You have some peculiar design. It is usual for display components to be organised in the constructor of a display class, or in methods called setUpGUI or similar. You are also using multiple frames. That is not usually a good idea. You want a single frame to hold your initial display, and if you require another sort of display, consider a dialog, an internal frame or different displays within the same frame with card layout or similar. In the method which creates the second frame, you introduce a variable list (which is not a good name for a GUI component), but I cannot see where you declared that.
Darryl is right. You should be writing a few lines and compiling, so you only have to sort out a few errors at a time.

As for design: I suspect there is a risk of your trying to put the logic of the calculations into the same class. That is not good design. You should separate the logic from the display. In fact, you ought to get the logic working from the command line before trying to attach the display. Then you can be sure of the logic and only need to worry about the display
 
wood burning stoves
 
subject: Add ActionListener to open new JFrame + error