JcomboBox action


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: JcomboBox action

  1. #1
    Join Date
    Nov 2003
    Posts
    13

    JcomboBox action

    hello all,

    ok i have a problem assigning a actionto a JComboBox

    rather than post 200+ lines of code i thought i would show you all a image of my program and show you what i need to accomplish



    ok, i want to select a item from the JComboBox list and as a result of that item being selected all the JText fields below it must change with diffrent vlaues !

    here is the code i have so far for the action that is to accomplish this

    ====================================

    bikeList.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e) {
    JComboBox cb = (JComboBox)e.getSource();
    String bikeType = (String)cb.getSelectedIte();

    }

    });///end

    ======================================

    i relise i may have to write a user defined method to actual make the changes to the text fields and then call on that method in my "actionpreformed" method

    togive you a bigger perspective here is a link to my source code


    http://www.freewebs.com/lrrpclan/across.txt

    thanks for your help

  2. #2
    Join Date
    Mar 2003
    Posts
    834
    i relise i may have to write a user defined method to actual make the changes to the text fields and then call on that method in my "actionpreformed" method
    Exactly. What's the problem.
    ArchAngel.
    O:-)

  3. #3
    Join Date
    Nov 2003
    Posts
    13
    well like i said in the above post

    ok, i want to select a item from the JComboBox list and as a result of that item being selected all the JText fields below it must change with diffrent vlaues !
    thats the problem, how do i make it change the values in the textboxs when i click on the items in the combobox ???????????????

  4. #4
    Join Date
    Mar 2003
    Posts
    834
    What's wrong with just calling .setText(...) on each of the text boxes?
    ArchAngel.
    O:-)

  5. #5
    Join Date
    Nov 2003
    Posts
    13

    Smile

    yeah i just figured out that i could just call the .setText() method !! and that works

    however when i click on ANY item in the comboBox it will change then all to the same values

    how do i make it so when i click on a diffrent item those values will change aswell

    to explain abit more

    so far i have about 4 items in my comboBox. all my text filesd start with a defult value. when i click on the firet item the text feilds whill change the value yay! when i click on the second item the values will NOT change.
    so.....

    how do i make it so......

    if (item 1 is clicked) {

    change values

    } else if (item 2 is clicked){

    change values

    } ect ect ect.......

    please help

  6. #6
    Join Date
    Mar 2003
    Posts
    834
    In your handler code, check what the current selection of the JComboBox is by calling the getSelectedItem(...) method.

    http://java.sun.com/j2se/1.4.2/docs/...etSelectedItem()
    ArchAngel.
    O:-)

  7. #7
    Join Date
    Nov 2003
    Posts
    13
    handler code
    what the **** are you talking about man ????????

    i have posted my soruce code in my first post so feel free to take a look.

    i have already posted my question in my last thred if any one can help me SOLVE it i would be thnkfull just send me in the right drection and ill figure it out or a example would be nice.

  8. #8
    Join Date
    Mar 2003
    Posts
    834
    Handler Code: The code written which handles a particular event.

    Therefore, in this context, I'm talking about the code you've written which responds to changes in the JComboBox i.e. loading the different Bike Stats into the text boxes.
    ArchAngel.
    O:-)

  9. #9
    Join Date
    Nov 2003
    Posts
    13
    hello, is any one there.

    im still waitng on some help here...


    so far i have about 4 items in my comboBox. all my text filesd start with a defult value. when i click on the first item the text feilds whill change the value yay! when i click on the second item the values will NOT change.
    so.....

    how do i make it so......

    if (item 1 is clicked) {

    change values

    } else if (item 2 is clicked){

    change values

    } ect ect ect.......

    could some one plese provide me with some help or examples of how to accomplish this as i am really stuck here, please porvide a bit more help that other guy is just giving me the run around and is no help at all !

  10. #10
    Join Date
    Mar 2003
    Posts
    834
    Joshua...


    ...insulting the only person that's been trying to help you is not a good idea...

    I let this go:
    what the **** are you talking about man ????????
    ...but now I'm getting rather irritated.

    I think you're expecting the answer handed to you on a plate. You might have to do some work yourself....

    OK...firstly, when I run your program, the following does *not* happen:
    when i click on the first item the text feilds whill change the value
    ...instead absolutely nothing happens when I select a new item in the ComboBox. This is because your handler code (see previous definition) is doing nothing:
    Code:
    bikeList.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        JComboBox cb = (JComboBox)e.getSource();
        String bikeType = (String)cb.getSelectedItem();
      }
    });
    This code isn't *doing* anything. You know which bike has just been selected, so you now know which data you have to load:
    Code:
    bikeList.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        JComboBox cb = (JComboBox)e.getSource();
        String bikeName = (String)cb.getSelectedItem();
        loadBikeData(bikeName);
      }
    });
    You need to write the method loadBikeData that will move through each of the text boxes, setting the text appropriate to the bike name which has just been passed to it.

    A few more things:

    1. It's the convention in Java for classes to begin with a capital letter i.e. 'Across', not 'across'.

    2. Names such as 'label1' are not particularly helpful.

    3. Your indentation is a bit all over the place. See the way I've posted your code, this is more conventional.
    ArchAngel.
    O:-)

  11. #11
    Join Date
    Nov 2003
    Posts
    13
    thank you

    if you posted that in the first place !! that would have been much more helpful rather than giving me information which was no help at all as you did before(just a tip for next time)
    but thanks anyway..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center