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

Thread: radio button with spinner to create an answer in a textarea box

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default radio button with spinner to create an answer in a textarea box

    basically i'm new to java and i'm trying to make a ticket machine and i'm stuck on how to make it so that once a radio button is selected and a value from a spinner is selected it would both times together to give me a value in a textarea box but i'm unsure of how to do this

    could anyone give me some tips or useful advice on how to tackle this please


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: radio button with spinner to create an answer in a textarea box

    Put a listener on them? That should be covered in the tutorial for each.

    Lesson: Using Swing Components (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: radio button with spinner to create an answer in a textarea box

    i read through it but still don't really understand what to do

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: radio button with spinner to create an answer in a textarea box

    What didn't you understand? What did you try? Keep in mind that to really read through the Swing tutorials should take days or weeks, not minutes or hours. If you want help, you should provide an SSCCE that demonstrates what you tried and where you're stuck, and you should ask a specific technical question. It's hard to answer "how do I do this" with anything other than links to the tutorials.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: radio button with spinner to create an answer in a textarea box

    oh sorry about that i'll be more exact

    PHP Code:
    import static javax.swing.JOptionPane.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    import javax.swing.*;
    import java.text.DecimalFormat;

    public class 
    ticket extends JFrame implements ActionListener {

        static final 
    double SINGLE 1.40;
        static final 
    double RETURN = 2.40;
        static final 
    double ZONE_A 3.80;
        static final 
    double ZONE_A_B 5.50;

          
    DecimalFormat pounds = new DecimalFormat("£0.00");
        private 
    String id;
         
    JTextField ticket = new JTextField(5);
        
    JTextField stp = new JTextField(5);
        
    int amount 0//pay in pence
        
    JTextField change = new JTextField(5);
        
    JTextField paid = new JTextField(5);
        
    JButton ten = new JButton("10p");
        
    JButton twenty = new JButton("20p");
        
    JButton fifty = new JButton("50p");
        
    JButton pound = new JButton("£1");
        
    JButton two = new JButton("£2");
        
    JButton five = new JButton("£5");
        
    JButton tenp = new JButton("£10");
        
    JButton twentyp = new JButton("£20");
        
    JButton cancel = new JButton("Cancel");
        
    JRadioButton single = new JRadioButton("single (£1.40)",false);
        
    JRadioButton Return = new JRadioButton("return (£2.40)",false);
        
    JRadioButton zoneA = new JRadioButton("Zone A (£3.80)",false);
        
    JRadioButton zoneAB = new JRadioButton("Zone A B (£5.50)",false);
        
    ButtonGroup type =new ButtonGroup();
        
    // The spinner give double values in the range 1 to 10.
         // From the spinner, the user can only purchase 10 tickets at a time!
         
    private JSpinner numtick = new JSpinner(new SpinnerNumberModel(10101));
         private 
    int stpno 0;
         private 
    int size 0;
     
    // Set as private so that it can only be used in this class and no others. Set as public to be used in other classes.

        
    public static void main(String[] args)
        {
            new 
    ticket();
        }
        public 
    ticket()
        {
              
    setLayout(new BorderLayout());
             
    setSize(700150);
             
    setTitle("Redwich Trams - Ticket Machine");
             
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             
    JPanel top = new JPanel();
             
    top.setLayout(new FlowLayout());
             
    top.add(new JLabel("Ticket Type:   "));

             
    type.add(single);
             
    type.add(Return);
             
    type.add(zoneA);
             
    type.add(zoneAB);
             
    top.add(single);
             
    top.add(Return);
             
    top.add(zoneA);
             
    top.add(zoneAB);
             
    top.add(new JLabel("  Tickets"));
             
    top.add(numtick);
             
    single.addActionListener(this);
             Return.
    addActionListener(this);
             
    zoneA.addActionListener(this);
             
    zoneAB.addActionListener(this);

            
    add("North"top);
             
    setVisible(true);

             
    JPanel middle = new JPanel();
             
    middle.setLayout(new FlowLayout());
             
    middle.add(new JLabel("Ticket Type:   "));
            
    setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
             
    middle.add(ten);
             
    middle.add(twenty);
             
    middle.add(fifty);
             
    middle.add(pound);
             
    middle.add(two);
             
    middle.add(five);
             
    middle.add(tenp);
             
    middle.add(twentyp);
             
    middle.add(cancel);
             
    ten.addActionListener(this);
             
    twenty.addActionListener(this);
             
    fifty.addActionListener(this);
             
    pound.addActionListener(this);
             
    two.addActionListener(this);
             
    five.addActionListener(this);
             
    tenp.addActionListener(this);
             
    twentyp.addActionListener(this);
             
    cancel.addActionListener(this);

             
    add("Center"middle);
             
    setVisible(true);

             
    JPanel bottom = new JPanel();
             
    bottom.setLayout(new FlowLayout());
             
    bottom.add(new JLabel("   Ticket Cost:"));
             
    bottom.add(ticket);
             
    bottom.add(new JLabel("   Amount Paid:"));
             
    bottom.add(paid);
             
    bottom.add(new JLabel("   Amount Still To Pay:"));
             
    bottom.add(stp); 
             
    stp.setEditable(false);
             
    bottom.add(new JLabel("   Change:"));
             
    bottom.add(change);
             
    ticket.setEditable(false);
             
    paid.setEditable(false);
            
             
    change.setEditable(false);
             
    ticket.setText("£0.00");
             
    stp.setText("£0.00");
             
    change.setText("£0.00");
             
    paid.setText("£0.00");

             
    add("South"bottom);
             
    setVisible(true);
        }
              public 
    void actionPerformed(ActionEvent e
         {
            if (
    e.getSource() == tenamount -= 10;
            if (
    e.getSource() == twentyamount -= 20;
            if (
    e.getSource() == fiftyamount -= 50;
            if (
    e.getSource() == poundamount -= 100;
            if (
    e.getSource() == twoamount -= 200;
            if (
    e.getSource() == fiveamount -= 500;
        
    //    if (e.getsource() == tenp) amount -= 1000;
         //   if (e.getsource() == twentyp) amount -= 2000;
         
    }
        {
    double tp 0;
           
    int tick Integer.parseInt(""+numtick.getValue());
          if (
    single.isSelected()) tp 1.40
            {
            
              
    ticket.setText(pounds.format(tp));
              
    stp.setText(pounds.format(tp));
                  }
             
            if (Return.
    isSelected()) tp 2.40;
                {

              
    ticket.setText(pounds.format(tp));
              
    stp.setText(pounds.format(tp));
                }
            
         if (
    zoneA.isSelected()) tp 3.80;
                  {
                
    ticket.setText(pounds.format(tp));
                
    stp.setText(pounds.format(tp));
                 }
              
             if (
    zoneAB.isSelected()) tp 5.50
                  {
                
    ticket.setText(pounds.format(tp));
                
    stp.setText(pounds.format(tp));
                  }
             
    //    if  (e.getSource() == cancel);
             
    {  
           
    ticket.setText("£0.00");
           
    stp.setText("£0.00");
           
    change.setText("£0.00");
           
    paid.setText("£0.00"); 
             }
            }
     } 
    thats the code i've got so far with that i'm trying to work out how to make it so that when the radio button and the spinner are both selected it puts the value in the ticket cost text area box

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: radio button with spinner to create an answer in a textarea box

    I don't know what to tell you that I haven't already said. Sounds like a job for a listener. And that's not an SSCCE- if you want help with adding a listener, write a small program that adds a listener to a single component, or show us where you're having trouble with that one step. We don't need any of that extra stuff.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: radio button with spinner to create an answer in a textarea box

    i'm having trouble with this step

    PHP Code:
    double tp 0;
          
    int noticket Integer.parseInt(""+numtick.getValue());
          if (
    single.isSelected()) tp 1.40
            {
              
    ticket.setText(pounds.format(tp));
              
    stp.setText(pounds.format(tp));

                  }
             
            if (Return.
    isSelected()) tp 2.40;
                {

              
    ticket.setText(pounds.format(tp));
              
    stp.setText(pounds.format(tp));
                }
            
         if (
    zoneA.isSelected()) tp 3.80;
                  {
                
    ticket.setText(pounds.format(tp));
                
    stp.setText(pounds.format(tp));
                 }
              
             if (
    zoneAB.isSelected()) tp 5.50
                  {
                
    ticket.setText(pounds.format(tp));
                
    stp.setText(pounds.format(tp));
                  } 

  8. #8
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: radio button with spinner to create an answer in a textarea box

    ticket.setText(pounds.format(tp)); 
                stp.setText(pounds.format(tp));
    These two statements are being compiled each time.......
    Try looking at if-else

Similar Threads

  1. GUI Calculator: Adding Answer Button? Please Help
    By Staticity in forum What's Wrong With My Code?
    Replies: 29
    Last Post: July 27th, 2011, 03:18 PM
  2. Replies: 3
    Last Post: April 11th, 2011, 09:51 PM
  3. need help Coding RADIO BUTTON for REMEMBER ME
    By timosoft in forum Java Theory & Questions
    Replies: 3
    Last Post: February 4th, 2011, 05:19 PM
  4. Adding Radio Button Values
    By bazazu in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 13th, 2010, 10:41 AM
  5. Action from Radio Button
    By halfwaygone in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 25th, 2010, 10:52 AM