Results 1 to 12 of 12
  1. #1
    pinks_70986 is offline Member
    Join Date
    Feb 2009
    Posts
    18
    Rep Power
    0

    Smile how unhighlight text in jcombobox?

    when i type in editable jcombobox the text automatically get selected(highlighted) i dont want that...anyone knows how to do it?

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    You are trying to remove a feature of JComboBox. I'm sure that it can be done, but I would suggest leaving things alone. When you modify a standard component that way, you make your GUI non-standard and therefore more difficult to use.

  3. #3
    pinks_70986 is offline Member
    Join Date
    Feb 2009
    Posts
    18
    Rep Power
    0

    Default

    but then i am not able to type continuosly in the combobox.... after every letter i typed i have to hit right key to remove selection otherwise the next letter i type overwrites previous letter

  4. #4
    pinks_70986 is offline Member
    Join Date
    Feb 2009
    Posts
    18
    Rep Power
    0

    Default

    This is wat i want to do -I want that if user enters a in the combo box then it should search database and return strings starting with a nd shuold be displayed in dropdown list of combo box(If possible the combobox must automatically dropdown). also if there are no words starting with enterd string then the dropdown list should not show anything.

  5. #5
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    From my understanding of what you need,

    all you need to do is store the value currently in the box,

    search the database for values that start with said value, and add them to the combo box,

    set the current item back to the combo box


    Just add a key listener to the combo box, in one of those methods call a refresh methods that goes through the database and adds the words/items that start with the value currently entered in

  6. #6
    pinks_70986 is offline Member
    Join Date
    Feb 2009
    Posts
    18
    Rep Power
    0

    Default

    I have done all that.I have created jcombobox with DefaultJlistModel.whenevr a key is pressed it searches database for strings starting with enterd letter.my current problem is if type in the JComboBox text gets highlighted so i cant continue typing a complete word...

  7. #7
    pinks_70986 is offline Member
    Join Date
    Feb 2009
    Posts
    18
    Rep Power
    0

    Default

    namecombo.getEditor().getEditorComponent().addKeyL istener(new KeyAdapter(){
    public void keyReleased(KeyEvent e)
    {



    letter=(String)namecombo.getEditor().getItem();


    if(letter.equals(""))
    {
    list.removeAllElements();
    namecombo.hidePopup();
    }


    if(e.getKeyCode()==KeyEvent.VK_BACK_SPACE)
    {

    list.removeAllElements();

    if(letter.equals(""))
    {
    list.removeAllElements();
    namecombo.hidePopup();
    return;
    }

    namecombo.setSelectedItem(letter);

    try {
    SimpleOraJava orac=new SimpleOraJava();
    orac.csearch(letter);
    } catch (SQLException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    }

    else
    if(letter.endsWith(""+e.getKeyChar()))
    {

    try {

    list.removeAllElements();
    SimpleOraJava orac=new SimpleOraJava();
    orac.csearch(letter);

    }
    catch (SQLException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }}

    else if(e.getKeyCode==KeyEvent.VK_RIGHT)
    {

    namecombo.hidePopup();

    }


    }
    });



    namecombo.setEditable(true);
    add(namecombo, cc.xy(9, 7));


    csearch is method in another class dat handels database connectivity

  8. #8
    pinks_70986 is offline Member
    Join Date
    Feb 2009
    Posts
    18
    Rep Power
    0

    Default

    public void csearch(String letter)throws SQLException

    {
    String word=null;
    option=false;
    String SQL="Select cname from company";
    Statement stat1 = conn.createStatement();
    stat1.executeQuery(SQL);
    ResultSet rs = stat1.executeQuery(SQL);
    int i=0;
    while(rs.next())
    {
    word=rs.getString(1);
    word=word.trim();
    if(word.startsWith(letter))
    {
    a.list.addElement(word); option=true;
    }

    }
    if(option==true)
    {

    a.namecombo.showPopup();

    }
    else
    {
    a.namecombo.getEditor().setItem(letter);
    a.namecombo.hidePopup();
    }

    stat1.close();
    conn.close();

    }

  9. #9
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    So basically all you need to do is set the caret position then?

    The problem with JComboBox is that there is no method to do this, so you need to obtain the TextField component underneath.

    Try casting the JComboBox's Editor EditorComponent into a JTextField.

    JTextField field = (JTextField)comboBox().getEditor().getEditorCompon ent()

    Then set the caret position on that after you add the item

  10. #10
    pinks_70986 is offline Member
    Join Date
    Feb 2009
    Posts
    18
    Rep Power
    0

    Default

    letter=(String)namecombo.getEditor().getItem();
    JTextField field = (JTextField)namecombo.getEditor().getEditorCompone nt();
    field.setCaretPosition(letter.length());
    i did this bt still the text is highlighting

  11. #11
    sachanurag is offline Member
    Join Date
    Jun 2012
    Posts
    1
    Rep Power
    0

    Default Re: how unhighlight text in jcombobox?

    Quote Originally Posted by pinks_70986 View Post
    when i type in editable jcombobox the text automatically get selected(highlighted) i dont want that...anyone knows how to do it?


    After the showPopUp() method you need to add just one line.

    String text = jTextFieldName.getText();
    jTextFieldName.setSelectionStart(text.length());// where text is the current value of combo box.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,302
    Rep Power
    20

    Default Re: how unhighlight text in jcombobox?

    Don't post to old dead threads. This one's more than 3 years old.

    db

    THREAD CLOSED
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. JComboBox
    By pinks_70986 in forum New To Java
    Replies: 2
    Last Post: 02-19-2009, 05:26 PM
  2. I need help with JComboBox
    By eva21 in forum New To Java
    Replies: 1
    Last Post: 11-28-2008, 10:05 PM
  3. JCombobox
    By daniel50096230 in forum NetBeans
    Replies: 1
    Last Post: 09-21-2008, 12:23 PM
  4. JComboBox
    By Fosters in forum AWT / Swing
    Replies: 0
    Last Post: 08-10-2008, 01:22 PM
  5. jcombobox
    By Freddie in forum AWT / Swing
    Replies: 4
    Last Post: 05-11-2007, 12:48 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
  •