1

I'm creating a simple project using hibernate. I know how to display records on JTable as well as JList but I don't know how can I display records from the JComboBox. Anyway here are my code and guide me on displaying database record to the JComboBox?

I don't have any error or anything but it only show one record and that is the last record I know it is on the loop, but can't still figure out how to display all record. So here are my code.

Variables:

private Object[] loadName;

Methods on Loading and retrieving data:

public Object[] LoadSupplier(){
    b = a.openSession();
    b.beginTransaction();
    Query query = b.createQuery("FROM Supplier");
    @SuppressWarnings("unchecked")
    ArrayList<Supplier> load = (ArrayList<Supplier>) query.list();
    b.getTransaction().commit();
    b.close();
    for(Supplier supply : load){
        loadName = new Object[]{supply.getSupplierName()};
    }
    return loadName;
}

And for showing it to the database:

comboCategory = new JComboBox(LoadSupplier());

What I did is I call the method directly by putting it as the JComboBox value :) Tell me if I'm doing it right. And what is the best way on achieving the desired output?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Zyrax
  • 171
  • 2
  • 7
  • 16

1 Answers1

3

You're re-assigning the loadName variable each time the for-loop iterates, and so you shouldn't be surprised that none of the previous data is saved. Why not instead create a DefaultComboBoxModel<E> object before the for loop, and then add to the model inside of the loop with each iteration?

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Yeah your right, that's why only the last record show I never thought of that. :D Is there any solution without using model, like a simple storing to the arraylist, is that a better solution or not? – Zyrax Sep 14 '12 at 02:10
  • @Jerome: a `Vector` will work as well, but why not use a `DefaultComboBoxModel` object? – Hovercraft Full Of Eels Sep 14 '12 at 02:12
  • Vector is also good, but maybe your right to get the desired output I should use the DefaultComboBoxModel. :D – Zyrax Sep 14 '12 at 02:14
  • 1
    @Jerome: There's a possibly related example [here](http://stackoverflow.com/a/2531942/230513) that uses an implicit `DefaultComboBoxModel`. – trashgod Sep 14 '12 at 02:28
  • 1+ to @trashgod's great example. – Hovercraft Full Of Eels Sep 14 '12 at 02:33
  • @trashgod sorry but how did the Jcombobox display the records from database I don't seems to understand the code :D I'm newbie to hibernate. – Zyrax Sep 14 '12 at 02:44
  • I already do the default comboBoxmodel but it always display an object even though I declare the variable as String. Does DefaultComboBoxModel support Object datatype? – Zyrax Sep 14 '12 at 03:32
  • @Jerome: DefaultComboBoxModel has *always* supported Object data type. Since Java 7 (I believe), it has supported generics as well. Your problem may be with your JComboBox -- do you give it a custom renderer? You may want to do this. – Hovercraft Full Of Eels Sep 14 '12 at 03:35
  • why do I still need to render it I just want it to be display in the JCombobox as what it is I mean the output is always like this [Ljava.lang.String;@1d1f40c some kind of a encrypted word, so I believe that custom renderer is not needed. – Zyrax Sep 14 '12 at 03:41
  • @Jerome: You aren't putting Strings into your combo box but rather each item in your combo box is an array of String. I can say this because we're seeing the class name and hashcode for an array of String -- there's nothing "encrypted" or magical about this. If you want to add Strings to the model so that each item holds a String, then you'll need to change your code so that it takes care to add a single String in each iteration of the for loop, not an array of String. If you want to add arrays of Strings and want the arrays displayed somehow, then yes, you'll need a renderer. – Hovercraft Full Of Eels Sep 14 '12 at 03:47
  • @Jerome: There's more on this [here](http://stackoverflow.com/a/3424872/230513), cited in a comment to the example. – trashgod Sep 14 '12 at 04:04
  • Thanks it's working now your right its a String of array, so I just make it a simple string and it work, however what if I wanted to store an array to the combobox should I use renderer? anyway the link is awesome I really need this thanks :D – Zyrax Sep 14 '12 at 04:12
  • @Jerome: please clarify what you're trying to do. Are you trying to have *each item in the combo box be an array of String*, or are you trying to *add an array of Strings to the combo box so that each item is a String from the array*? We've already discussed how to do number 2: use a model instead, and for number 1: use a renderer. So where are you stuck? – Hovercraft Full Of Eels Sep 14 '12 at 04:14
  • well what I'm trying to do is to display let's say a name and at the same time it's associate userID so that every time the user choose from the combobox I can easily get the ID and use it to my query. I know its not that complicated but tricky. actually what I want to do is just to get the ID that's all :D – Zyrax Sep 14 '12 at 04:29
  • 1
    @Jerome: then create a class that holds the name and userID and populate the JComboBox's model with objects of this class. Use a generic `DefaultComboBoxModel` for this. Then (again) use a custom renderer that allows the combo box to display the items correctly. This is all spelled out in the combo box tutorial. I urge you to read it soon. – Hovercraft Full Of Eels Sep 14 '12 at 04:34
  • thanks a lot I've been doing this for almost 5 hours, If only I know that the answer is only here in the forum I should have asked earlier. :) – Zyrax Sep 14 '12 at 04:40