combo boxes and actionlisteners

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • carlos123
    New Member
    • Sep 2007
    • 53

    #1

    combo boxes and actionlisteners

    this is pretty basic I'm sure for most of you, but im having alot of trouble with the 2. Here is my code its BlueJ. Im sure this is simple for most of you, but im totally lost. Please try to explain to me what you did. here the code below. i know its messy but watever lol. Thanks for your help!


    import java.awt.*;
    import java.awt.event. *;
    import javax.swing.*;

    public class Main
    {
    JFrame f1;
    JPanel p1,p2;

    GP g1;
    JButton Bok,Bcc;
    JTextField first,last,age, street,city,gen der;

    public Main()
    {


    String[] year = { "1980", "1981", "1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008" , "2009", "2010"};

    JComboBox yearcombo = new JComboBox(year) ;
    yearcombo.setSe lectedIndex(29) ;
    yearcombo.addAc tionListener(ac tionEvent());
    String[] month = { "January", "Febuary", "March", "April", "May", "June", "July", "October", "November", "December"} ;
    JComboBox monthcombo = new JComboBox(month );
    monthcombo.setS electedIndex(9) ;
    monthcombo.addA ctionListener(t his);
    String[] state = { "Alabama AL", "Alaska AK" ,"Arizona AZ", "Arkansas AR", "California CA" ,"Colorado CO", "Connecticu t CT" ,"Delaware DE", "Florida FL","Georgia GA", "Hawaii HI"," Idaho ID","Illinois IL" ,"Indiana IN", "Iowa IA", "Kansas KS" ,"Kentucky KY" ,"Louisiana LA", "Maine ME", "Maryland MD" ,"Massachuse tts MA", "Michigan MI" ,"Minnesota MN" ,"Mississipp i MS" ,"Missouri MO", "Montana MT" ,"Nebraska NE", "Nevada NV" ,"New Hampshire NH" ,"New Jersey NJ", "New Mexico NM", "New York NY", "North Carolina NC", "North Dakota ND" ,"Ohio OH" ,"Oklahoma OK", "Oregon OR" ,"Pennsylvan ia PA" ,"Rhode Island RI", "South Carolina SC", "South Dakota SD" ,"Tennessee TN", "Texas TX" ,"Utah UT", "Vermont VT" ,"Virginia VA" ,"Washington WA", "West Virginia WV", "Wisconsin WI" ,"Wyoming WY"};
    JComboBox statecombo = new JComboBox(state );
    statecombo.setS electedIndex(49 );
    statecombo.addA ctionListener(t his);
    String[] gender = {"M","F"};
    JComboBox gendercombo = new JComboBox(gende r);
    gendercombo.set SelectedIndex(1 );
    gendercombo.add ActionListener( this);
    f1 = new JFrame("fafafff das");
    f1.setSize(832, 669);
    Container c1 = f1.getContentPa ne();


    p1 = new JPanel();
    p1.setSize(1000 ,730);
    p1.setLayout(ne w BorderLayout()) ;

    first = new JTextField("Fir st Name");
    last = new JTextField("Las t Name");
    age = new JTextField("Age ");
    street = new JTextField("Str eet Address");
    city = new JTextField("Cit y");
    clear = new JButton("Clear" );
    clear.addAction Listener(new ButtonListener( ));
    submit = new JButton("Submit ");
    submit.addActio nListener(new ButtonListener( ));
    Bok = new JButton("Save") ;
    Bok.addActionLi stener(new ButtonListener( ));
    Bcc = new JButton("Quit") ;
    Bcc.addActionLi stener(new ButtonListener( ));

    p2 = new JPanel(); //panel to add JLabel and TextField

    g1 = new GP();
    p1.add(g1,Borde rLayout.CENTER) ;


    g1.add(first);
    g1.add(last);
    g1.add(age);
    g1.add(year);
    g1.add(gender);
    g1.add(street);
    g1.add(city);
    g1.add(state);
    p2.add(submit);
    p2.add(Bok);
    p2.add(clear);
    p2.add(Bcc);

    p1.add(p2,Borde rLayout.SOUTH); //adds p2 to south (Bottom) Graphics Panel will be added to Center
    c1.add(p1);

    f1.show();

    }





    public void actionPerformed (ActionEvent e)
    {
    JComboBox yearcombo = (JComboBox)e.ge tSource();
    String year = (String)cb.getS electedItem();

    }





    private class ButtonListener implements ActionListener
    {
    public void actionPerformed (ActionEvent event)
    {


    if (event.getSourc e() == clear)
    {


    }

    if (event.getSourc e() == Bok)
    {
    g1.requestFocus ();

    }

    if (event.getSourc e() == Bcc)
    {

    f1.dispose();
    }
    if (event.getSourc e() == submit)
    {


    }

    g1.repaint();
    }
    }


    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by carlos123
    this is pretty basic I'm sure for most of you, but im having alot of trouble with the 2. Here is my code its BlueJ. Im sure this is simple for most of you, but im totally lost. Please try to explain to me what you did. here the code below. i know its messy but watever lol. Thanks for your help!
    Please do ask a question that's on target: what doesn't work? What doesn't it do
    what you were expecting it to do? What does your code do that you don't want
    it to do? Please don't just dump your code here hoping that some kind soul
    will repair it without knowing what you want it to do. Programming and design
    of code doesn't work like that and this forum doesn't work like that.

    kind regards,

    Jos

    Comment

    • carlos123
      New Member
      • Sep 2007
      • 53

      #3
      i just want the combo boxes to compile and just add a basic actionlistener to them

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by carlos123
        i just want the combo boxes to compile and just add a basic actionlistener to them
        So the code does not compile?
        What error message do you get then.

        P.S Do not double post.

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          QUOTE from carlos123
          [CODE=java]
          import java.awt.*;
          import java.awt.event. *;
          import javax.swing.*;

          public class Main
          {
          JFrame f1;
          JPanel p1,p2;

          GP g1;
          JButton Bok,Bcc;
          JTextField first,last,age, street,city,gen der;

          public Main()
          {


          String[] year = { "1980", "1981", "1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989", "1990", "1991", "1992", "1993", "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007", "2008" , "2009", "2010"};

          JComboBox yearcombo = new JComboBox(year) ;
          yearcombo.setSe lectedIndex(29) ;
          yearcombo.addAc tionListener(ac tionEvent());
          String[] month = { "January", "Febuary", "March", "April", "May", "June", "July", "October", "November", "December"} ;
          JComboBox monthcombo = new JComboBox(month );
          monthcombo.setS electedIndex(9) ;
          monthcombo.addA ctionListener(t his);
          String[] state = { "Alabama AL", "Alaska AK" ,"Arizona AZ", "Arkansas AR", "California CA" ,"Colorado CO", "Connecticu t CT" ,"Delaware DE", "Florida FL","Georgia GA", "Hawaii HI"," Idaho ID","Illinois IL" ,"Indiana IN", "Iowa IA", "Kansas KS" ,"Kentucky KY" ,"Louisiana LA", "Maine ME", "Maryland MD" ,"Massachuse tts MA", "Michigan MI" ,"Minnesota MN" ,"Mississipp i MS" ,"Missouri MO", "Montana MT" ,"Nebraska NE", "Nevada NV" ,"New Hampshire NH" ,"New Jersey NJ", "New Mexico NM", "New York NY", "North Carolina NC", "North Dakota ND" ,"Ohio OH" ,"Oklahoma OK", "Oregon OR" ,"Pennsylvan ia PA" ,"Rhode Island RI", "South Carolina SC", "South Dakota SD" ,"Tennessee TN", "Texas TX" ,"Utah UT", "Vermont VT" ,"Virginia VA" ,"Washington WA", "West Virginia WV", "Wisconsin WI" ,"Wyoming WY"};
          JComboBox statecombo = new JComboBox(state );
          statecombo.setS electedIndex(49 );
          statecombo.addA ctionListener(t his);
          String[] gender = {"M","F"};
          JComboBox gendercombo = new JComboBox(gende r);
          gendercombo.set SelectedIndex(1 );
          gendercombo.add ActionListener( this);
          f1 = new JFrame("fafafff das");
          f1.setSize(832, 669);
          Container c1 = f1.getContentPa ne();


          p1 = new JPanel();
          p1.setSize(1000 ,730);
          p1.setLayout(ne w BorderLayout()) ;

          first = new JTextField("Fir st Name");
          last = new JTextField("Las t Name");
          age = new JTextField("Age ");
          street = new JTextField("Str eet Address");
          city = new JTextField("Cit y");
          clear = new JButton("Clear" );
          clear.addAction Listener(new ButtonListener( ));
          submit = new JButton("Submit ");
          submit.addActio nListener(new ButtonListener( ));
          Bok = new JButton("Save") ;
          Bok.addActionLi stener(new ButtonListener( ));
          Bcc = new JButton("Quit") ;
          Bcc.addActionLi stener(new ButtonListener( ));

          p2 = new JPanel(); //panel to add JLabel and TextField

          g1 = new GP();
          p1.add(g1,Borde rLayout.CENTER) ;


          g1.add(first);
          g1.add(last);
          g1.add(age);
          g1.add(year);
          g1.add(gender);
          g1.add(street);
          g1.add(city);
          g1.add(state);
          p2.add(submit);
          p2.add(Bok);
          p2.add(clear);
          p2.add(Bcc);

          p1.add(p2,Borde rLayout.SOUTH); //adds p2 to south (Bottom) Graphics Panel will be added to Center
          c1.add(p1);

          f1.show();

          }





          public void actionPerformed (ActionEvent e)
          {
          JComboBox yearcombo = (JComboBox)e.ge tSource();
          String year = (String)cb.getS electedItem();

          }





          private class ButtonListener implements ActionListener
          {
          public void actionPerformed (ActionEvent event)
          {


          if (event.getSourc e() == clear)
          {


          }

          if (event.getSourc e() == Bok)
          {
          g1.requestFocus ();

          }

          if (event.getSourc e() == Bcc)
          {

          f1.dispose();
          }
          if (event.getSourc e() == submit)
          {


          }

          g1.repaint();
          }
          }


          }[/CODE]End of Quote

          Please use CODE tags when posting. Now, what is line 22 (the line yearcombo.addAc tionListener(ac tionEvent());) supposed to do? Shouldn't it look a bit more like this:
          [CODE=java]
          yearcombo.addAc tionListener(ne w ActionListener( )
          {
          public void actionPerformed (ActionEvent e)
          {
          // your code here
          }
          });
          [/CODE]
          Greetings,
          Nepomuk

          Comment

          Working...