Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: ActionListener help

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ActionListener help

    Greetings,

    This is my first post on a Java help forum, so forgive me (and tell me what to do better next time) for any mistakes. Anyway, I've been assigned *this project* over the weekend, and I am having some difficulty with the ActionListener needed to complete it. I took a read of *JavaPF's post on the topic*, but I'm still unable to make it work.

    I am building two different classes, as described in the assignment link. I'll post them below, House first and MyRealEstateOffice second.

    public class House {
    	private int nBeds;
    	private double nBaths;
    	private double sqFeet;
    	private double askingPrice;
    	private String ownerName;
    	private String ownerPhoneNumber;
    	private String address;
    	private boolean fence;
    	private double lotSize;
    	public House(int nBeds,
    			double nBaths,
    			double sqFeet,
    			double askingPrice,
    			String ownerName,
    			String ownerPhoneNumber,
    			String address,
    			boolean fence,
    			double lotSize){
    		this.nBeds = nBeds;
    		this.nBaths = nBaths;
    		this.sqFeet = sqFeet;
    		this.askingPrice = askingPrice;
    		this.ownerName = ownerName;
    		this.ownerPhoneNumber = ownerPhoneNumber;
    		this.setAddress(address);
    		this.setFence(fence);
    		this.setLotSize(lotSize);
    	}
    	public void setNBeds(int nBeds) {
    		this.nBeds = nBeds;
    	}
    	public int getNBeds() {
    		return nBeds;
    	}
    	public void setNBaths(double nBaths) {
    		this.nBaths = nBaths;
    	}
    	public double getNBaths() {
    		return nBaths;
    	}
    	public void setSqFeet(double sqFeet) {
    		this.sqFeet = sqFeet;
    	}
    	public double getSqFeet() {
    		return sqFeet;
    	}
    	public void setAskingPrice(double askingPrice) {
    		this.askingPrice = askingPrice;
    	}
    	public double getAskingPrice() {
    		return askingPrice;
    	}
    	public void setOwnerName(String ownerName) {
    		this.ownerName = ownerName;
    	}
    	public String getOwnerName() {
    		return ownerName;
    	}
    	public void setOwnerPhoneNumber(String ownerPhoneNumber) {
    		this.ownerPhoneNumber = ownerPhoneNumber;
    	}
    	public String getOwnerPhoneNumber() {
    		return ownerPhoneNumber;
    	}
    	public void setAddress(String address) {
    		this.address = address;
    	}
    	public String getAddress() {
    		return address;
    	}
    	public void setFence(boolean fence) {
    		this.fence = fence;
    	}
    	public boolean isFence() {
    		return fence;
    	}
    	public void setLotSize(double lotSize) {
    		this.lotSize = lotSize;
    	}
    	public double getLotSize() {
    		return lotSize;
    	}
    	public String toString(){
    		String r="";
    		r += ownerName +" ";
    		r += ownerPhoneNumber +" ";
    		r += address +" ";
    		r += nBeds +" ";
    		r += nBaths +" ";
    		r += sqFeet +" ";
    		r += lotSize +" ";
    		r += fence +" ";
    		r += askingPrice +" ";
    		return r;
    	}
    }

    import java.util.ArrayList;
    import javax.swing.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;
     
    public class MyRealEstateOffice extends JFrame {
    	ArrayList<House> listings = new ArrayList<House>();
    	JPanel panel = new JPanel();
    	JLabel lblBeds = new JLabel("# Beds: ");
    	JLabel lblBaths = new JLabel("# Baths: ");
    	JLabel lblSqFeet = new JLabel("Square Feet: ");
    	JLabel lblPrice = new JLabel ("Asking Price: ");
    	JLabel lblOwner = new JLabel("Owner's Name: ");
    	JLabel lblPhone = new JLabel("Owner's Phone: ");
    	JLabel lblLot = new JLabel("Lot size: ");
    	JLabel lblAddress = new JLabel("Address: ");
     
    	JCheckBox checkFence = new JCheckBox("Fence:");
     
    	JTextField txtBeds = new JTextField(20);
    	JTextField txtBaths = new JTextField(20);
    	JTextField txtSqFeet = new JTextField(20);
    	JTextField txtPrice = new JTextField (20);
    	JTextField txtOwner = new JTextField(20);
    	JTextField txtPhone = new JTextField(20);
    	JTextField txtLot = new JTextField(20);
    	JTextField txtAddress = new JTextField(20);
     
    	JButton addListings = new JButton("Add to listings");
    	addListings.addActionListener(new Listener1());
    	JButton showListings = new JButton("Show listings");
    	JButton saveListings = new JButton("Save listings");
     
    	public MyRealEstateOffice() {
    		setTitle("Steve's Real Estate Office");
    		setSize(500,400);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		build();
    		add(panel);
    		setVisible(true);
    	}
    	public void build() {
    		panel.setLayout(null);
    		int x = 12, y = 0, yh=18, ys=24,w=120;
    		lblBeds.setBounds(x,y += ys,w,yh);
    		lblBaths.setBounds(x,y += ys,w,yh);
    		lblSqFeet.setBounds(x,y += ys,w,yh);
    		lblPrice.setBounds(x,y += ys,w,yh);
    		lblOwner.setBounds(x,y += ys,w,yh);
    		lblPhone.setBounds(x,y += ys,w,yh);
    		lblLot.setBounds(x,y += ys,w,yh);
    		lblAddress.setBounds(x,y += ys,w,yh);
    		x = 140;
    		y = 0;
    		w = 240;
    		txtBeds.setBounds(x,y += ys,w,yh);
    		txtBaths.setBounds(x,y += ys,w,yh);
    		txtSqFeet.setBounds(x,y += ys,w,yh);
    		txtPrice.setBounds(x,y += ys,w,yh);
    		txtOwner.setBounds(x,y += ys,w,yh);
    		txtPhone.setBounds(x,y += ys,w,yh);
    		txtLot.setBounds(x,y += ys,w,yh);
    		txtAddress.setBounds(x,y += ys,w,yh);
    		x=400;
    		y=70;
    		checkFence.setBounds(x,y += ys,w,yh);
    		x=10;
    		y=220;
    		w=150;
    		addListings.setBounds(x,y += ys,w,yh);
    		x=170;
    		y=220;
    		w=150;
    		showListings.setBounds(x,y += ys,w,yh);
    		x=330;
    		y=220;
    		w=150;
    		saveListings.setBounds(x,y += ys,w,yh);
     
    		panel.add(txtBeds);
    		panel.add(txtBaths);
    		panel.add(txtSqFeet);
    		panel.add(txtPrice);
    		panel.add(txtOwner);
    		panel.add(txtPhone);
    		panel.add(txtLot);
    		panel.add(txtAddress);
     
    		panel.add(lblBeds);
    		panel.add(lblBaths);
    		panel.add(lblSqFeet);
    		panel.add(lblPrice);
    		panel.add(lblOwner);
    		panel.add(lblPhone);
    		panel.add(lblLot);
    		panel.add(lblAddress);
     
    		panel.add(checkFence);
     
    		panel.add(addListings);
    		panel.add(showListings);
    		panel.add(saveListings);
     
    	}
     
    	private class Listener1 implements ActionListener {
    		public void actionPerformed(ActionEvent e){
    			String t = "";
    		}
    	}
     
    	public static void main(String[] args) {
    		MyRealEstateOffice mre = new MyRealEstateOffice();
    	}
    }

    Obviously, the project is far from complete. The portion I'm specifically having trouble with is line 30 of the MyRealEstateOffice class.

    addListings.addActionListener(new Listener1());

    That line is giving me a constant error alert of "Syntax error on token "addActionListener", = expected after this token," and I have no idea what to do to fix it. Any help anyone can offer is extremely appreciated.


  2. #2
    Junior Member
    Join Date
    Feb 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ActionListener help

    Hey,

    You must put this code inside a method, a constructor!
    Outside methods you can only declare attributes, for example a button.

    I guess it will be solved with that... See you!

Similar Threads

  1. How to Add ActionListener to a JButton in Swing?
    By JavaPF in forum Java Swing Tutorials
    Replies: 17
    Last Post: April 24th, 2013, 05:14 PM
  2. need help with ActionListener,JPanel,JFrame
    By amahara in forum AWT / Java Swing
    Replies: 5
    Last Post: February 3rd, 2010, 01:40 PM
  3. Question about ActionListener
    By TimW in forum AWT / Java Swing
    Replies: 6
    Last Post: November 4th, 2009, 11:00 AM
  4. SOMEONE PLEASE HELP ME ADD THE ACTIONLISTENER INTERFACE...
    By beginning2Understand in forum AWT / Java Swing
    Replies: 5
    Last Post: June 30th, 2009, 12:42 AM