Results 1 to 6 of 6

Thread: How to Use Combo Boxes in Java?

  1. #1
    Join Date
    Aug 2006
    Posts
    139

    How to Use Combo Boxes in Java?

    Hi everyone,
    I am beginner for the Java programming language. I have made a small coding for the form on Java. But in that I want to add the combo box in that.!! I don't know the coding for that.!! In that combo box i want to add the name of animals so the user can click the animal that he wants. So please help me to explain how to Use Combo Boxes in Java.?? Waiting for your Responses..!!
    The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little.
    -Joe Martin

  2. #2
    Join Date
    Nov 2008
    Posts
    996

    Re: How to Use Combo Boxes in Java?

    A Combo Boxes in Java are used when there is list of variables, and you want the user to select from that list. I think that you want the combo box so that the user can select the animal from that box instead of mentioning by its own. By using the Combo Box, you can tie the users with the boundaries. A JComboBox, which lets the user choose one of several choices, can have two very different forms. The first is default form, in which features a button and a drop-down list of values. This is also called as uneditable combo box, because you cannot edit from that. And the second type is called the editable combo box, features a text field with a small button abutting it.

  3. #3
    Join Date
    Nov 2008
    Posts
    1,192

    Re: How to Use Combo Boxes in Java?

    I think that you will get some idea from the following sample of example in which I have coded combo box for some animals. Have a look at the code :
    Code:
    String[] petStrings = { "Dog", "Rabbit", "Parrot", "Cat", "Hen", "Pigeon" };
    
    JComboBox petList = new JComboBox(petStrings);
    petList.setSelectedIndex(5);
    petList.addActionListener(this);

  4. #4
    Join Date
    Mar 2008
    Posts
    672

    Re: How to Use Combo Boxes in Java?

    Before making the combo boxes you should know something that is very important. When implementing a custom model for a combo box, you should not forget that JComboBox methods work only if the data model implements the MutableComboBoxModel interface. This happens only when you are using the JComboBox for changing the items in the combo box's menu, just like insertItemAt. Also while using the uneditable combo boxes, you will have to make sure that custom model fires list data events when the combo box's data or state changes. Whenever the selection changes, immutable combo box models, whose data never changes, must fire a list data event.

  5. #5
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to Use Combo Boxes in Java?

    You will also have to handle an events on a Combo Box. so I have given you a coding that demonstrates the same :
    Code:
    public class ComboBoxDemo ... implements ActionListener {
        . . .
            petList.addActionListener(this) {
        . . .
        public void actionPerformed(ActionEvent ae) {
            JComboBox combo = (JComboBox)ae.getSource();
            String petName = (String)combo.getSelectedItem();
            updateLabel(petName);
        }
        . . .
    }

  6. #6
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to Use Combo Boxes in Java?

    If you want the code for the editable combo box, you can paste the following code :
    Code:
    String[] patternExamples = {
             "dd MMMMM yyyy",
             "dd.MM.yy",
             "MM/dd/yy",
             "yyyy.MM.dd G 'at' hh:mm:ss z",
             "EEE, MMM d, ''yy",
             "h:mm a",
             "H:mm:ss:SSS",
             "K:mm a,z",
             "yyyy.MMMMM.dd GGG hh:mm aaa"
    };
    . . .
    JComboBox patternList = new JComboBox(patternExamples);
    patternList.setEditable(true);
    patternList.addActionListener(this);

Similar Threads

  1. Replies: 4
    Last Post: 29-06-2013, 11:03 AM
  2. Replies: 8
    Last Post: 01-12-2011, 08:58 PM
  3. How to nest boxes inside parent boxes in CSS?
    By Dilbert in forum Software Development
    Replies: 5
    Last Post: 11-05-2010, 04:41 PM
  4. Reliance 649 combo VS Airtel 699 combo
    By Anish-Mumbai in forum India BroadBand
    Replies: 5
    Last Post: 05-05-2010, 09:45 AM
  5. Close Combo Box in java
    By cyber-noob in forum Software Development
    Replies: 2
    Last Post: 24-11-2009, 02:39 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,572,465.30733 seconds with 17 queries