Help Me On This Please

Discussion in 'Java' started by DiceV2, Sep 23, 2010.

  1. DiceV2

    DiceV2 New Member

    Joined:
    Sep 23, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Malate, Manila
    Good day.. i'm working on a program in GUI and i'm having a problem on how to call the value from itemstatechanged to the actionlistener using the RADIO BUTTON.
    Code:
    private class OrderButtonHandler implements ActionListener
        {
            
            public void actionPerformed(ActionEvent e)
            {
                double numItem, kilo;
                String name;
                kilo = Double.parseDouble(pkTF.getText());
                name = (customerTF.getText());
                numItem = (ItemEvent.SELECTED);
                JOptionPane.showMessageDialog(null, "Customer: " + name + "\n"
                        + "Ordered: " + kilo + " kilo(s)" + "\n"
                        + "The Price is: " + numItem);            
            }
        }
        
        private class RadioListener implements ItemListener
        {
        public void itemStateChanged(ItemEvent event)
            {
               double num;
                if (event.getSource() == appleRB){
                    num = 30;   
                }
                else if (event.getSource() == orangeRB){
                    num = 20;             
                }
                else if (event.getSource() == bananaRB) {
                    num = 20;              
                }
                else if (event.getSource() == grapeRB){
                    num = 40;
                }
                else if(event.getSource() == mangoRB){
                    num = 40;
                }
                event.getItem();
            }
        }
    the price(numItem) should be the PRODUCT of the kilos entered and the fruit selected, but the output is (1) i think its from the "ItemEvent.SELECTED"
    how can i call the value "NUM" selected in itemstatechanged and assign it to the "numItem" in actionlistener?
    and if you have a good suggestion or alternative that i can do about this program please do suggest thanks Godbless!
     
    Last edited by a moderator: Sep 24, 2010
  2. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    send all your project here in a zip file and use code tags for your code.
     
  3. DiceV2

    DiceV2 New Member

    Joined:
    Sep 23, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Malate, Manila
    hope you can help me as soon as possible thanks.. Godbless
    the project is attached..
     

    Attached Files:

  4. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    Code:
    ............
    public class FruitOrder extends JFrame 
    {
        [COLOR=DarkRed]private double numItem=0;[/COLOR]//add this line there
    ............
    
    Code:
    ..........
            rbgroup.add(mangoRB);
            [COLOR=DarkRed]rbgroup.clearSelection();[/COLOR]//nothing selected by default
            rListener = new RadioListener();
    ............
    
    Code:
    ...............
        private class OrderButtonHandler implements ActionListener
        {
            
            public void actionPerformed(ActionEvent e)
            {
                double kilo;
                String name;
                kilo = Double.parseDouble(pkTF.getText());
                name = (customerTF.getText());
                JOptionPane.showMessageDialog(null, "Customer: " + name + "\n"
                        + "Ordered: " + kilo + " kilo(s)" + "\n"
                        + "The Price is: " + numItem[COLOR=DarkRed]*kilo[/COLOR]);
            }
        }
    ..........
    



    Code:
    
    public void itemStateChanged(ItemEvent event)
            {
                if (event.getSource() == appleRB){
                   [COLOR=DarkRed] numItem = 30;[/COLOR]//since we declared this as a global variable
                }
                else if (event.getSource() == orangeRB){
                   [COLOR=DarkRed] numItem = 20;[/COLOR]
                }
                else if (event.getSource() == bananaRB) {
                    [COLOR=DarkRed]numItem = 20;[/COLOR]
                }
                else if (event.getSource() == grapeRB){
                    [COLOR=DarkRed]numItem = 40;[/COLOR]
                }
                else if(event.getSource() == mangoRB){
                    [COLOR=DarkRed]numItem = 40;[/COLOR]
                }
                event.getItem();
            }
    
    
     
    shabbir likes this.
  5. DiceV2

    DiceV2 New Member

    Joined:
    Sep 23, 2010
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Malate, Manila
    Sir Vixen thank you very much! it worked! can i add you as a friend here? this is my first time here, and joined this forum to learn more about programming and stuff. till next time!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice