• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

add item in jcombobox from database

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I add items in jcombobox from the database?
I have been trying and trying for hours and yet I still cant figure out.

Any examples to show for me to try out for reference?
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JComboBox. This page has a nice linked called "How to Use Combo Boxes". The page also includes all methods, one of which is "addItem".

As for the items to put in there, you will need to create a class for your database contents. I can't tell you what this class should look like because it all depends on the data inside it.
 
Ah Ling
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

Yes, i know how to add items in the jcombobox and i already have a class for my database.

Can I know if this is correct?

private JComboBox getJComboBoxStudent() {
if (jComboBoxStudent == null) {
jComboBoxStudent = new JComboBox();
jComboBoxStudent.setBounds(new Rectangle(165, 105, 166, 31));

jComboBoxStudent.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {

std.retrieveStudent();
jComboBoxStudent.addItem(std.getStudentName().toString());
}
});
}
return jComboBoxStudent;
}
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags. That will make your code easier to read.

Now, about your code. As I see it, you create your student combo box when you need it. You set its bounds but you don't add it to any control. Perhaps that is in the calling method. Either way, the setBounds is not encouraged; you should use a proper layout manager which will set the bounds for you.

Anyway, getting back to your code. You add an action listener that will retrieve another student (which one?) which you then add. Is this really what you want? Wouldn't you want to load all students from the database immediately and store those in your combo box? A combo box can store any object; it will use the result of toString() by default to display it but you can override this by using your own ListCellRenderer.
 
BWA HA HA HA HA HA HA! Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic