Link to home
Start Free TrialLog in
Avatar of stevefNYC
stevefNYC

asked on

Java swing/awt inquiry

Hi all,

I'm trying to get a better hang of Java and Swing/AWT in general. I've written this piece of code to calculate area for the width/length of an office. Really simple just to understand the stuff at hand.  

I'm getting compile issues that look like the following:

$ javac OfficeAreaCalculator.java
OfficeAreaCalculator.java:84: OfficeAreaCalculator.calculateButtonHandler is not abstract and does not override abstract method actionPerformed(java.awt.event.ActionEvent) in java.awt.event.ActionListener
      class calculateButtonHandler implements ActionListener
        ^
OfficeAreaCalculator.java:120: OfficeAreaCalculator.FocusHandler is not abstract and does not override abstract method focusLost(java.awt.event.FocusEvent) in java.awt.event.FocusListener
   class FocusHandler implements FocusListener
   ^
Note: OfficeAreaCalculator.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
2 errors


Anyone kind enough to point out what I'm doing incorrect?
import java.text.*; // need this for formatting
import javax.swing.*;
import java.awt.event.*;
import java.awt.Container;
import java.awt.*; // this is for layout management
 
public class OfficeAreaCalculator extends JFrame
{
	private JFrame mainFrame;
	private JButton calculateButton;
	private JButton exitButton;
	private JTextField lengthField;
	private JTextField widthField;
	private JTextField areaField;
	private JLabel lengthLabel;
	private JLabel widthLabel;
	private JLabel areaLabel;
                            
 
	public OfficeAreaCalculator()
	{
		mainFrame = new JFrame("Office Area Calculator");
	
		// create all components
		calculateButton 	= new JButton("Calculate area");
		exitButton			= new JButton("Exit");
		lengthLabel			= new JLabel("Enter the length of the office: ");
		widthLabel			= new JLabel("Enter the width of the office: ");
		areaLabel			= new JLabel("Office area: ");
		lengthField			= new JTextField(5);
		widthField			= new JTextField(5);
		areaField			= new JTextField(5);
	
		// get the content pane
		Container c = mainFrame.getContentPane();
	
		// set the layout manager
		c.setLayout(new FlowLayout());
	
		// add the components to the content pane
		c.add(lengthLabel);
		c.add(lengthField);
		c.add(widthLabel);
		c.add(widthField);
		c.add(areaLabel);
		c.add(areaField);
		c.add(calculateButton);
		c.add(exitButton);
 
	    // set size
		mainFrame.setSize(250,150);
	
		// define and register window event handler
		mainFrame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) { System.exit(0); }
		});
	
		// create and register the handlers
		calculateButtonHandler chandler = new calculateButtonHandler(); //instantiate new object
		calculateButton.addActionListener(chandler); // add event listener
	
		ExitButtonHandler ehandler = new ExitButtonHandler(); // instant a new handler
		exitButton.addActionListener(ehandler); // register the handler
	
		FocusHandler fhandler = new FocusHandler();
		lengthField.addFocusListener(fhandler);
		widthField.addFocusListener(fhandler);
		areaField.addFocusListener(fhandler);
	
		mainFrame.show();
	}   
	
	// classes for the button event handlers
	class calculateButtonHandler implements ActionListener
	{
		public void actionPerformed(Action e)
		{
			DecimalFormat num = new DecimalFormat(", ###.##");
			double width, length, area;
			String instring;
			
			instring = lengthField.getText();
			if (instring.equals(""))
			{
				instring = "0";
				lengthField.setText("0");
			}
			length = Double.parseDouble(instring);
			
			instring = widthField.getText();
			if (instring.equals(""))
			{
				instring = "0";
				widthField.setText("0");
			}                       
			width = Double.parseDouble(instring);
			area = length * width;
			areaField.setText(num.format(area));
		} 
	} // inner class ends here
	
   class ExitButtonHandler implements ActionListener
   {
   		public void actionPerformed(ActionEvent e)
		{
			System.exit(0);
		}
   }
 
   class FocusHandler implements FocusListener
   {
		public void focusGained(FocusEvent e)
		{
			if(e.getSource() == lengthField || e.getSource() == widthField)
			{
				areaField.setText("");
			}
			else if(e.getSource() == areaField)
			{
				areaField.setNextFocusableComponent(calculateButton);
				calculateButton.grabFocus();
			}
		}
		
		public void focusList(FocusEvent e)
		{
			if(e.getSource() == widthField)
			{
				widthField.setNextFocusableComponent(calculateButton);
			}
		}
   } // end focus listener class
 
   public static void main(String args[])
   {
   		new OfficeAreaCalculator();	//instantiage GUI
   }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of contactkarthi
contactkarthi
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stevefNYC
stevefNYC

ASKER

Silly overlooks on my behalf.  

Thanks karthi.  BTW, any chance of explaining what I'm using that's deprecated?

Note: OfficeAreaCalculator.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
setNextFocusableComponent

and mainFrame.show(); are both deprecated
I guess I'm reading an old book. :-( Is there something more reliable to read that's current, so I'm not using deprecated methods?
if you use an ide it will point you out immediately. also you can google for the new alternatives for the deprecated ones.

http://java.sun.com/docs/books/tutorial/