Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Can anyone help me with an action listener?

  1. #1
    mdCollins1 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Can anyone help me with an action listener?

    I'm new to java and trying to learn on my own by watching video tutorials. I made a program with a scanner to do a simple conversion of whatever you typed in. Like you enter kilos and it converts it into pounds. What i'm trying to do now is make a program using java swing so it opens up a window but I can't seem to get the action listener right. (I'm using Java SE6)
    Here's the code:
    Java Code:
    import javax.swing.*;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class Converter extends JFrame implements ActionListener {
    
    	private JButton convert;
    	JTextArea text = new JTextArea(1,20);
    	private int conversion = 0;
    	
    	
    	
    	public Converter() {
    		setLayout(new FlowLayout());
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(400,200);
    		convert = new JButton("Convert");
    		add(convert);
    		add(text);
    		convert.addActionListener(this);
    		
    		
    
    		
    		
    	}
    
    
    public void actionPerformed(ActionEvent e) {
    
    		
    	}
    }
    Last edited by mdCollins1; 03-21-2012 at 01:45 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Can anyone help me with an action listener?

    What is your problem exactly? Your actionPerformed method should do something in it (in this case get data and perform operations on it, then display the result).

    http://docs.oracle.com/javase/tutori...nlistener.html
    http://docs.oracle.com/javase/tutori...nts/intro.html

    http://docs.oracle.com/javase/tutori...ybigindex.html is a great resource to use along side whatever videos you are watching.
    Last edited by sunde887; 03-21-2012 at 03:46 AM.

  3. #3
    mdCollins1 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: Can anyone help me with an action listener?

    Quote Originally Posted by sunde887 View Post
    What is your problem exactly? Your actionPerformed method should do something in it (in this case get data and perform operations on it, then display the result).

    How to Write an Action Listener (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)
    Introduction to Event Listeners (The Java™ Tutorials > Creating a GUI With JFC/Swing > Writing Event Listeners)

    The Really Big Index is a great resource to use along side whatever videos you are watching.
    I'm not exactly sure what to put into it to make it display the correct answer. I'm trying to make it so you enter inches and click convert and it tells you how many cms that would equal. (or another conversion)

    (And thanks for the link on the really big index. I think it will come in handy)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Can anyone help me with an action listener?

    Quote Originally Posted by mdCollins1 View Post
    I'm not exactly sure what to put into it to make it display the correct answer. I'm trying to make it so you enter inches and click convert and it tells you how many cms that would equal. (or another conversion)
    Still, let's see your attempt to do this first, no matter how bad it is. Doing this will show us what you're doing wrong and what you need to work on, and it will help us give more helpful directed answers/
    sunde887 likes this.

  5. #5
    mdCollins1 is offline Member
    Join Date
    Mar 2012
    Posts
    7
    Rep Power
    0

    Default Re: Can anyone help me with an action listener?

    Quote Originally Posted by Fubarable View Post
    Still, let's see your attempt to do this first, no matter how bad it is. Doing this will show us what you're doing wrong and what you need to work on, and it will help us give more helpful directed answers/
    This is the original code that only displays it in the console:
    Java Code:
    import java.util.Scanner;
    
    public class lbstokg_exact {
    	public static void main(String[] args) {
    		Scanner scanner = new Scanner(System.in);
    		double lbs;
    		double answer;
    		System.out.println("Pounds to KG converter");
    		System.out.println("Enter pounds");
    		lbs = scanner.nextDouble();
    		answer = lbs / 2.20462262;
    		System.out.println(lbs+" lbs is equal to "+answer+" kg");
    
    	}
    }
    I was trying to make it with java swing and got completely stuck. I have no idea what to do now.
    (This isn't the inches to centimeters but it is essentially the same idea.)

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Can anyone help me with an action listener?

    The main components you will need is a JLabel and a JTextField.

    Here are the APIdocs for both classes:
    JTextField (Java Platform SE 7 )
    JLabel (Java Platform SE 7 )

    Specifically look for methods to get or set data.

Similar Threads

  1. Replies: 2
    Last Post: 12-11-2011, 10:44 PM
  2. Problem in action listener
    By cool in forum AWT / Swing
    Replies: 4
    Last Post: 11-16-2010, 07:44 AM
  3. Action Listener
    By greatmajestics in forum AWT / Swing
    Replies: 8
    Last Post: 03-25-2010, 05:39 PM
  4. Action Listener? how to use this?
    By jeffrey in forum New To Java
    Replies: 2
    Last Post: 10-12-2009, 08:51 AM
  5. action listener on jcombobox
    By chkm8 in forum New To Java
    Replies: 2
    Last Post: 02-05-2009, 10:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •