Results 1 to 12 of 12
- 02-19-2009, 07:09 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 18
- Rep Power
- 0
- 02-19-2009, 08:12 PM #2
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.
- 02-19-2009, 08:26 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 18
- Rep Power
- 0
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
- 02-19-2009, 08:29 PM #4
Member
- Join Date
- Feb 2009
- Posts
- 18
- Rep Power
- 0
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.
- 02-19-2009, 08:40 PM #5
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
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
- 02-19-2009, 08:49 PM #6
Member
- Join Date
- Feb 2009
- Posts
- 18
- Rep Power
- 0
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...
- 02-19-2009, 08:58 PM #7
Member
- Join Date
- Feb 2009
- Posts
- 18
- Rep Power
- 0
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
- 02-19-2009, 09:00 PM #8
Member
- Join Date
- Feb 2009
- Posts
- 18
- Rep Power
- 0
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();
}
- 02-19-2009, 09:41 PM #9
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
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
- 02-20-2009, 05:53 AM #10
Member
- Join Date
- Feb 2009
- Posts
- 18
- Rep Power
- 0
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
- 06-01-2012, 12:57 PM #11
Member
- Join Date
- Jun 2012
- Posts
- 1
- Rep Power
- 0
- 06-01-2012, 01:13 PM #12
Similar Threads
-
JComboBox
By pinks_70986 in forum New To JavaReplies: 2Last Post: 02-19-2009, 05:26 PM -
I need help with JComboBox
By eva21 in forum New To JavaReplies: 1Last Post: 11-28-2008, 10:05 PM -
JCombobox
By daniel50096230 in forum NetBeansReplies: 1Last Post: 09-21-2008, 12:23 PM -
JComboBox
By Fosters in forum AWT / SwingReplies: 0Last Post: 08-10-2008, 01:22 PM -
jcombobox
By Freddie in forum AWT / SwingReplies: 4Last Post: 05-11-2007, 12:48 AM


LinkBack URL
About LinkBacks


Bookmarks