Results 1 to 7 of 7
  1. #1
    dalu is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default ComboBoX dropdown

    Hi,
    I have a frame with a combobox. the combox is editable. It should show [Select] in its text field when we open the frame but in the drop down, [Select] should not be shown as a drop down item.

    Now I have implemented the combobos as:
    combo.addItem("Select");
    combo.addItem("A");
    combo.addItem("B");
    and so on...
    But the Select is an item and will be shown in the drop down
    Can anyone help me plz....

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    The Combo Box Prompt might be what you are looking for.

  3. #3
    dalu is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    Yes...Its the same thing I wanted..
    Now my code part looks like this..

    combo.addItem("A");
    combo.addItem("B");
    combo.setRenderer(new PromptComboBoxRenderer("Select "));

    Did I missed something???It doesnt seem to work...

    Thnks fr the help...

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    You didn't miss anything. Only a single line of code is required. It was tested on XP using JDK6.7.

    Post your Short, Self Contained, Correct Example that demonstrates the problem.

  5. #5
    dalu is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    Hi,
    I am involved in a project for creating a library database.
    My exact code looks like this:

    public combobox()
    {
    JFrame frame = new JFrame("Library Database");
    String Books[] = {"Maths","Physics","Chemistry","Commerce"};
    Panel panel = new JPanel();
    combo = new JComboBox(Books);
    combo.setRenderer(new PromptComboBoxRenderer("[Select] "));
    panel.add(combo);
    }

    and this selelcted item is used for display in a text field. But it doesn't work.

    Thanks for ur help once again...

  6. #6
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    The combo box selects the first item by default. Once you've set the renderer and the items into the combo box, clear the default selection:
    Java Code:
    combo.setSelectedIndex(-1);

  7. #7
    dalu is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

Similar Threads

  1. dropdown with other option
    By vallaru in forum Web Frameworks
    Replies: 1
    Last Post: 02-28-2011, 09:00 AM
  2. dropdown onchange
    By srivempala in forum New To Java
    Replies: 0
    Last Post: 01-23-2011, 11:54 AM
  3. dynamically populate a dropdown box using jsp
    By sandy1000 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 12-27-2010, 02:21 PM
  4. To populate dropdown in Struts 1.3.8
    By techV in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-27-2010, 08:49 AM
  5. Help with dropdown list
    By barney in forum AWT / Swing
    Replies: 1
    Last Post: 08-07-2007, 07:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •