Page 1 of 2 12 LastLast
Results 1 to 20 of 26
Like Tree1Likes

Thread: radio button

  1. #1
    gbonecapone is offline Member
    Join Date
    May 2013
    Posts
    68
    Blog Entries
    1
    Rep Power
    0

    Default radio button

    can someone tell me how i can write part of my program to preform the task "if that radio button is pressed let 'this' = 'that' if not 'this' = 'that2'"?
    thanks.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: radio button

    You are asking several questions here. Some google keywords to look into include ActionListener, if statements, and assignment operators. Without knowing specifically where you're stuck, it's hard to be more specific. Feel free to post an SSCCE demonstrating what you've tried and we'll try to be more specific.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    gbonecapone is offline Member
    Join Date
    May 2013
    Posts
    68
    Blog Entries
    1
    Rep Power
    0

    Default Re: radio button

    thanks for the advice. and sorry my work through me into this big project that no one else here knows how to do, and told me to learn java and take thier old true basic code and translate it. since the program is the heart of the company i cant really release but info about it (meaning formulas and some functions) but its a program to output data based on 9 inputs to send to a mazak to cut a timing screw for bottle packaging lines. im just trying to figure out some simple functions in order to finish this. thanks for all the help everyone has been giving me. im really making progress due to you guys. much appreciated!

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,340
    Rep Power
    5

    Default Re: radio button

    You don't have to release info about the program to demonstrate the problem. Just provide a short compilable example that shows what you are trying to do.

    Regards,
    Jim
    DarrylBurke likes this.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    gbonecapone is offline Member
    Join Date
    May 2013
    Posts
    68
    Blog Entries
    1
    Rep Power
    0

    Default Re: radio button

    well i have this so far-----
    this is inside the part of my program that does the calculations by retrieving all the data inputed by the user.

    Java Code:
    handSignNeg handneg = new handSignNeg(); ///////semi learned this from a video but dont know really what to do with it.
     jRadioButton1.addActionListener((ActionListener) handneg);
     
    System.out.println("handsign "+handsign);
     
    }
     
    
    
    jRadioButton1.setText("Left Hand");
     jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
     public void actionPerformed(java.awt.event.ActionEvent evt) {
     jRadioButton1ActionPerformed(evt);
     }
     });
     
    private void jRadioButton1ActionPerformed(java.awt.event.Action Event evt) { 
    double handsign = -1; // TODO add your handling code here:
     } 
    
    
    
    
    public handSignNeg() {
     }


    when they click the radio button i would like it to change the set variable of handsign, which is originally 1, to -1. to change the outcome of some of my calculations.
    feel free to ask me any questions to go into further detail if it might help. im not sure what else you need to know. i would think it would be simple, clicked button = changed variable but i cant get it correct

    this is my error . it didnt print out the handsign value but the rest of the program worked.

    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: screw.timingScrew$handSignNeg cannot be cast to java.awt.event.ActionListener
    at screw.timingScrew.applyButtActionPerformed(timingS crew.java:1821)
    at screw.timingScrew.access$1100(timingScrew.java:23)
    at screw.timingScrew$12.actionPerformed(timingScrew.j ava:245)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.jav a:6505)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3321)
    at java.awt.Component.processEvent(Component.java:627 0)
    at java.awt.Container.processEvent(Container.java:222 9)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4861)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2287)
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719 )
    at java.awt.Component.dispatchEvent(Component.java:46 87)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:729)
    at java.awt.EventQueue.access$200(EventQueue.java:103 )
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:702)
    at java.awt.EventQueue$4.run(EventQueue.java:700)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPri vilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 699)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:91)

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,340
    Rep Power
    5

    Default Re: radio button

    First, check out the Tutorials in my Signature. There are examples of actionListeners and RadioButtons there. Specifically, How to Use Buttons, Check Boxes, and Radio Buttons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components). Then if you need more help we can assist.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    gbonecapone is offline Member
    Join Date
    May 2013
    Posts
    68
    Blog Entries
    1
    Rep Power
    0

    Default Re: radio button

    Okay thanks ill definitely look at that. thanks a lot for your help and patience.

  8. #8
    gbonecapone is offline Member
    Join Date
    May 2013
    Posts
    68
    Blog Entries
    1
    Rep Power
    0

    Default Re: radio button

    so i ended up using a toggle button instead because for the simple fact ive made more progress with it. i got it to change the variable handsign within the buttons property change method but i cant get it to relay the variable back to the rest of the program where it uses the variable in a equation.



    here is the button .
    how can i call that from the program. can i just write { handsign = leftTog."something"} to change the value of my handsign variable??
    Java Code:
      private void leftTogPropertyChange(java.beans.PropertyChangeEvent evt) {                                       
           leftTog.addActionListener(new ActionListener(){
                          
                 @Override
                 public void actionPerformed(ActionEvent ev){
                  double handsign;
            boolean selected = leftTog.getModel().isSelected();
                     if (selected) {
                         handsign = -1;
                         System.out.println("hand2 = "+handsign);
                     }
                     else{ handsign = 1;
                         System.out.println("hand2 = "+handsign);}
                    
                     }});
        }

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: radio button

    I really don't know what you're asking here. What do you want your handsign variable to equal? What in the leftTog class do you want it to hold?

    I recommend you check out the button tutorial that Jim posted above, as well as the API so you can see what values are available from each class: Java Platform SE 7
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    gbonecapone is offline Member
    Join Date
    May 2013
    Posts
    68
    Blog Entries
    1
    Rep Power
    0

    Default Re: radio button

    I want it to equal 1 unless i click the toggle which would change it to negative one. i pretty much just want it to change the sign of it. i did look through that and it was pretty helpful besides for the part im stuck on i couldnt find help for it anywhere.
    in my program the user enters 9 variable that are inputed into an equation when i click a button. one of the variables is handsign which default is 1. if they wanted to change it to -1 they simply click the toggle button. turns out it isnt as simple as i thought. right now when i click the button it changes handsign to -1 but only within the the propertychange method created by the gui. i just need to relay that value to the equation. sorry for my stupidity and thanks for your patience. this has been very frustrating.

  11. #11
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: radio button

    Quote Originally Posted by gbonecapone View Post
    I want it to equal 1 unless i click the toggle which would change it to negative one. i pretty much just want it to change the sign of it. i did look through that and it was pretty helpful besides for the part im stuck on i couldnt find help for it anywhere.
    in my program the user enters 9 variable that are inputed into an equation when i click a button. one of the variables is handsign which default is 1. if they wanted to change it to -1 they simply click the toggle button. turns out it isnt as simple as i thought. right now when i click the button it changes handsign to -1 but only within the the propertychange method created by the gui. i just need to relay that value to the equation. sorry for my stupidity and thanks for your patience. this has been very frustrating.
    How are you reading the numbers from the user? Can't you just do the same thing with the sign? The JRadioButton class has a method that returns true or false depending on whether or not it is selected.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    gbonecapone is offline Member
    Join Date
    May 2013
    Posts
    68
    Blog Entries
    1
    Rep Power
    0

    Default Re: radio button

    radio button-time.png
    this is what it looks like. out of convience and ease i would like the engineers to just click the button if they need the screw to be left handed(handsign = -1)

  13. #13
    gbonecapone is offline Member
    Join Date
    May 2013
    Posts
    68
    Blog Entries
    1
    Rep Power
    0

    Default Re: radio button

    they enter in the variables click apply and it inputs the values to the equation

  14. #14
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: radio button

    Right, but what I'm saying is, you get the numbers from those text boxes programatically when you need them, right? Why not just do the same thing with the JRadioButton?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  15. #15
    gbonecapone is offline Member
    Join Date
    May 2013
    Posts
    68
    Blog Entries
    1
    Rep Power
    0

    Default Re: radio button

    they all have to be inputed for the equation to work. and it would be easy to have a handsign text box to that was set to 1 unless they wanted to change it to -1 but im hardheaded and want it to be cool and convient to click it if they needed it. and i would gladly use the jRadioButton that i have there instead because that was my original idea but not sure how to do it. I would even like it to be a group if that was easier. one radio button be for right hand and one for left hand. think that would be easier than a toggle? thanks for your help by the way. i really appreciate it.

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: radio button

    Quote Originally Posted by gbonecapone View Post
    i would gladly use the jRadioButton that i have there instead because that was my original idea but not sure how to do it. I would even like it to be a group if that was easier. one radio button be for right hand and one for left hand. think that would be easier than a toggle?
    So you still haven't gone through the tutorial linked in #6?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  17. #17
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: radio button

    Quote Originally Posted by gbonecapone View Post
    they all have to be inputed for the equation to work. and it would be easy to have a handsign text box to that was set to 1 unless they wanted to change it to -1 but im hardheaded and want it to be cool and convient to click it if they needed it. and i would gladly use the jRadioButton that i have there instead because that was my original idea but not sure how to do it. I would even like it to be a group if that was easier. one radio button be for right hand and one for left hand. think that would be easier than a toggle? thanks for your help by the way. i really appreciate it.
    Right, I'm suggesting you use a JRadioButton, but access its value the same way you're accessing the values of the JTextFields- not by attaching a Listener, but by simply reading the value like I presume you do with the JTextFields.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  18. #18
    gbonecapone is offline Member
    Join Date
    May 2013
    Posts
    68
    Blog Entries
    1
    Rep Power
    0

    Default Re: radio button

    yea i looked at it db. thanks for posting it, but for some reason im an idiot and cant figure out how to get it to work. i got the actionlistener with the toggle to work but i cant seem to do it with my radio group or single radio. idk if its because i generate the class with the GUI instead of just writing it all out.

  19. #19
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: radio button

    Why do you think you need an ActionListener on the JRadioButton? Why can't you just read whether it is selected or not when you read the rest of the values from the JTextFields?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  20. #20
    gbonecapone is offline Member
    Join Date
    May 2013
    Posts
    68
    Blog Entries
    1
    Rep Power
    0

    Default Re: radio button

    i like that idea kevin. but im not sure how i will do that. instead of "variable"= "input".getText(); what would you suggest writing? button.get"what"(). should i use a group or just one for left hand? and how would i write if button is clicked set handsign =-1 without having it listen for the click?

Page 1 of 2 12 LastLast

Similar Threads

  1. radio button error
    By dekon in forum New To Java
    Replies: 2
    Last Post: 08-10-2011, 09:06 PM
  2. lwuit radio button
    By gaurav_17may in forum CLDC and MIDP
    Replies: 1
    Last Post: 07-25-2011, 12:10 PM
  3. Radio Button
    By S-NESH in forum New To Java
    Replies: 6
    Last Post: 02-04-2011, 05:03 PM
  4. Radio Button help!
    By javanator in forum New To Java
    Replies: 3
    Last Post: 04-25-2010, 08:01 PM
  5. How to use SWT Radio Button
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-25-2008, 02:25 PM

Tags for this Thread

Posting Permissions

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