5

Im using BlueJ and I have run into problem. I have a combo box with 3 options. Each option is a string. The three options are Day, Week, and Month. What I want to do is if Day is picked than a variable equals 30 (a double) and so on. Can someone help me buy telling me how to give "emailvalue" a value based on the selected option in the combobox?

EDIT: Im going to add my actual program code which is a monthly data calculater. It does compile.

Here is the code:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    import java.awt.image.*;
    import java.awt.geom.*;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    /** 
    * @author (Andrew Napier, Sikander Singh) 
    * @version (December 18th, 2012)
    */
    public class MonthlyData extends JFrame
    {
    private JLabel title, stitle, l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11,l12,l13,l14;
    private JLabel l15,l16,l17,l18,l19,l20,l21,l22,l23,l24,l25,l26,l27,l28,l29;
    private JLabel Usage,Estimated,solu1,solu2,total2;
    private JPanel  panel1, panel2, panel3, panel4, panel5, panel6, panel7;
    String[] deviceStrings = { "Blackberry", "Other", "Internetstick"};
    String[] date = { "Day", "Week", "Month"};
    public double email2 = 0;
    public double email1;
    static final int email50 = 50;
    static final int email100 = 100;
    static final int email150 = 150;
    static final int email200 = 200;
    static final int email250 = 250;
    static final int email300 = 300;
    public JSlider Email = new JSlider(0,300,0);
    public JSlider Web = new JSlider(0,500,0);
    public JSlider IM = new JSlider(0,500,0);
    public JSlider Video = new JSlider(0,300,0);
    public JSlider Song = new JSlider(0,200,0);
    public  JSlider App_G = new JSlider(0,100,0);
    public String currentdate;        
    public double emailvalue;
    public MonthlyData ()
    {
        super ("MonthlyData");
        setSize (1000,1100);    
        ////////////////////////////////////////////////////// panel1
        panel1 = new JPanel ();                                              
        panel1.setLayout (new GridLayout(1,1));
        panel1.setVisible(true);
        panel1.setBackground (Color.black);
        title = new JLabel ("                                     WANNA KNOW HOW MUCH DATA",JLabel.LEFT);
        title.setForeground(Color.white);
        title.setFont (new Font("Times New Roman",Font.BOLD,25));
        l1 = new JLabel ("");
        l2 = new JLabel ("");
        /////////////////////////////////////////////// panel2   
        panel2 = new JPanel ();                                             
        panel2.setLayout (new GridLayout(1,1));
        panel2.setVisible(true);
        panel2.setBackground (Color.gray);
        stitle = new JLabel ("           PICK A PHONE OR INTERNET STICK AND THEN DRAG THE .",JLabel.LEFT);
        stitle.setForeground(Color.black);
        stitle.setFont (new Font("Times New Roman",Font.BOLD,14));
        //////////////////////////////////////////////// panel3
        panel3 = new JPanel ();                                              
        panel3.setLayout (new FlowLayout());
        panel3.setVisible(true);
        panel3.setBackground (Color.white);
        l3 = new JLabel ("Select Your Device:",JLabel.RIGHT);
        l3.setForeground(Color.red);
        JComboBox devicelist = new JComboBox (deviceStrings);
        l4 = new JLabel ("Tell us how much of what's listed here you think you'll use:");
        ///////////////////////////////////////////////// panel4    
        panel4 = new JPanel ();                                               
        panel4.setLayout (new GridLayout(6,7));
        panel4.setVisible(true);
        panel4.setBackground (Color.white);
        l5 = new JLabel ("Email");
        l6 = new JLabel ("WebPage");
        l7 = new JLabel ("Instant Message");
        l8 = new JLabel ("Streaming Video");
        l9 = new JLabel ("Song");
        l10 = new JLabel ("Application or Game");    
        l11 = new JLabel (""+" Emails");
        final JComboBox date1 = new JComboBox (date);
           date1.addActionListener(new ActionListener() {//add actionlistner to listen for change
            @Override
            public void actionPerformed(ActionEvent e) {    
                String s = (String) date1.getSelectedItem();//get the selected item    
                switch (s) {//check for a match
                    case "Day":
                        emailvalue = 30;                            
                        break;
                    case "Week":
                        emailvalue = 4;                            
                        break;
                    case "Month":
                        emailvalue = 1;
                        System.out.println("Month selected, emailvalue:" + emailvalue);
                        break;                        
                }
            }
        });

        l12 = new JLabel ("   0.002 MB");    
        l13 = new JLabel (""+email2+" MB");
        double web1 = 0;
        l14 = new JLabel (""+web1+" WebPages");
        JComboBox date2 = new JComboBox (date);
        l15 = new JLabel ("  0.07 MB");
        double web2 = 0;
        l16 = new JLabel (""+web2+" MB");
        double IM1 = 0;
        l17 = new JLabel (""+IM1+" IMs");
        JComboBox date3 = new JComboBox (date);
        l18 = new JLabel ("  0.005 MB");
        double IM2 = 0;
        l19 = new JLabel (""+IM2+" MB");
        double video1 = 0;
        l20 = new JLabel (""+video1+" Minutes");
        JComboBox date4 = new JComboBox (date);
        l21 = new JLabel ("   1 MB");
        double video2 = 0;
        l22 = new JLabel (""+video2+" MB");
        double song1 = 0;
        l23 = new JLabel (""+song1+" MP3's");
        JComboBox date5 = new JComboBox (date);
        l24 = new JLabel ("    4 MB");
        double song2 = 0;
        l25 = new JLabel (""+song2+" MB");
        double app1 = 0;
        l26 = new JLabel (""+app1+" App/Games");
        JComboBox date6 = new JComboBox (date);
        l27 = new JLabel ("   0.44 MB");
        double app2 = 0;
        l28 = new JLabel (""+app2+" MB");
        //////////////////////////////////////////////// panel5
        panel5 = new JPanel ();                                            
        panel5.setLayout (new GridLayout(1,1));
        panel5.setVisible(true);
        panel5.setBackground (Color.white);
        solu1 = new JLabel ("WE THINK YOUR MONTHLY DATA USAGE WILL BE:");
        solu1.setForeground (Color.red);
        solu1.setFont (new Font("Times New Roman",Font.BOLD,20));
        double total = 0;
        total2 = new JLabel (""+total+" MB");
        total2.setForeground (Color.red);
        total2.setFont (new Font("Times New Roman",Font.BOLD,20));
        /////////////////////////////////////////// panel6
        panel6 = new JPanel ();
        panel6.setLayout (new GridLayout (1,1));
        panel6.setVisible (true);
        panel6.setBackground (Color.white);
        Usage = new JLabel ("Usage per unit");
        Usage.setForeground (Color.red);
        Estimated = new JLabel ("     Estimated total");
        Estimated.setForeground (Color.red);
        ////////////////////////////////////////////// panel7
        panel7 = new JPanel ();
        panel7.setLayout (new GridLayout (1,1));
        panel7.setVisible (true);
        panel7.setBackground (Color.white);
        solu2 = new JLabel ("Data usage varies by device and other factors and the .");
        solu2.setForeground (Color.black);
        /////////////////////////////////////////////////// Implements
        Container container = getContentPane ();
        container.setLayout (null);      //changed
        container.setBackground (Color.white);

        container.add (panel1).setBounds (0,0,1000,120);        //panel1
        panel1.add (title).setSize(500,500);

        container.add (panel2).setBounds (0,100,1000,120);      //panel2
        panel2.add (stitle);

        container.add (panel3).setBounds (0,220,1000,100);      //panel3
        panel3.add (l3);
        panel3.add (devicelist);
        panel3.add (l4);

        container.add (panel4).setBounds (0,340,1000,150);      //panel4
        Email.addChangeListener(new SliderHandler());
        Web.addChangeListener(new SliderHandler());
        IM.addChangeListener(new SliderHandler());
        Video.addChangeListener(new SliderHandler());
        Song.addChangeListener(new SliderHandler());
        App_G.addChangeListener(new SliderHandler());

        panel4.add (l5);
        panel4.add (Email);
        panel4.add (l11);
        panel4.add (date1);
        panel4.add (l12);
        panel4.add (l13);
        panel4.add (l6);
        panel4.add (Web);
        panel4.add (l14);
        panel4.add (date2);
        panel4.add (l15);
        panel4.add (l16);
        panel4.add (l7);
        panel4.add (IM);
        panel4.add (l17);
        panel4.add (date3);
        panel4.add (l18);
        panel4.add (l19);
        panel4.add (l8);
        panel4.add (Video);
        panel4.add (l20);
        panel4.add (date4);
        panel4.add (l21);
        panel4.add (l22);
        panel4.add (l9);
        panel4.add (Song);
        panel4.add (l23);
        panel4.add (date5);
        panel4.add (l24);
        panel4.add (l25);
        panel4.add (l10);
        panel4.add (App_G);
        panel4.add (l26);
        panel4.add (date6);
        panel4.add (l27);
        panel4.add (l28);
        container.add (panel5).setBounds (210,540,1000,50);    // panel5
        panel5.add (solu1);
        panel5.add (total2);
        container.add (panel6).setBounds (670,220,200,220);      // panel6
        panel6.add (Usage);
        panel6.add (Estimated);

        container.add (panel7).setBounds (210,580,530,50);    // panel7
        panel7.add (solu2);

        Email.setOpaque(false);
        Web.setOpaque(false);
        IM.setOpaque(false); 
        Video.setOpaque(false); 
        Song.setOpaque(false);
        App_G.setOpaque(false); 
        setVisible (true);
    }

    public static void main (String[] args)
    {
        MonthlyData application = new MonthlyData ();
    }        

    public class SliderHandler implements ChangeListener
    {
        public void stateChanged(ChangeEvent e)
        {
            if (e.getSource().equals(Email))
            {
                l11.setText(""+Email.getValue());    
                email2= (Email.getValue() * 0.002);
                l13.setText(email2 + "MB");
            }
            if (e.getSource().equals(Web))
            {
                l14.setText(""+Web.getValue());
            }    
            if (e.getSource().equals(IM))
            {
                l17.setText(""+IM.getValue());
            }    
            if (e.getSource().equals(Video))
            {
                l20.setText(""+Video.getValue());
            }    
            if (e.getSource().equals(Song))
            {
                l23.setText(""+Song.getValue());
            }    
            if (e.getSource().equals(App_G))
            {
                l26.setText(""+App_G.getValue());
            }    
        }
    }

    public void paint (Graphics g)
    {
        super.paint(g);    
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Sunny
  • 69
  • 1
  • 2
  • 8

4 Answers4

8

1) Well firstly you'd need an ActionListener added to the JComboBox to listen for selected item changes.

2) Within actionPerformed(...) of ActionListener get the selected item via getSelectedItem() and cast to a String as the method returns an Object.

3) Use a switch statement to check which one the selected item matches i.e day, month,week and than assign a value accordingly.

For example:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class MonthlyData {

    public double emailvalue;
    private String[] date = {"Day", "Week", "Month"};
    private JFrame frame;

    public MonthlyData() {
        frame = new JFrame();//use an instance rather than extending the class
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        final JComboBox date1 = new JComboBox(date);

        ActionListener cbActionListener = new ActionListener() {//add actionlistner to listen for change
            @Override
            public void actionPerformed(ActionEvent e) {

                String s = (String) date1.getSelectedItem();//get the selected item

                switch (s) {//check for a match
                    case "Day":
                        emailvalue = 1.1;
                        System.out.println("Day selected, emailvalue:" + emailvalue);
                        break;
                    case "Week":
                        emailvalue = 2.2;
                        System.out.println("Week selected, emailvalue:" + emailvalue);
                        break;
                    case "Month":
                        emailvalue = 3.3;
                        System.out.println("Month selected, emailvalue:" + emailvalue);
                        break;
                    default:
                        emailvalue = 4.4;
                        System.out.println("No match selected, emailvalue:" + emailvalue);
                        break;
                }
            }
        };

        date1.addActionListener(cbActionListener);

        frame.add(date1);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MonthlyData();
            }
        });
    }
}

UPDATE:

After you added an SSCCE and commented:

If you go down to the SliderHandler class, type in email2=(Email.getValue() * 0.002)*emailvalue; if you do the label wont update in real time. but if you replace emailvalue with lets say 5, it will update

The problem is

On start up the emailvalue is not yet set. It will only be set on the first time the selected item/index is changed.

To solve this we must simply give it a default selected value by calling setSelectedItem(..) before its visible:

final JComboBox date1 = new JComboBox(date);
date1.addActionListener(new ActionListener() {//add actionlistner to listen for change
    @Override
    public void actionPerformed(ActionEvent e) {

        String s = (String) date1.getSelectedItem();//get the selected item

        switch (s) {//check for a match
            case "Day":
                emailvalue = 30;
                break;
            case "Week":
                emailvalue = 4;
                break;
            case "Month":
                emailvalue = 1;
                System.out.println("Month selected, emailvalue:" + emailvalue);
                break;
        }
    }
});
date1.setSelectedItem(date[0]);//set Day as default selected item/emailvalue

Suggestions on your code:

  • Swing components should be created and manipulated on EDT via SwingUtilities.invokeLater(Runnable r) block.

  • Dont override paint(..) of JFrame rather add JPanel and override paintComponent(Graphics g)

  • Dont call setBounds or setSize rather use an appropriate LayoutManager and/or override getPreferredSize() of JPanel and return Dimensions which fit its contents than call pack() on JFrame before setting it visible.

  • Dont extend JFrame unnecessarily

  • Dont implement ActionListener on the class unless the class will be used as one and or its methods must be exposed to other classes

River
  • 8,585
  • 14
  • 54
  • 67
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • I didnt post all the code, I only posted the combobox bits. Im making a program in which I have to add Jpanels etc, so I thought itd be easier to extend Jframe – Sunny Jan 13 '13 at 17:54
  • 1
    @Sunny No this is bad practice we should only extend a class if we want to add functionality that doesnt already exist/is beyond the capabilities through simple method calls. Also seen as you are new please have a read on: [How does accepting an answer work?](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) as this will apply once you have found a suitable solution. – David Kroukamp Jan 13 '13 at 17:58
  • 1
    It makes sure the compiler knows we are overriding a specific method from another class. Thus if we dont override the method correctly i.e we put the wrong name or something, a compiler error would be given as we added the `@Override` annotation. If we didnt it would have compiled and assumed we aded a new method. See [here](http://stackoverflow.com/questions/94361/when-do-you-use-javas-override-annotation-and-why) for more – David Kroukamp Jan 13 '13 at 18:00
  • Again if I use (new ActionListener) I get a error that says its abstract and cannot be instantiated – Sunny Jan 13 '13 at 18:02
  • @Sunny I have tested my code it compiles fine, please copy and paste my snippet and test it rather than copying pieces. Also Id need to see code to give you a reason for the error. Tough its sounds like you are trying to do `ActionListener al=new ActionListener();` which cannot be done as this class is abstract and thus must override `actionPerformed` – David Kroukamp Jan 13 '13 at 18:03
  • @Sunny edit your question to reflect an [SSCCE](http://sscce.org). Than I can see what up with your code. – David Kroukamp Jan 13 '13 at 18:12
  • Basically what Im doing is getting a value from a Jslider and multiplying it by the emailvalue to update a label in real time. But If I multiply it by emailvalue, it doesnt update in real time, but if I replace email value with a number, it updates – Sunny Jan 13 '13 at 18:26
  • If you go down to the SliderHandler class, type in email2=(Email.getValue() * 0.002)*emailvalue; if you do the label wont update in real time. but if you replace emailvalue with lets say 5, it will update – Sunny Jan 13 '13 at 18:28
1

Add a actionListener to the JComboBox:

JComboBox date1 = new JComboBox (date);
date1.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
        String selectedItem = (String) date1.getSelectedItem();
        if(selectedItem.equals("date")
        {
            emailValue = 30;
        }
    }
});

Since your class already implements ActionListener, you can do this as well (as an alternative to the above):

Add a actionCommand to your JComboBox:

JComboBox date1 = new JComboBox (date);
date1.setActionCommand("date1");
date1.addActionListener(this)

Note: date1 should be a class level variable (field) so that it is accessible to the actionPerformed method.

Add an actionPerformed method to the class (you most likely already have this).

public void actionPerformed(ActionEvent ae)
{
    if(ae.getActionCommand().equals("date1")
    {
        String selectedItem = date1.getSelectedItem();
        if(selectedItem.equals("date")
        {
           emailValue = 30;
        }
        …
        //Note: From Java 7, you can use Strings with switch
    }
}
Nivas
  • 18,126
  • 4
  • 62
  • 76
  • I tried the above code Nivas but when I run it I get a error saying java.awt.event.ActionListener is abstract; cannot be instantiated. If I try date1.addActionListener(this) I get a error that says cannot find variable data 1 and it highlights data1.getselectedItem() – Sunny Jan 13 '13 at 17:39
  • @Sunny: You need to have `date1.addActionListener(this)` (added to answer). For this date1 should be a class level variable. – Nivas Jan 13 '13 at 17:40
  • I tried your second answer and I get an error which says incompatible types and it highlights String selectedItem = date1.getselectedItem(); – Sunny Jan 13 '13 at 17:51
  • -1 for uncompilable code after OP has said its not working giving the line and all. @Sunny Please see [my answer](http://stackoverflow.com/a/14306189/1133011) you should cast the returned value to a `String` as said in my answer (the method returns an `Object`) like so: `String selectedItem = (String)date1.getselectedItem();` – David Kroukamp Jan 13 '13 at 17:54
  • @Sunny You need to cast to String as getSelectedItem returns a Object. I have updated the code. @ DavidKroukamp The answer is just a pointer to the right approach. Basic compiler errors should be easy to resolve. My intention is not to provide working code. My intention is to provide the right approach. – Nivas Jan 13 '13 at 18:01
  • @Nivas lifted my downvote accordingly though I disagree with *My intention is not to provide working code* than you are in fact not helping at all if the code does not work, rather just complicating the matter. – David Kroukamp Jan 13 '13 at 18:11
  • @DavidKroukamp My point was that it wouldn't be that difficult to identify and resolve the error. Still, I agree it is not fair to give an snippet that would not compile. – Nivas Jan 13 '13 at 21:56
0

Simply have a map and use it in selection listener.

HashMap<String,Integer> map = new HashMap();
map.put("Day",1);
map.put("Week",7);
map.put("Month",30);
vels4j
  • 11,208
  • 5
  • 38
  • 63
-2

this is just an example

implements ItemListener //on your class addItemListener to your combo


public void itemStateChanged(ItemEvent e) {

     String i = "" + cboMonth.getSelectedItem();
     String b = "" + cboMonth1.getSelectedItem();
     System.out.println("The number  Start:" + i);
     System.out.println("The number End:" + b);
     if(e.getStateChange() == ItemEvent.SELECTED){
          int mSatrt = Integer.parseInt(i);
          int mEnd = Integer.parseInt(b);
          // System.out.println("The number m FROM ITEMSTATE is:" + mSatrt +"   == " + "M1 is :  "+ mEnd);

          if(mSatrt >= mEnd){ JOptionPane.showMessageDialog (this, " Next Date Payment est moin que la date de paiement" + mSatrt + ">=" + "" + mEnd);}
              //else if (mSatrt> mEnd) { JOptionPane.showMessageDialog (this, "la date Erroné" + mSatrt + ">" + "" + mEnd);}
              //else{JOptionPane.showMessageDialog (this, "la date Erroné" + mSatrt + "=" + "" + mEnd);}
          }
    }
}
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
driss
  • 1