Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can a Vector be used in a Combo Box Model?  RSS feed

 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand how vectors work I'm currently using one to store my id's from my txt file but how do you put them in a defaultComboBoxModel?



Can anyone give me an example of how a vector is used with a defaultComboBoxModel so I can then use that to populate my JComboBox?
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone give me an example of how a vector is used with a defaultComboBoxModel


Did you read the DefaultComboBoxModel API? Do any constructors/methods accepts a Vector as a parameter?
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah it does, I know it can I'm just not 100% on how to lay it out. I presumed I would need a method for it like so



But I don't know how correct that it :/
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presumed I would need a method for it like so


What do you mean you "need a method". The API provides you with the method. You just use the existing method.

Your code already has a loop, so instead of displaying the value you add the value to the Vector. Then when you finish the loop you create the model using the Vector.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't that the method for it anyway?
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is not a method. That is a constructor. Also reread my previous answer I added more info.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just went to edit it there to correct what I had said. But I could not edit it,I think it was because you had replied.

*Edit /I know I have that constructor but I genuinely don't get what I'd have as my method, perhaps it will come to me but for now I'm totally clueless.
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your current code is creating the model BEFORE the Vector is populated with data. You need to create the model AFTER you have read the data from the file and populated the Vector.

Post a proper SSCCE if you need more help. And we don't have access to your data file so you will need to hardcode the adding of multiple items to the Vector.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If my txt file is roughly 6 lines of code is it okay to just post it? As I don't have much code overall - thanks for SSCCE reminder, I think I may have to post one so I can get a good understanding of this
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The point of creating a SSCCE is to understand a concept. The concept you are trying to understand is how to create a DefaultComboBoxModel using a Vector. The file is irrelevant for creating the Vector. All you need to do is manually add a couple of items to the Vector.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah righto, thanks for the reply,i'll sort that out then. Thank you for helping me with this, I've learnt a great deal on these forums!
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed my code a lot to make the vector hard coded. But it still stands that I need to add the vector to the DefaultComboBoxModel. So I can then use said model in my Display.java class so it can be added to my JComboBox.



Thank you to anyone who can take the time to help with this
 
Campbell Ritchie
Sheriff
Posts: 53773
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think they have an error there in the constructors; they obviously created that class before ArrayList became popular and never overloaded the constructor to take a List as a parameter.
By the way: Intarr is a dreadful name for your List. 1: It sounds like an array. 2: You can't tell from its name it is supposed to do. 3: The capital letters are in the wrong place. intArr not Intarr.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah I forgot to rename that :/ Other than that however - do you know how the vector can be placed into a defaultComboBoxModel?
 
Campbell Ritchie
Sheriff
Posts: 53773
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read about combo boxes in the Java API documentation and in the Java Tutorials?
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I have but I can't find anything that specifically demonstrates how to place a vector into a DefaultComboBoxModel so that can then be placed into the JComboBox in the other class. I posted an SSCCE yesterday after speaking to Rob in the hopes someone could help/give an example as to how you go about doing this.
 
Campbell Ritchie
Sheriff
Posts: 53773
127
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you find this constructor?
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've sorted it thankyou - and yes I did originally use it but I've set it up another way now
 
Rob Camick
Ranch Hand
Posts: 2754
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't find anything that specifically demonstrates how to place a vector into a DefaultComboBoxModel


I don't understand that statement, there is a constructor for the DefaultComboBoxModel that takes a Vector as a parameter. Part of programming is learning to read the API. This constructor is straight forward, you just provide the Vector. What do you find confusing about that? I'm not sure how to help if I don't understand the confusion you are having.

but I've set it up another way now


I also don't understand that statement. Why would you not just use the provided constructor? It sounds like you are doing something strange. Post you new SSCCE that shows what you are doing.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!