File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

add item in jcombobox from database

 
Ah Ling
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Rob Spoor
Sheriff
Pie
Posts: 20396
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20396
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic