Page 1 of 3 123 LastLast
Results 1 to 20 of 53
Like Tree2Likes

Thread: Problem with action performing !!

  1. #1
    baig-sh is offline Member
    Join Date
    Nov 2011
    Posts
    35
    Rep Power
    0

    Default Problem with action performing !!

    Hi,
    i m end at the of my assignment "Ideal Weight Calculator" but hanging with action performed method.

    i successfully save the slider position in a integer.
    public void stateChanged( ChangeEvent evt )
    {
    int h;
    String sl;
    JSlider source;
    source = (JSlider)evt.getSource();
    if ( source.getName().equals("sliderA") )
    sl.getText(sliderA);
    h = IntegerparseInt(sl);
    }

    i have 2 JRadio buttons "Male" and "Female"

    i successfully set & add action command and action listener at both buttons add in to jpanel.
    Now!
    don't know how to get value from radio buttons and how to link with slider's value(h)


    my bottom line need is

    Male = h*h/30

    Female = h*h/28

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default Re: Problem with action performing !!

    You need to define the variable(s) at the class level (not as local variable(s) in a method) so that other methods can see them.

    Where are the radio buttons defined? Where are you trying to get their value?

  3. #3
    baig-sh is offline Member
    Join Date
    Nov 2011
    Posts
    35
    Rep Power
    0

    Default Re: Problem with action performing !!

    i want to trying if someone select "Male" button then application apply the formula h*h/30 if female then h*h/28
    but not know how it,ll possible?
    how can i get value from Radio buttons and how implement the formula .... please give me an example

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default Re: Problem with action performing !!

    Here is some pseudo code:
    if (maleRadioButton selected)
    amt = formula for male
    else
    amt = formula for female.

    Read the API doc for the radio buttons to see what method to call to determine if it is selected.

  5. #5
    baig-sh is offline Member
    Join Date
    Nov 2011
    Posts
    35
    Rep Power
    0

    Default Re: Problem with action performing !!

    Hi,
    I am beginner i tried my best but fail.
    here is the image of bit of code and error message.
    error indicates a variable not find (may be)
    what is wrong please guide me Thanks.
    Problem with action performing !!-void.jpg

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default Re: Problem with action performing !!

    The variables you are using are local to a method and are NOT known outside of the method.
    Move the variables out of the method to be defined at the class level.

  7. #7
    baig-sh is offline Member
    Join Date
    Nov 2011
    Posts
    35
    Rep Power
    0

    Default Re: Problem with action performing !!

    Thanks Norm

  8. #8
    baig-sh is offline Member
    Join Date
    Nov 2011
    Posts
    35
    Rep Power
    0

    Default Re: Problem with action performing !!

    Hi,
    i m sad glade
    code is working but in wrong way... Problem with action performing !!-sldr.jpg

    weight values,ll change in this way
    user >>
    1 step ..> select Gender
    2. adjust slider


    but my application reverse the method

    1 adjust slider and then select the Gender
    how can fix
    here is a i bit of 2 methods
    -------------------------------------------------
    public void stateChanged( ChangeEvent evt )
    {

    JSlider source = (JSlider)evt.getSource();

    if ( !source.getValueIsAdjusting( )) {
    sl = source.getValue();

    h = sl*sl;
    h2 = h/28;
    h3 = h/30;
    ans = h2 +"";
    ans2 = h3 +"";
    }
    }
    ------------------------------------

    public void actionPerformed(ActionEvent ae) {

    if(ae.getActionCommand().equals("Male") ){
    textA.setText(ans);
    }
    else {
    textA.setText(ans2);
    }
    }
    ------------------------------------------------

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

    Default Re: Problem with action performing !!

    You might want to check the value of the JRadioButtons in your slide'rs listener. Calling isSelected() on the JRadioButtons is one way to find out which is selected.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default Re: Problem with action performing !!

    Be careful of doing integer arithmetic. If any of your h variables are int, the results of dividing by an integer may not be what you want.
    For example 10/12 = 0

    Change the variables to double if you don't what truncation.

  11. #11
    baig-sh is offline Member
    Join Date
    Nov 2011
    Posts
    35
    Rep Power
    0

    Default Re: Problem with action performing !!

    i tried different options of code but app give me the correct value with when i release the slider and click in JRadio Buttons.
    i mean slider is not "live". when i change the slider position there is no change in result box but when i click in the radio button
    app show the correct results with current slider position.
    out of mind can't understand what can i do. Help me please.
    now i tried the following method :
    ----------------------------------------------------------
    public void actionPerformed(ActionEvent ae) {

    if("Male".equals(ae.getActionCommand())) {
    if(m.isSelected() );

    ans = h2 +"";
    textA.setText(ans);

    }else{

    ans2 = h3 +"";
    textA.setText(ans2);
    }
    }

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default Re: Problem with action performing !!

    when i change the slider position there is no change in result box
    If you are having problems with your slider code you need to post that code.

  13. #13
    baig-sh is offline Member
    Join Date
    Nov 2011
    Posts
    35
    Rep Power
    0

    Default Re: Problem with action performing !!

    slider code
    --------------------

    public void stateChanged( ChangeEvent evt )
    {

    JSlider source = (JSlider)evt.getSource();

    if ( !source.getValueIsAdjusting( )) {
    sl = source.getValue();
    h = sl*sl;
    h2 = h/28;
    h3 = h/30;

    }
    }

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default Re: Problem with action performing !!

    You need to post a fully working program that will compile and execute if you want anyone to try debugging your code. Code snippets can not be tested.

    Have you tried debugging your code by adding println statements to show the values of all the variables as they change?
    Print out the values of all the variables: h, h1, h2, h3

  15. #15
    baig-sh is offline Member
    Join Date
    Nov 2011
    Posts
    35
    Rep Power
    0

    Default Re: Problem with action performing !!

    Here complete code:
    ----------------------------
    Java Code:
    import java.awt.* ;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.* ;
    import java.io.*;
    import java.util.*;
    import java.lang.Integer.*;
    
    public class  IdealWeight extends JFrame
                            implements ChangeListener,ActionListener
    {
      JSlider    sliderA;
      JTextField textA;
      JLabel lab1, lab2, lab3, lab4;
     JRadioButton m;
     JRadioButton f;
     ButtonGroup fm;
     JPanel jp1, jp2, jp3;
    int h, h2, h3, sl;
      String ans, ans2;	
      public IdealWeight()  
      { 
        setTitle( "Your Ideal Weight" );
        
        setDefaultCloseOperation( EXIT_ON_CLOSE );
        
        sliderA = new JSlider( SwingConstants.VERTICAL,  
           40, 80, 40);
        sliderA.setMajorTickSpacing( 10 );
        sliderA.setMinorTickSpacing(  1);
        sliderA.setPaintTicks( true );
        sliderA.setPaintLabels( true );
        sliderA.setPreferredSize( new Dimension(100,300)) ;
        sliderA.setVisible(true);
        sliderA.addChangeListener( this );
        sliderA.setName("sliderA"); 
        textA   = new JTextField( 4 );
     textA.setText( sliderA.getValue() + " ");
        lab1 = new JLabel("<html><font color =green><b>your Height</b></font></html>");
    lab2 = new JLabel("<html><font color =RED><b><u>Your Gender</u></b></font></html>");
    lab3 = new JLabel("in Inches");
    lab4 =  new JLabel("Your Ideal Weight");
       m = new JRadioButton("Male", true);
       m.setActionCommand("Male");
       
       m.addActionListener(this);
       f = new JRadioButton("Female", false);
       f.setActionCommand("Female");
       f.addActionListener(this);
      fm = new ButtonGroup();
     fm.add(m);
    fm.add(f);
    jp1 = new JPanel();
    jp1.setLayout(new GridLayout(3, 2, 5, 5));
        
       jp1.add(lab2);
    jp1.add(lab1);
     jp1.add(m);
    jp1.add(lab3);
       jp1.add(f);
    jp2 = new JPanel();
    jp2.setLayout(new GridLayout(1, 1));
    jp2.add(lab4);
     jp2.add(textA); 
       
        // content pane
        getContentPane().setLayout( new BorderLayout() );
      getContentPane().add(jp1,BorderLayout.NORTH);
       getContentPane().add(jp2,BorderLayout.SOUTH);
      getContentPane().add(sliderA,BorderLayout.EAST);
       
         
      }
       
      public void stateChanged( ChangeEvent evt )
      {
    
       JSlider source = (JSlider)evt.getSource();
           
        if ( !source.getValueIsAdjusting( )) {
        sl = source.getValue();
        h = sl*sl;
     h2 = h/28;
       h3 = h/30;
    
    
    
    } // end of  if
       } 
    
    
      public void actionPerformed(ActionEvent ae) { 
    
       if("Male".equals(ae.getActionCommand())) {
    if(m.isSelected() );
    
    ans = h2 +"";
    textA.setText(ans);
    
    }else{
    
    ans2 = h3 +"";
    textA.setText(ans2);
    }
    }  // end of  performed
      public static void main ( String args[] )
      {
       IdealWeight weightApp  = new IdealWeight() ;
        weightApp.setSize( 250, 500 );   
        weightApp.setVisible( true );         
        
      }
    }
    Last edited by Norm; 11-09-2011 at 01:49 AM. Reason: added code tags

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default Re: Problem with action performing !!

    You need to add some printlns to the code for debugging.


    Print out the values of the variables as has been suggested before.

  17. #17
    baig-sh is offline Member
    Join Date
    Nov 2011
    Posts
    35
    Rep Power
    0

    Default Re: Problem with action performing !!

    i add the println method. Yes slider movement shown in console other function act like past.
    how can println method fix this prob !
    Please Mr Norm help me

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default Re: Problem with action performing !!

    how can println method fix this prob !
    Printing out the values that your program sees will help you understand what your program is doing.
    When you see what the values are you should know why your program is not doing what you want it to do.

    Also the print outs will show when code is being executed.
    I just added some printlns to your code and what it printed out shows me that the slider is working ok.
    What should happen when the slider changes?
    Last edited by Norm; 11-09-2011 at 02:43 AM.

  19. #19
    baig-sh is offline Member
    Join Date
    Nov 2011
    Posts
    35
    Rep Power
    0

    Default Re: Problem with action performing !!

    dear norm,
    i m trying since last night but fail. coz i am just 30 days student of java and have not experience and also pressurized
    with this assignment, i m near to closing date.
    if u provide me some example ( like past) i,ll thankful to you.

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,789
    Rep Power
    25

    Default Re: Problem with action performing !!

    Add printlns to your program that shows the values it is getting and where it is getting them.
    Execute the program and copy and paste here the contents of the console where the program printed.

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 02-03-2011, 06:19 AM
  2. Replies: 3
    Last Post: 01-12-2011, 12:45 AM
  3. Problem in action listener
    By cool in forum AWT / Swing
    Replies: 4
    Last Post: 11-16-2010, 08:44 AM
  4. problem in action
    By jperson in forum New To Java
    Replies: 2
    Last Post: 07-24-2010, 01:00 PM
  5. Performing Exponentiation in Java without the Math Class
    By BlueJ Coder in forum New To Java
    Replies: 9
    Last Post: 10-14-2009, 09:48 PM

Posting Permissions

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