How to Populate JComboBox from ArrayListCreate ArrayList from arrayHow do I call one constructor from another in Java?How do I read / convert an InputStream into a String in Java?When to use LinkedList over ArrayList in Java?How do I generate random integers within a specific range in Java?How to get an enum value from a string value in Java?Initialization of an ArrayList in one lineConverting 'ArrayList<String> to 'String[]' in JavaConvert ArrayList<String> to String[] arrayHow do I convert a String to an int in Java?

Have I found a major security issue with login

Head-internal relative clauses

Is it wise to pay off mortgage with 401k?

Why could the Lunar Ascent Engine be used only once?

Can I have a delimited macro with a literal # in the parameter text?

Does a windmilling propeller create more drag than a stopped propeller in an engine out scenario?

How does the "reverse syntax" in Middle English work?

How to fix "webpack Dev Server Invalid Options" in Vuejs

pwaS eht tirsf dna tasl setterl fo hace dorw

Can't think of a good word or term to describe not feeling or thinking

Why does snapping your fingers activate the Infinity Gauntlet?

Richard's Favourite TV Programme

How could the B-29 bomber back up under its own power?

Isn't Kirchhoff's junction law a violation of conservation of charge?

In how many ways can we partition a set into smaller subsets so the sum of the numbers in each subset is equal?

What does it mean for a program to be 32 or 64 bit?

Germany rejected my entry to Schengen countries

Is it a good idea to teach algorithm courses using pseudocode instead of a real programming language?

Managing heat dissipation in a magic wand

Why did Nick Fury not hesitate in blowing up the plane he thought was carrying a nuke?

Find the 3D region containing the origin bounded by given planes

How to plot a surface from a system of equations?

How to convince boss to spend notice period on documentation instead of new projects

Bash - Execute two commands and get exit status 1 if first fails



How to Populate JComboBox from ArrayList


Create ArrayList from arrayHow do I call one constructor from another in Java?How do I read / convert an InputStream into a String in Java?When to use LinkedList over ArrayList in Java?How do I generate random integers within a specific range in Java?How to get an enum value from a string value in Java?Initialization of an ArrayList in one lineConverting 'ArrayList<String> to 'String[]' in JavaConvert ArrayList<String> to String[] arrayHow do I convert a String to an int in Java?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am trying to fetch some values from the MySQL database to JComboBox using ArrayList and DefaultComboBoxModel to avoid fetching duplicate values. Because JComboBox keeps filling the same values again and again to the JComboBox when I called the method to fetch values.



Currently, when the following code runs combo box is showing values like these,




sometext@24084,
sometext@716a1




Here is my code,



public static void loadAll(JComboBox comboBox) ClassNotFoundException ex) 

Logger.getLogger(AllComboBoxMethod.class.getName()).log(Level.SEVERE, null, ex);




And Fetch Method,



int itemCount = combo.getItemCount(); 

for(int i = 1; i < itemCount; i++)
combo.removeItemAt(0);


String sql = "SELECT * FROM combo_type";
dbConnection = DbConnection.getInstance();
con = dbConnection.getConnection();
stm = con.createStatement();
rst = stm.executeQuery(sql);

ArrayList<Combo> comboList = new ArrayList();
while(rst.next())
comboList.add(new Combo(rst.getString(2)));

return comboList;


So what I'm doing wrong? Is this the only way to avoid fetching duplicate values? I tried comboBox.removeAllItems() earlier (before adding this code) and then fetch the values. But it gives me NullPointerException some times. So how can I fix this?










share|improve this question
























  • Could you clarify which problem you need to solve? Is it only for the String representation?

    – LppEdd
    Mar 23 at 18:53











  • @LppEdd Mainly, I need to avoid fetching duplicate values when calling the JComboBox fetch method again and again.

    – Hasitha M Jayawardana
    Mar 23 at 18:55











  • Where is this "fetch" method?

    – LppEdd
    Mar 23 at 18:56











  • @LppEdd I have updated my question.

    – Hasitha M Jayawardana
    Mar 23 at 19:01

















0















I am trying to fetch some values from the MySQL database to JComboBox using ArrayList and DefaultComboBoxModel to avoid fetching duplicate values. Because JComboBox keeps filling the same values again and again to the JComboBox when I called the method to fetch values.



Currently, when the following code runs combo box is showing values like these,




sometext@24084,
sometext@716a1




Here is my code,



public static void loadAll(JComboBox comboBox) ClassNotFoundException ex) 

Logger.getLogger(AllComboBoxMethod.class.getName()).log(Level.SEVERE, null, ex);




And Fetch Method,



int itemCount = combo.getItemCount(); 

for(int i = 1; i < itemCount; i++)
combo.removeItemAt(0);


String sql = "SELECT * FROM combo_type";
dbConnection = DbConnection.getInstance();
con = dbConnection.getConnection();
stm = con.createStatement();
rst = stm.executeQuery(sql);

ArrayList<Combo> comboList = new ArrayList();
while(rst.next())
comboList.add(new Combo(rst.getString(2)));

return comboList;


So what I'm doing wrong? Is this the only way to avoid fetching duplicate values? I tried comboBox.removeAllItems() earlier (before adding this code) and then fetch the values. But it gives me NullPointerException some times. So how can I fix this?










share|improve this question
























  • Could you clarify which problem you need to solve? Is it only for the String representation?

    – LppEdd
    Mar 23 at 18:53











  • @LppEdd Mainly, I need to avoid fetching duplicate values when calling the JComboBox fetch method again and again.

    – Hasitha M Jayawardana
    Mar 23 at 18:55











  • Where is this "fetch" method?

    – LppEdd
    Mar 23 at 18:56











  • @LppEdd I have updated my question.

    – Hasitha M Jayawardana
    Mar 23 at 19:01













0












0








0








I am trying to fetch some values from the MySQL database to JComboBox using ArrayList and DefaultComboBoxModel to avoid fetching duplicate values. Because JComboBox keeps filling the same values again and again to the JComboBox when I called the method to fetch values.



Currently, when the following code runs combo box is showing values like these,




sometext@24084,
sometext@716a1




Here is my code,



public static void loadAll(JComboBox comboBox) ClassNotFoundException ex) 

Logger.getLogger(AllComboBoxMethod.class.getName()).log(Level.SEVERE, null, ex);




And Fetch Method,



int itemCount = combo.getItemCount(); 

for(int i = 1; i < itemCount; i++)
combo.removeItemAt(0);


String sql = "SELECT * FROM combo_type";
dbConnection = DbConnection.getInstance();
con = dbConnection.getConnection();
stm = con.createStatement();
rst = stm.executeQuery(sql);

ArrayList<Combo> comboList = new ArrayList();
while(rst.next())
comboList.add(new Combo(rst.getString(2)));

return comboList;


So what I'm doing wrong? Is this the only way to avoid fetching duplicate values? I tried comboBox.removeAllItems() earlier (before adding this code) and then fetch the values. But it gives me NullPointerException some times. So how can I fix this?










share|improve this question
















I am trying to fetch some values from the MySQL database to JComboBox using ArrayList and DefaultComboBoxModel to avoid fetching duplicate values. Because JComboBox keeps filling the same values again and again to the JComboBox when I called the method to fetch values.



Currently, when the following code runs combo box is showing values like these,




sometext@24084,
sometext@716a1




Here is my code,



public static void loadAll(JComboBox comboBox) ClassNotFoundException ex) 

Logger.getLogger(AllComboBoxMethod.class.getName()).log(Level.SEVERE, null, ex);




And Fetch Method,



int itemCount = combo.getItemCount(); 

for(int i = 1; i < itemCount; i++)
combo.removeItemAt(0);


String sql = "SELECT * FROM combo_type";
dbConnection = DbConnection.getInstance();
con = dbConnection.getConnection();
stm = con.createStatement();
rst = stm.executeQuery(sql);

ArrayList<Combo> comboList = new ArrayList();
while(rst.next())
comboList.add(new Combo(rst.getString(2)));

return comboList;


So what I'm doing wrong? Is this the only way to avoid fetching duplicate values? I tried comboBox.removeAllItems() earlier (before adding this code) and then fetch the values. But it gives me NullPointerException some times. So how can I fix this?







java arraylist jcombobox






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 8:05









piet.t

10.2k73246




10.2k73246










asked Mar 23 at 18:43









Hasitha M JayawardanaHasitha M Jayawardana

1,1801721




1,1801721












  • Could you clarify which problem you need to solve? Is it only for the String representation?

    – LppEdd
    Mar 23 at 18:53











  • @LppEdd Mainly, I need to avoid fetching duplicate values when calling the JComboBox fetch method again and again.

    – Hasitha M Jayawardana
    Mar 23 at 18:55











  • Where is this "fetch" method?

    – LppEdd
    Mar 23 at 18:56











  • @LppEdd I have updated my question.

    – Hasitha M Jayawardana
    Mar 23 at 19:01

















  • Could you clarify which problem you need to solve? Is it only for the String representation?

    – LppEdd
    Mar 23 at 18:53











  • @LppEdd Mainly, I need to avoid fetching duplicate values when calling the JComboBox fetch method again and again.

    – Hasitha M Jayawardana
    Mar 23 at 18:55











  • Where is this "fetch" method?

    – LppEdd
    Mar 23 at 18:56











  • @LppEdd I have updated my question.

    – Hasitha M Jayawardana
    Mar 23 at 19:01
















Could you clarify which problem you need to solve? Is it only for the String representation?

– LppEdd
Mar 23 at 18:53





Could you clarify which problem you need to solve? Is it only for the String representation?

– LppEdd
Mar 23 at 18:53













@LppEdd Mainly, I need to avoid fetching duplicate values when calling the JComboBox fetch method again and again.

– Hasitha M Jayawardana
Mar 23 at 18:55





@LppEdd Mainly, I need to avoid fetching duplicate values when calling the JComboBox fetch method again and again.

– Hasitha M Jayawardana
Mar 23 at 18:55













Where is this "fetch" method?

– LppEdd
Mar 23 at 18:56





Where is this "fetch" method?

– LppEdd
Mar 23 at 18:56













@LppEdd I have updated my question.

– Hasitha M Jayawardana
Mar 23 at 19:01





@LppEdd I have updated my question.

– Hasitha M Jayawardana
Mar 23 at 19:01












1 Answer
1






active

oldest

votes


















1














This



sometext@716a1


is the unique identifier/memory address of a concrete class instance.

You're seeing that because you haven't correctly overridden the



public String toString();


method, for the Combo class.




Also, DefaultComboBoxModel is a generic enabled class



DefaultComboBoxModel<E>


so why are you using a raw instance?

You should be dealing with



DefaultComboBoxModel<Combo>


instead. Which means you could do



for (final Combo combo : comboList) 
dtm.addElement(combo);




Try with



public static void loadAll(final JComboBox comboBox) 
try
final List<Combo> comboList = ComboBoxController.getComboType();
final DefaultComboBoxModel dtm = (DefaultComboBoxModel) comboBox.getModel();

dtm.removeAllItems();

for (final Combo combo : comboList)
dtm.addElement(combo.getComboType());

catch (final SQLException



int itemCount = combo.getItemCount();

for (int i = itemCount - 1; i >= 0; i++)
combo.removeItemAt(0);






share|improve this answer

























  • isn't this a duplicate of a gabillion similar questions?

    – Hovercraft Full Of Eels
    Mar 23 at 18:51











  • @HovercraftFullOfEels mmmh I'm trying to understand if he's trying to solve only a "toString" problem or if there is more.

    – LppEdd
    Mar 23 at 18:52











  • I tried overriding toString method. It shows me Java.Lang.Object;@546 like this.

    – Hasitha M Jayawardana
    Mar 23 at 18:57











  • @HasithaMJayawardana Is it showing it like [Ljava.lang.Object;@60438a68 ? With an [L in front?

    – LppEdd
    Mar 23 at 19:01











  • @LppEdd Yes L is there

    – Hasitha M Jayawardana
    Mar 23 at 19:01











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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55317185%2fhow-to-populate-jcombobox-from-arraylist%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














This



sometext@716a1


is the unique identifier/memory address of a concrete class instance.

You're seeing that because you haven't correctly overridden the



public String toString();


method, for the Combo class.




Also, DefaultComboBoxModel is a generic enabled class



DefaultComboBoxModel<E>


so why are you using a raw instance?

You should be dealing with



DefaultComboBoxModel<Combo>


instead. Which means you could do



for (final Combo combo : comboList) 
dtm.addElement(combo);




Try with



public static void loadAll(final JComboBox comboBox) 
try
final List<Combo> comboList = ComboBoxController.getComboType();
final DefaultComboBoxModel dtm = (DefaultComboBoxModel) comboBox.getModel();

dtm.removeAllItems();

for (final Combo combo : comboList)
dtm.addElement(combo.getComboType());

catch (final SQLException



int itemCount = combo.getItemCount();

for (int i = itemCount - 1; i >= 0; i++)
combo.removeItemAt(0);






share|improve this answer

























  • isn't this a duplicate of a gabillion similar questions?

    – Hovercraft Full Of Eels
    Mar 23 at 18:51











  • @HovercraftFullOfEels mmmh I'm trying to understand if he's trying to solve only a "toString" problem or if there is more.

    – LppEdd
    Mar 23 at 18:52











  • I tried overriding toString method. It shows me Java.Lang.Object;@546 like this.

    – Hasitha M Jayawardana
    Mar 23 at 18:57











  • @HasithaMJayawardana Is it showing it like [Ljava.lang.Object;@60438a68 ? With an [L in front?

    – LppEdd
    Mar 23 at 19:01











  • @LppEdd Yes L is there

    – Hasitha M Jayawardana
    Mar 23 at 19:01















1














This



sometext@716a1


is the unique identifier/memory address of a concrete class instance.

You're seeing that because you haven't correctly overridden the



public String toString();


method, for the Combo class.




Also, DefaultComboBoxModel is a generic enabled class



DefaultComboBoxModel<E>


so why are you using a raw instance?

You should be dealing with



DefaultComboBoxModel<Combo>


instead. Which means you could do



for (final Combo combo : comboList) 
dtm.addElement(combo);




Try with



public static void loadAll(final JComboBox comboBox) 
try
final List<Combo> comboList = ComboBoxController.getComboType();
final DefaultComboBoxModel dtm = (DefaultComboBoxModel) comboBox.getModel();

dtm.removeAllItems();

for (final Combo combo : comboList)
dtm.addElement(combo.getComboType());

catch (final SQLException



int itemCount = combo.getItemCount();

for (int i = itemCount - 1; i >= 0; i++)
combo.removeItemAt(0);






share|improve this answer

























  • isn't this a duplicate of a gabillion similar questions?

    – Hovercraft Full Of Eels
    Mar 23 at 18:51











  • @HovercraftFullOfEels mmmh I'm trying to understand if he's trying to solve only a "toString" problem or if there is more.

    – LppEdd
    Mar 23 at 18:52











  • I tried overriding toString method. It shows me Java.Lang.Object;@546 like this.

    – Hasitha M Jayawardana
    Mar 23 at 18:57











  • @HasithaMJayawardana Is it showing it like [Ljava.lang.Object;@60438a68 ? With an [L in front?

    – LppEdd
    Mar 23 at 19:01











  • @LppEdd Yes L is there

    – Hasitha M Jayawardana
    Mar 23 at 19:01













1












1








1







This



sometext@716a1


is the unique identifier/memory address of a concrete class instance.

You're seeing that because you haven't correctly overridden the



public String toString();


method, for the Combo class.




Also, DefaultComboBoxModel is a generic enabled class



DefaultComboBoxModel<E>


so why are you using a raw instance?

You should be dealing with



DefaultComboBoxModel<Combo>


instead. Which means you could do



for (final Combo combo : comboList) 
dtm.addElement(combo);




Try with



public static void loadAll(final JComboBox comboBox) 
try
final List<Combo> comboList = ComboBoxController.getComboType();
final DefaultComboBoxModel dtm = (DefaultComboBoxModel) comboBox.getModel();

dtm.removeAllItems();

for (final Combo combo : comboList)
dtm.addElement(combo.getComboType());

catch (final SQLException



int itemCount = combo.getItemCount();

for (int i = itemCount - 1; i >= 0; i++)
combo.removeItemAt(0);






share|improve this answer















This



sometext@716a1


is the unique identifier/memory address of a concrete class instance.

You're seeing that because you haven't correctly overridden the



public String toString();


method, for the Combo class.




Also, DefaultComboBoxModel is a generic enabled class



DefaultComboBoxModel<E>


so why are you using a raw instance?

You should be dealing with



DefaultComboBoxModel<Combo>


instead. Which means you could do



for (final Combo combo : comboList) 
dtm.addElement(combo);




Try with



public static void loadAll(final JComboBox comboBox) 
try
final List<Combo> comboList = ComboBoxController.getComboType();
final DefaultComboBoxModel dtm = (DefaultComboBoxModel) comboBox.getModel();

dtm.removeAllItems();

for (final Combo combo : comboList)
dtm.addElement(combo.getComboType());

catch (final SQLException



int itemCount = combo.getItemCount();

for (int i = itemCount - 1; i >= 0; i++)
combo.removeItemAt(0);







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 23 at 19:25

























answered Mar 23 at 18:46









LppEddLppEdd

10.2k31849




10.2k31849












  • isn't this a duplicate of a gabillion similar questions?

    – Hovercraft Full Of Eels
    Mar 23 at 18:51











  • @HovercraftFullOfEels mmmh I'm trying to understand if he's trying to solve only a "toString" problem or if there is more.

    – LppEdd
    Mar 23 at 18:52











  • I tried overriding toString method. It shows me Java.Lang.Object;@546 like this.

    – Hasitha M Jayawardana
    Mar 23 at 18:57











  • @HasithaMJayawardana Is it showing it like [Ljava.lang.Object;@60438a68 ? With an [L in front?

    – LppEdd
    Mar 23 at 19:01











  • @LppEdd Yes L is there

    – Hasitha M Jayawardana
    Mar 23 at 19:01

















  • isn't this a duplicate of a gabillion similar questions?

    – Hovercraft Full Of Eels
    Mar 23 at 18:51











  • @HovercraftFullOfEels mmmh I'm trying to understand if he's trying to solve only a "toString" problem or if there is more.

    – LppEdd
    Mar 23 at 18:52











  • I tried overriding toString method. It shows me Java.Lang.Object;@546 like this.

    – Hasitha M Jayawardana
    Mar 23 at 18:57











  • @HasithaMJayawardana Is it showing it like [Ljava.lang.Object;@60438a68 ? With an [L in front?

    – LppEdd
    Mar 23 at 19:01











  • @LppEdd Yes L is there

    – Hasitha M Jayawardana
    Mar 23 at 19:01
















isn't this a duplicate of a gabillion similar questions?

– Hovercraft Full Of Eels
Mar 23 at 18:51





isn't this a duplicate of a gabillion similar questions?

– Hovercraft Full Of Eels
Mar 23 at 18:51













@HovercraftFullOfEels mmmh I'm trying to understand if he's trying to solve only a "toString" problem or if there is more.

– LppEdd
Mar 23 at 18:52





@HovercraftFullOfEels mmmh I'm trying to understand if he's trying to solve only a "toString" problem or if there is more.

– LppEdd
Mar 23 at 18:52













I tried overriding toString method. It shows me Java.Lang.Object;@546 like this.

– Hasitha M Jayawardana
Mar 23 at 18:57





I tried overriding toString method. It shows me Java.Lang.Object;@546 like this.

– Hasitha M Jayawardana
Mar 23 at 18:57













@HasithaMJayawardana Is it showing it like [Ljava.lang.Object;@60438a68 ? With an [L in front?

– LppEdd
Mar 23 at 19:01





@HasithaMJayawardana Is it showing it like [Ljava.lang.Object;@60438a68 ? With an [L in front?

– LppEdd
Mar 23 at 19:01













@LppEdd Yes L is there

– Hasitha M Jayawardana
Mar 23 at 19:01





@LppEdd Yes L is there

– Hasitha M Jayawardana
Mar 23 at 19:01



















draft saved

draft discarded
















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55317185%2fhow-to-populate-jcombobox-from-arraylist%23new-answer', 'question_page');

);

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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript