How to add items to a JComboBox from a List of Objects
I'm currently trying to create a JComboBox
that shows me the name of different car components. The Component
class is currently with the following variables:
public class Component{
private int id;
private String name;
private double price;
private int quantity;
private String type;
}
Now i have another class that contains all the components that exist, wich have been loaded from my Database:
public class Stand{
private List<Component> allComponents;
public Stand(){
//loads the components from my database into allComponents
this.allComponents = componentDAO.loadComponents();
public List<Componente> getAllComponents(){
return this.allComponents;
}
}
What i'm looking for now is to create a class called SelectComponent
with some JComboBox
that show me only the name of each Component
as an option. How would i do that?
Also, after selecting the desired Component
, i would have another List<Component>
in this same class to keep the selected Components, since i will need to have more than one JComboBox in the same window. How do i add the selected Component
to this List
after it being selected?
If you can set the JComboBox to already show the components for a certain type, for example "Paint", would be even better since i would need help with that too. Please let me know if there's more information i need to provide. I have some difficulty in keeping track on everything that's needed since there's already a lot more done. Just trying to get an idea and apply it to my program.
java swing
add a comment |
I'm currently trying to create a JComboBox
that shows me the name of different car components. The Component
class is currently with the following variables:
public class Component{
private int id;
private String name;
private double price;
private int quantity;
private String type;
}
Now i have another class that contains all the components that exist, wich have been loaded from my Database:
public class Stand{
private List<Component> allComponents;
public Stand(){
//loads the components from my database into allComponents
this.allComponents = componentDAO.loadComponents();
public List<Componente> getAllComponents(){
return this.allComponents;
}
}
What i'm looking for now is to create a class called SelectComponent
with some JComboBox
that show me only the name of each Component
as an option. How would i do that?
Also, after selecting the desired Component
, i would have another List<Component>
in this same class to keep the selected Components, since i will need to have more than one JComboBox in the same window. How do i add the selected Component
to this List
after it being selected?
If you can set the JComboBox to already show the components for a certain type, for example "Paint", would be even better since i would need help with that too. Please let me know if there's more information i need to provide. I have some difficulty in keeping track on everything that's needed since there's already a lot more done. Just trying to get an idea and apply it to my program.
java swing
Don't call your classComponent
. There is already an AWT class by then name. Make you class name more descriptive, maybeCarComponent
to avoid confusion.
– camickr
Jan 2 at 0:03
If you can set the JComboBox to already show the components for a certain type,
- well you just check the "type" of the CarComponent before you add the object to the combo box.
– camickr
Jan 2 at 1:55
add a comment |
I'm currently trying to create a JComboBox
that shows me the name of different car components. The Component
class is currently with the following variables:
public class Component{
private int id;
private String name;
private double price;
private int quantity;
private String type;
}
Now i have another class that contains all the components that exist, wich have been loaded from my Database:
public class Stand{
private List<Component> allComponents;
public Stand(){
//loads the components from my database into allComponents
this.allComponents = componentDAO.loadComponents();
public List<Componente> getAllComponents(){
return this.allComponents;
}
}
What i'm looking for now is to create a class called SelectComponent
with some JComboBox
that show me only the name of each Component
as an option. How would i do that?
Also, after selecting the desired Component
, i would have another List<Component>
in this same class to keep the selected Components, since i will need to have more than one JComboBox in the same window. How do i add the selected Component
to this List
after it being selected?
If you can set the JComboBox to already show the components for a certain type, for example "Paint", would be even better since i would need help with that too. Please let me know if there's more information i need to provide. I have some difficulty in keeping track on everything that's needed since there's already a lot more done. Just trying to get an idea and apply it to my program.
java swing
I'm currently trying to create a JComboBox
that shows me the name of different car components. The Component
class is currently with the following variables:
public class Component{
private int id;
private String name;
private double price;
private int quantity;
private String type;
}
Now i have another class that contains all the components that exist, wich have been loaded from my Database:
public class Stand{
private List<Component> allComponents;
public Stand(){
//loads the components from my database into allComponents
this.allComponents = componentDAO.loadComponents();
public List<Componente> getAllComponents(){
return this.allComponents;
}
}
What i'm looking for now is to create a class called SelectComponent
with some JComboBox
that show me only the name of each Component
as an option. How would i do that?
Also, after selecting the desired Component
, i would have another List<Component>
in this same class to keep the selected Components, since i will need to have more than one JComboBox in the same window. How do i add the selected Component
to this List
after it being selected?
If you can set the JComboBox to already show the components for a certain type, for example "Paint", would be even better since i would need help with that too. Please let me know if there's more information i need to provide. I have some difficulty in keeping track on everything that's needed since there's already a lot more done. Just trying to get an idea and apply it to my program.
java swing
java swing
asked Jan 1 at 23:56
João AmorimJoão Amorim
275
275
Don't call your classComponent
. There is already an AWT class by then name. Make you class name more descriptive, maybeCarComponent
to avoid confusion.
– camickr
Jan 2 at 0:03
If you can set the JComboBox to already show the components for a certain type,
- well you just check the "type" of the CarComponent before you add the object to the combo box.
– camickr
Jan 2 at 1:55
add a comment |
Don't call your classComponent
. There is already an AWT class by then name. Make you class name more descriptive, maybeCarComponent
to avoid confusion.
– camickr
Jan 2 at 0:03
If you can set the JComboBox to already show the components for a certain type,
- well you just check the "type" of the CarComponent before you add the object to the combo box.
– camickr
Jan 2 at 1:55
Don't call your class
Component
. There is already an AWT class by then name. Make you class name more descriptive, maybe CarComponent
to avoid confusion.– camickr
Jan 2 at 0:03
Don't call your class
Component
. There is already an AWT class by then name. Make you class name more descriptive, maybe CarComponent
to avoid confusion.– camickr
Jan 2 at 0:03
If you can set the JComboBox to already show the components for a certain type,
- well you just check the "type" of the CarComponent before you add the object to the combo box.– camickr
Jan 2 at 1:55
If you can set the JComboBox to already show the components for a certain type,
- well you just check the "type" of the CarComponent before you add the object to the combo box.– camickr
Jan 2 at 1:55
add a comment |
2 Answers
2
active
oldest
votes
show me only the name of each Component as an option.
Create custom renderer to display the "name" property.
The code for a basic renderer would be something like:
class CarComponentRenderer extends BasicComboBoxRenderer
{
public Component getListCellRendererComponent(
JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value instanceof CarComponent)
{
CarComponent carComponent = (CarComponent)value;
setText( carComponent.getName() );
}
return this;
}
}
You then set the renderer on the combo box using:
comboBox.setRenderer( new CarComponentRenderer() );
Check out Combo Box With Custom Renderer for more information and a more complete solution.
Could you also help me with the creating of the JComboBox and further use of the custom renderer?
– João Amorim
Jan 2 at 1:04
@JoãoAmorim, see edit showing how to use the renderer.
– camickr
Jan 2 at 1:54
@JoãoAmorim, don't forget to "accept" the answer so people know the problem has been solved.
– camickr
Jan 3 at 21:39
add a comment |
If you override toString()
method (like below code) in your Component
class, you can show Component's name in the combo box even without having to write a custom renderer.
@Override
public String toString()
{
return this.name;
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999868%2fhow-to-add-items-to-a-jcombobox-from-a-list-of-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
show me only the name of each Component as an option.
Create custom renderer to display the "name" property.
The code for a basic renderer would be something like:
class CarComponentRenderer extends BasicComboBoxRenderer
{
public Component getListCellRendererComponent(
JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value instanceof CarComponent)
{
CarComponent carComponent = (CarComponent)value;
setText( carComponent.getName() );
}
return this;
}
}
You then set the renderer on the combo box using:
comboBox.setRenderer( new CarComponentRenderer() );
Check out Combo Box With Custom Renderer for more information and a more complete solution.
Could you also help me with the creating of the JComboBox and further use of the custom renderer?
– João Amorim
Jan 2 at 1:04
@JoãoAmorim, see edit showing how to use the renderer.
– camickr
Jan 2 at 1:54
@JoãoAmorim, don't forget to "accept" the answer so people know the problem has been solved.
– camickr
Jan 3 at 21:39
add a comment |
show me only the name of each Component as an option.
Create custom renderer to display the "name" property.
The code for a basic renderer would be something like:
class CarComponentRenderer extends BasicComboBoxRenderer
{
public Component getListCellRendererComponent(
JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value instanceof CarComponent)
{
CarComponent carComponent = (CarComponent)value;
setText( carComponent.getName() );
}
return this;
}
}
You then set the renderer on the combo box using:
comboBox.setRenderer( new CarComponentRenderer() );
Check out Combo Box With Custom Renderer for more information and a more complete solution.
Could you also help me with the creating of the JComboBox and further use of the custom renderer?
– João Amorim
Jan 2 at 1:04
@JoãoAmorim, see edit showing how to use the renderer.
– camickr
Jan 2 at 1:54
@JoãoAmorim, don't forget to "accept" the answer so people know the problem has been solved.
– camickr
Jan 3 at 21:39
add a comment |
show me only the name of each Component as an option.
Create custom renderer to display the "name" property.
The code for a basic renderer would be something like:
class CarComponentRenderer extends BasicComboBoxRenderer
{
public Component getListCellRendererComponent(
JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value instanceof CarComponent)
{
CarComponent carComponent = (CarComponent)value;
setText( carComponent.getName() );
}
return this;
}
}
You then set the renderer on the combo box using:
comboBox.setRenderer( new CarComponentRenderer() );
Check out Combo Box With Custom Renderer for more information and a more complete solution.
show me only the name of each Component as an option.
Create custom renderer to display the "name" property.
The code for a basic renderer would be something like:
class CarComponentRenderer extends BasicComboBoxRenderer
{
public Component getListCellRendererComponent(
JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (value instanceof CarComponent)
{
CarComponent carComponent = (CarComponent)value;
setText( carComponent.getName() );
}
return this;
}
}
You then set the renderer on the combo box using:
comboBox.setRenderer( new CarComponentRenderer() );
Check out Combo Box With Custom Renderer for more information and a more complete solution.
edited Jan 2 at 1:53
answered Jan 2 at 0:06
camickrcamickr
276k16127239
276k16127239
Could you also help me with the creating of the JComboBox and further use of the custom renderer?
– João Amorim
Jan 2 at 1:04
@JoãoAmorim, see edit showing how to use the renderer.
– camickr
Jan 2 at 1:54
@JoãoAmorim, don't forget to "accept" the answer so people know the problem has been solved.
– camickr
Jan 3 at 21:39
add a comment |
Could you also help me with the creating of the JComboBox and further use of the custom renderer?
– João Amorim
Jan 2 at 1:04
@JoãoAmorim, see edit showing how to use the renderer.
– camickr
Jan 2 at 1:54
@JoãoAmorim, don't forget to "accept" the answer so people know the problem has been solved.
– camickr
Jan 3 at 21:39
Could you also help me with the creating of the JComboBox and further use of the custom renderer?
– João Amorim
Jan 2 at 1:04
Could you also help me with the creating of the JComboBox and further use of the custom renderer?
– João Amorim
Jan 2 at 1:04
@JoãoAmorim, see edit showing how to use the renderer.
– camickr
Jan 2 at 1:54
@JoãoAmorim, see edit showing how to use the renderer.
– camickr
Jan 2 at 1:54
@JoãoAmorim, don't forget to "accept" the answer so people know the problem has been solved.
– camickr
Jan 3 at 21:39
@JoãoAmorim, don't forget to "accept" the answer so people know the problem has been solved.
– camickr
Jan 3 at 21:39
add a comment |
If you override toString()
method (like below code) in your Component
class, you can show Component's name in the combo box even without having to write a custom renderer.
@Override
public String toString()
{
return this.name;
}
add a comment |
If you override toString()
method (like below code) in your Component
class, you can show Component's name in the combo box even without having to write a custom renderer.
@Override
public String toString()
{
return this.name;
}
add a comment |
If you override toString()
method (like below code) in your Component
class, you can show Component's name in the combo box even without having to write a custom renderer.
@Override
public String toString()
{
return this.name;
}
If you override toString()
method (like below code) in your Component
class, you can show Component's name in the combo box even without having to write a custom renderer.
@Override
public String toString()
{
return this.name;
}
answered Jan 2 at 6:33


Prasad KarunagodaPrasad Karunagoda
1,7031814
1,7031814
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999868%2fhow-to-add-items-to-a-jcombobox-from-a-list-of-objects%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Don't call your class
Component
. There is already an AWT class by then name. Make you class name more descriptive, maybeCarComponent
to avoid confusion.– camickr
Jan 2 at 0:03
If you can set the JComboBox to already show the components for a certain type,
- well you just check the "type" of the CarComponent before you add the object to the combo box.– camickr
Jan 2 at 1:55