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 3 of 3

Thread: Problem with JText Fields and using action listener

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

    Default Problem with JText Fields and using action listener

    /**
     *
     */
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JTextField;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JLabel;
     
     
    public class Artpanel extends JPanel
    {
     
       private JButton calculate;
        private JLabel label1;
        private JLabel label2;
        private JLabel label3;
        private JTextField input1;
        private JTextField input2;
        int piece1;
        int piece2;
     
     
     
        /**
         * Constructor for objects of class ArtprizePanel
        **/
     
        public Artpanel()
        {
            label1 = new JLabel ("Enter amount of votes for piece 1:");
            label2 = new JLabel ("Enter amount of votes for piece 2:");
     
            input1 = new JTextField (5);
            input2 = new JTextField (5);
            calculate.addActionListener (new ButtonListener());
     
     
            add (label1);
            add (input1);
            add (label2);
            add (input2);
            add (calculate);
            add (label3);
     
            setPreferredSize (new Dimension(400, 100));
            setBackground (Color.blue);
        }
        private class ButtonListener implements ActionListener
        {
         public void actionPerformed (ActionEvent event)
         { 
     
     
     
     
     
        }
    }
     
     
     
            }
    this is what I have so far. I need to be able to use the numbers input in to the two jtext fields, say 80 and 70, and use an action listener with if statements to have the program say 80 was the winner. i'm quite new at this stuff, so i'm really confused. im pretty sure i need to set two global integers and have a listener for each text field then set those text fields equal to those integers, then write the if statements. i'm just lost on how to do this.
    Last edited by toble; October 27th, 2010 at 12:36 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Problem with JText Fields and using action listener

    Quote Originally Posted by toble View Post
    /**
     *
     */
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JTextField;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JLabel;
     
     
     
    public class Artpanel extends JPanel
    {
     
       private JButton calculate;
        private JLabel label1;
        private JLabel label2;
        private JLabel label3;
        private JTextField input1;
        private JTextField input2;
        int piece1;
        int piece2;
     
     
     
        /**
         * Constructor for objects of class ArtprizePanel
        **/
     
        public Artpanel()
        {
            label1 = new JLabel ("Enter amount of votes for piece 1:");
            label2 = new JLabel ("Enter amount of votes for piece 2:");
     
     
            input1 = new JTextField (5);
            input2 = new JTextField (5);
            calculate.addActionListener (new ButtonListener());
     
     
            add (label1);
            add (input1);
            add (label2);
            add (input2);
            add (calculate);
            add (label3);
     
            setPreferredSize (new Dimension(400, 100));
            setBackground (Color.blue);
        }
        private class ButtonListener implements ActionListener
        {
         public void actionPerformed (ActionEvent event)
         { 
     
     
     
     
     
        }
    }
     
     
     
            }
    this is what I have so far. I need to be able to use the numbers input in to the two jtext fields, say 80 and 70, and use an action listener with if statements to have the program say 80 was the winner. i'm quite new at this stuff, so i'm really confused. im pretty sure i need to set two global integers and have a listener for each text field then set those text fields equal to those integers, then write the if statements. i'm just lost on how to do this.
    It seems that you've never defined calculate.

    Perhaps

    calculate = new JButton("Calculate");

    might solve the problem.

    Also, nothing seems to be setVisible(true);
    And you never imported Color.

    Also,

    /**
     *
     */
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JTextField;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JLabel;
    import java.awt.Color;
     
    public class Artpanel extends JPanel
    {
     
       private JButton calculate;
        private JLabel label1;
        private JLabel label2;
        private JLabel label3;
        private JTextField input1;
        private JTextField input2;
        int piece1;
        int piece2;
     
     
     
        /**
         * Constructor for objects of class ArtprizePanel
        **/
     
        public Artpanel()
        {
           setVisible(true);
    // or maybe just setVisible();  can't recall which
            label1 = new JLabel ("Enter amount of votes for piece 1:");
            label2 = new JLabel ("Enter amount of votes for piece 2:");
     
     
            input1 = new JTextField (5);
    // also, you might be able to ditch the JLabels by doing this
    // input1.setToolTipText("Enter amount of votes for piece 1:");
            input2 = new JTextField (5);
        piece1 = Integer.parseInt(input1.getText());
         piece2 = Integer.parseInt(input2.getText());
    calculate = new JButton("Calculate");
            calculate.addActionListener (new ActionListener() {
     
    public void actionPerformed(ActionEvent e)
    {
     
    }
     
    });
     
     
            add (label1);
    label.setVisible(true);
            add (input1);
    input1.setVisible(true);
            add (label2);
    label2.setVisible(true);
            add (input2);
    input2.setVisible(true);
            add (calculate);
    calculate.setVisible(true);
            add (label3);
    label3.setVisible(true);
     
            setPreferredSize (new Dimension(400, 100));
            setBackground (Color.BLUE);
        }
     
     
     
            }

    I think that you may have to have ButtonListener in the declarations before your constructor or else it won't be able to get it if I do it the other way.
    Last edited by javapenguin; October 27th, 2010 at 01:13 PM.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with JText Fields and using action listener

    /**
     *
     */
     
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.JTextField;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JLabel;
    import java.awt.Color;
     
     
    public class Artpanel extends JPanel
    {
     
        private JLabel label1;
        private JLabel label2;
        private JLabel label3;
        private JTextField input1;
        private JTextField input2;
        private JButton calculate;
        int piece1 = 0;
        int piece2 = 0;
     
     
     
        /**
         * Constructor for objects of class ArtprizePanel
        **/
     
        public Artpanel()
     
     
        {
            label1 = new JLabel ("Enter amount of votes for piece 1:");
            label2 = new JLabel ("Enter amount of votes for piece 2:");
            label3 = new JLabel ("---");
            calculate = new JButton ("Calculate");
            calculate.addActionListener (new ButtonListener());
     
            input1 = new JTextField (5);
            input2 = new JTextField (5);
     
     
            add (label1);
            add (input1);
            add (label2);
            add (input2);
            add (calculate);
            add (label3);
     
     
            setPreferredSize (new Dimension(400, 100));
            setBackground (Color.yellow);
        }
        private class ButtonListener implements ActionListener
        {
         public void actionPerformed (ActionEvent event)
         {
            String text = input1.getText();
            String text1 = input2.getText();
     
            piece1 = Integer.parseInt (text);
            piece2 = Integer.parseInt (text1);
     
     
     
     
     
     
     
     
     
        }
     
        }
     
    }

    okay here's what i have now. i got the thing to launch, but now i need to figure out how to set an if else statement to determine whether piece 1 or piece 2 is bigger, then display appropriate text in label 3 declaring which one is greater. how would i go about that?

Similar Threads

  1. converter program, problem with action listener
    By robertson.basil in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 2nd, 2010, 05:44 AM
  2. Action Listener
    By Suzanne in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 29th, 2010, 10:50 AM
  3. Action Listener
    By kray66 in forum AWT / Java Swing
    Replies: 2
    Last Post: April 19th, 2010, 03:26 PM
  4. Problem with Action Listener
    By JonoScho in forum AWT / Java Swing
    Replies: 4
    Last Post: March 19th, 2010, 01:03 AM
  5. Need Help Action Listener....
    By vhizent23 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 9th, 2009, 01:46 PM