4 Replies - 2137 Views - Last Post: 25 November 2008 - 03:43 PM Rate Topic: -----

#1 ghettoMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-September 08

Try{} Catch and ActionListener

Post icon  Posted 25 November 2008 - 03:13 PM

I'm supposed to create a class with a listener that displays a message with the hurricane category and when greater than category 2 displays Warning along with the category number. I have made the class and a listener class. it displays the message but will not show the category number. My category number stays at zero i do not see why. I know there is a problem with my try catch or set methods, but i do not see them.
here is my code so far. any advice.
import java.awt.*;
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.*;
import java.io.IOException;



public class lab13 extends JFrame{
	public int hurricaneCat;
	public String hurricane = "Hurricane Category ";
	public JLabel hurricaneMsg = new JLabel();
	public int hCat= 0;
	public lab13(){
	MyListner listner = new MyListner();
	setLayout(new BorderLayout());


	String enterMsg = "Enter Hurricane Category ";
	//int hurricaneCat = 0;
	JTextField catNum= new JTextField(5);

	JLabel enterLabel = new JLabel();
	enterLabel.setText(enterMsg);
	catNum.setHorizontalAlignment(JTextField.RIGHT);

	JPanel topPanel = new JPanel();
	JPanel bottomPanel = new JPanel();

	topPanel.add(enterLabel);
	topPanel.add(catNum);
	bottomPanel.add(hurricaneMsg);

	add(topPanel, BorderLayout.NORTH);
	add(bottomPanel, BorderLayout.CENTER);
	catNum.addActionListener(listner);

	}


public  void setHurricaneCat(int Cat){
	this.hurricaneCat = Cat;
	}

public  int getHurricaneCat(){
	return hurricaneCat;
	}

//Main
public static void main(String[] args) {
	lab13 frame = new lab13();
	frame.setTitle("Lab 13");
	frame.setLocationRelativeTo(null);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setSize(400,250);
	frame.setVisible(true);
 }


public class MyListner implements ActionListener {
	public void actionPerformed(ActionEvent e){
	try{
		String getInt= hurricaneMsg.getText();
		 hCat = Integer.parseInt(getInt);
		 setHurricaneCat(hCat);
		}
	catch(NumberFormatException ex){
		}


		if(hurricaneCat > 2)
			hurricaneMsg.setText("Warning "+hurricane + getHurricaneCat() );
		else
			hurricaneMsg.setText(hurricane + getHurricaneCat());
		}
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Try{} Catch and ActionListener

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Try{} Catch and ActionListener

Posted 25 November 2008 - 03:19 PM

Hm...I don't see why it's not working. One moment. Gonna run it for myself.

EDIT: Ok, I see what the problem is. You need a class variable of type JTextField, catNum is the one you have now, but that's only active in the constructor. What you're doing right now...check the Integer.parseInt() statement. You're parsing the text in your output label. NOT your JTextField.

Simply add a class variable...JTextField catNum;, then delete the JTextField class name from the place where you initialize that variable...then change the parseInt() line to use catNum instead of hurricaneMsg. That might be a little confusing...so I'll post the entire code and comment the changes.

import java.awt.*;
import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.*;
import java.io.IOException;



public class lab13 extends JFrame{
    public int hurricaneCat;
    public String hurricane = "Hurricane Category ";
    public JLabel hurricaneMsg = new JLabel();
    public int hCat= 0;
    public JTextField catNum; // ADD THIS

    public lab13(){
    MyListner listner = new MyListner();
    setLayout(new BorderLayout());


    String enterMsg = "Enter Hurricane Category ";
    
    catNum= new JTextField(5);  // DELETE 'JTextField'

    JLabel enterLabel = new JLabel();
    enterLabel.setText(enterMsg);
    catNum.setHorizontalAlignment(JTextField.RIGHT);

    JPanel topPanel = new JPanel();
    JPanel bottomPanel = new JPanel();

    topPanel.add(enterLabel);
    topPanel.add(catNum);
    bottomPanel.add(hurricaneMsg);

    add(topPanel, BorderLayout.NORTH);
    add(bottomPanel, BorderLayout.CENTER);
    catNum.addActionListener(listner);

    }


public  void setHurricaneCat(int Cat){
    this.hurricaneCat = Cat;
    }

public  int getHurricaneCat(){
    return hurricaneCat;
    }

//Main
public static void main(String[] args) {
    lab13 frame = new lab13();
    frame.setTitle("Lab 13");
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(400,250);
    frame.setVisible(true);
}


public class MyListner implements ActionListener {
    public void actionPerformed(ActionEvent e){
    try{
        String getInt= catNum.getText(); // CHANGE TO 'catNum'
         hCat = Integer.parseInt(getInt);
         setHurricaneCat(hCat);
        }
    catch(NumberFormatException ex){
        }


        if(hurricaneCat > 2)
            hurricaneMsg.setText("Warning "+hurricane + getHurricaneCat() );
        else
            hurricaneMsg.setText(hurricane + getHurricaneCat());
        }
    }

}


Hope this helps! :D

This post has been edited by Locke37: 25 November 2008 - 03:30 PM

Was This Post Helpful? 1
  • +
  • -

#3 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1940
  • View blog
  • Posts: 4,027
  • Joined: 11-December 07

Re: Try{} Catch and ActionListener

Posted 25 November 2008 - 03:33 PM

I've not run it but shouldn't his line:

String getInt= hurricaneMsg.getText();

be

String getInt= catNum.getText();

in which case you'll need to declate it as an instance variable and not in the constructor. You may already know this but I think the text field only generates action events when you press enter.

Edit: Damn. Ninjad!

This post has been edited by cfoley: 25 November 2008 - 03:34 PM

Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Try{} Catch and ActionListener

Posted 25 November 2008 - 03:35 PM

:D
Was This Post Helpful? 1
  • +
  • -

#5 ghettoMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-September 08

Re: Try{} Catch and ActionListener

Posted 25 November 2008 - 03:43 PM

Thanks that worked. I just overlooked, seems silly that i did now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1