LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-03-2009, 10:54 PM   #1
Cyhaxor
Member
 
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129

Rep: Reputation: 15
Java nested class (event handler) error


Hello,
I'm reading the book JAVA How To Program and I'm trying to implement a GUI program. An example from the book. And when I'm trying to compile the code I get an error message for my event handler. Here is the code:
Code:
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;


public class TextFieldFrame extends JFrame
{
    private JTextField jTextField1; // text field with set size
    private JTextField jTextField2; // text field constructed with text
    private JTextField jTextField3; // text field with text and size
    private JPasswordField jPasswordField1; // password field with text
    
    // TextFieldFrame constructor adds jTextFields to JFrame
    public TextFieldFrame()
    {
	super("Testing JTextField and JPasswordField");
	setLayout(new FlowLayout()); // set frame layouts
	
	// construct textfield with 10 columns
	jTextField1 = new JTextField(10);
	add(jTextField1); // adds the textfield to  JFrame
	
	// construct textfield with default text
	jTextField2 = new JTextField("Enter text here");
	add(jTextField2);
	
	// construct textfield with default text and 21 columns
	jTextField3 = new JTextField("Uneditable text filed", 21);
	jTextField3.setEditable(false); // disable editing
	add(jTextField3);
	
	// construct passwordtextfield with default text
	jPasswordField1 = new JPasswordField("Hidden text");
	add(jPasswordField1);
	
	// register event handlers
	TextFieldHandler handler = new TextFieldHandler();
	jTextField1.addActionListener(handler);
	jTextField2.addActionListener(handler);
	jTextField3.addActionListener(handler);
	jPasswordField1.addActionListener(handler);
	
	// private inner class for event handling
	private class TextFieldHandler implements ActionListener
	{
	    public void actionPerformed(ActionEvent event)
	    {
		String string = ""; // declare string to display
		
		// user pressed Enter on JTextField jTextFiled1
		if (event.getSource() == jTextField1)
		{
		    string = String.format("jTextFiled1: %s", event.getActionCommand());
		}
		
		// user pressed Enter on JTextField jTextFiled2
		else if (event.getSource() == jTextField2)
		{
		    string = String.format("jTextFiled2: %s", event.getActionCommand());
		}
		
		// user pressed Enter on JTextField jTextFiled3
		else if (event.getSource() == jTextField3)
		{
		    string = String.format("jTextFiled3: %s", event.getActionCommand());
		}
		
		// user pressed Enter on JPasswordField jPasswordField1
		else if (event.getSource() == jPasswordField1)
		{
		    string = String.format("jPasswordField1: %s", new String(jPasswordField1.getPassword()));
		}
		
		// display jTextField Content
		JOptionPane.showMessageDialog(null, string);
	    }
	}
    }
}
And here is the error message:
Code:
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
	TextFieldHandler cannot be resolved to a type
	TextFieldHandler cannot be resolved to a type
	Illegal modifier for the local class TextFieldHandler; only abstract or final is permitted

	at TextFieldFrame.<init>(TextFieldFrame.java:41)
	at TextFieldTest.main(TextFieldTest.java:8)
And the code of my main class but I don't think is necessary:
Code:
import javax.swing.JFrame;


public class TextFieldTest
{
    public static void main(String[] args)
    {
	TextFieldFrame textFieldFrame = new TextFieldFrame();
	textFieldFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	textFieldFrame.setSize(350, 100);
	textFieldFrame.setVisible(true);
    }

}
and the code at lines indicated by the compiler as error code:
Code:
at TextFieldFrame.<init>(TextFieldFrame.java:41)
TextFieldHandler handler = new TextFieldHandler();
==================================================
at TextFieldTest.main(TextFieldTest.java:8)
TextFieldFrame textFieldFrame = new TextFieldFrame();
Anyone seeing what is going wrong? I just copied the code from the book! Seems very strange.
 
Old 02-03-2009, 11:01 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 111Reputation: 111
You can't create non-anonymous classes inside functions.
 
Old 02-03-2009, 11:05 PM   #3
Cyhaxor
Member
 
Registered: Nov 2004
Location: UK
Distribution: Fedora 12
Posts: 129

Original Poster
Rep: Reputation: 15
The nested class in not inside a method(function) is inside the top-level class. I know that you can't create non-anonymous classes inside a function.


Sorry but it's inside the constructor.. I need to close the constructor:
Code:
TextFieldHandler handler = new TextFieldHandler();
jTextField1.addActionListener(handler);
jTextField2.addActionListener(handler);
jTextField3.addActionListener(handler);
jPasswordField1.addActionListener(handler);
} <---- here
Thanks

Last edited by Cyhaxor; 02-03-2009 at 11:15 PM.
 
Old 02-04-2009, 12:07 AM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 111Reputation: 111
hehe, same thing happens to me sometimes when I'm reorganizing a ton of code and lose track of scope.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java generic nested class problem ta0kira Programming 10 04-04-2011 04:18 AM
Help with linux event handler linux88 Linux - Server 3 09-18-2008 12:20 PM
Nagios Event Handler issue lil_drummaboy Linux - Networking 3 02-29-2008 03:03 AM
Gtkmm / UIManager / passing info to event handler cheeseplz Programming 4 10-18-2007 11:33 PM
TCP-IP event-handler in a Linux server Zingaro2002 Linux - Networking 4 06-06-2002 10:05 AM


All times are GMT -5. The time now is 01:44 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration