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

Thread: Need Help with program

  1. #1
    Junior Member
    Join Date
    Aug 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need Help with program

    Hello all I am kinda new to java I am trying to write a simple program. This program will accept 2 values from the user and calculate the third avue when the user clicks the "Calculate" button. The problem That I am having is that I want to assign an action listener to both of the text fields so that it can listen in on what value the user enters. Since I am using Eclipse it is showing me and error wehn I try to assign action listener to the formated text fields. Can someone please point out to me where I am going wrong. I have'ht written the logic that calculates the two numbers yet. I want to get everything correct in my GUI before I move on to that point. Below is my code so far
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    public class SimpleCalculation {
     
               //Fields for data entry
               final JFormattedTextField currentField; 
               final JFormattedTextField systemVoltageField;
     
              //Labels to identify the fields
              final JLabel currentLabel;
              final JLabel systemVoltageLabel;
     
             //The button
             final JButton button;
     
             // begin constructor here
             public SimpleCalculation(){
            JFrame frame;
            JPanel panel;
     
            frame = new JFrame("Sun Stone");
            panel = new JPanel();
     
            // create the button
            button = new JButton("Calculate");
     
           //Create the labels.
           currentLabel = new JLabel("Current");
           systemVoltageLabel = new JLabel("System Voltage");
     
          //Create the text fields
          currentField = new JFormattedTextField();
          systemVoltageField = new JFormattedTextField();
     
          // layout the button
          button.addActionListener(new actionEvent());
          panel.add(button);
          frame.add(panel);
     
         // layout the text fields
         frame.add(currentField);
         frame.add(systemVoltageField);
     
        //Other things
        currentField.addActionListener(1);
        systemVoltageField.addActionListener(1);
     
        // rest of the frame
        frame.setSize(400, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
     
    }
     
    	public static void main(String[] args) {
    	SimpleCalculation sc;
    	sc = new SimpleCalculation();
    	}
     
    	public void actionEvent (ActionEvent evt){
     
    	}
    }

    Please help me see where I went wrong. I know that there are many examples that I can put and paste that will do something similar but I want to learn the java language and I learn better when I do it on my own.


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Need Help with program

    Make you class implement ActionListener, then use .addActionListener(this);

    Regards,
    Chris

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Need Help with program

    I had a related question:

    How do I implement the .addActionListener(component) for a class (say, one that extends Frame, not JFrame)?

    edit:
    nevermind, I figured it out. I was doing it backwards.
    Last edited by helloworld922; September 2nd, 2009 at 10:14 PM.

  4. #4
    Junior Member
    Join Date
    Aug 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with program

    I woret a program it accepts 2 user input data and returns a value. I am pretty sure the constructor and the calcalation method is correct but what do I put in the main to run this program?
    Last edited by Hassan; September 5th, 2009 at 08:40 PM.

  5. #5
    Junior Member
    Join Date
    Jul 2009
    Location
    SomeWhere in the world
    Posts
    27
    Thanks
    1
    Thanked 11 Times in 6 Posts

    Default Re: Need Help with program

    For you Hassan
    in main method..Take an instance of your class and your program will run successfuly
    Or

    new YOUR_CLASS_NAME();

  6. #6
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Need Help with program

    Post your updated code and I'll have a look, be sure to use code tags

  7. #7
    Junior Member
    Join Date
    Aug 2009
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with program

    I resolved all my problems but one. I mounted the button, the labels and the text fields onto the frame. The program compiles fine the layouts are exactly what I want it to be. But the problem is that if I move my mouse cursor over the JFrame the labels and the text fields disappears except the button. Any Idea why it is happening?

    Here is the updated code

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
     
    public class SimpleCalculation implements ActionListener{
    	//Data Labels
     
        //Fields for data entry
    	final JFormattedTextField currentField; 
    	final JFormattedTextField systemVoltageField;
    	final JFormattedTextField kVAField;
     
    	//Labels to identify the  fields
        final JLabel currentLabel;
        final JLabel systemVoltageLabel;
        final JLabel kVALabel;
     
        //The button
    	JButton button;
     
    	// begin constructor here
    	public SimpleCalculation(){
    	JFrame frame;
     
    	frame = new JFrame("Sun Stone");
     
    	// create the button and set layout
    	button = new JButton("Calculate");
    	button.setBounds(new Rectangle(20, 90, 40, 30));
     
    	// Create current the label and set layout
        currentLabel = new JLabel("Current");
        currentLabel.setLocation(150,180);
        currentLabel.setSize(50,60);
     
        // Create the current text fields and set layout
        currentField = new JFormattedTextField();
        currentField.setLocation(200, 200);
        currentField.setSize(100,25);
        currentField.setForeground(Color.BLACK);
     
        // Create System Voltage the label and set layout
        systemVoltageLabel = new JLabel("System Voltage");
        systemVoltageLabel.setLocation(350,180);
        systemVoltageLabel.setSize(90,60);
     
        //Create the System Voltage text fields and set layout
        systemVoltageField = new JFormattedTextField();
        systemVoltageField.setLocation(445, 200);
        systemVoltageField.setSize(100,25);
        systemVoltageField.setForeground(Color.BLACK);
     
        // Create kVA the label and set layout
        kVALabel = new JLabel("kVA");
        kVALabel.setLocation(590,180);
        kVALabel.setSize(90,60);
     
        // Create the kVA text fields and set layout
        kVAField = new JFormattedTextField();
        kVAField.setEditable(false);
        kVAField.setLocation(620, 200);
        kVAField.setSize(100,25);
        kVAField.setForeground(Color.red);
     
      	button.addActionListener(this);
     
    	currentField.addActionListener(this);
    	systemVoltageField.addActionListener(this);	
     
    	//Put the panels in this panel, labels on left,
        //text fields on right and the button on the bottom.
    	frame.add(currentLabel);
    	frame.add(systemVoltageLabel);
    	frame.add(kVALabel);
    	frame.add(currentField);
    	frame.add(systemVoltageField);
    	frame.add(kVAField);
    	frame.add(button);
     
    	// Other frame properties
    	frame.setSize(1400,1400);
    	frame.setVisible(true);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    }
    	public void actionPerformed(ActionEvent e) {
    		double Current=Double.parseDouble(currentField.getText()); 
    		double systemVoltage=Double.parseDouble(systemVoltageField.getText()); 
     
    		double kVA;
     
    		kVA = (Current*(systemVoltage*Math.sqrt(3)))/1000;
     
    	}
     
    	public static void main(String[] args) {
    		SimpleCalculation sc;
    		sc = new SimpleCalculation();
    		}
    }