• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JComboBox with enum

 
Greenhorn
Posts: 12
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am facing somewhat similar problem.
my problem is that i am using auto generated code from netbrans.
i have created a Master/detail form.
in my mysql database i have used some fields as enum like gender( ENUM('Male','Female' ), maritalStatus.
how can i populated JCombobox with these enumerated values like 'Male','Female' automatically.

i tried populating jcombobox using expression select distinct d.gender from pInfo
it work only when there is some data in table. i understand, this wont solve my problem because every time table wont be containing all possible values from enum.

please help me.
thanking you in advance.
 
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

the answers to your questions are available in this post itself. You do have an enum 'Geneder' generated, right? then just call Gender.values() as has been done in the post earlier (Brian Cole's answer).
 
shraddha sawant
Greenhorn
Posts: 12
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for immediate reply.

I think i should specify my problem more deeply.

my table contains one column d1 datatype of which is ENUM('Y','N')

and what i want is that when i click on New button (automatically generated using Master/Detail Form feature of Netbeans), Combobox should be filled with Y and N automatically.

If i create an enum{Y,N} in java file, where should I bind this enum to Combobox.

Combobox's element property is bounded with list, and selectedElement property is bounded with jTable.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shraddha sawant wrote:... automatically generated ...



Don't use anything that automatically generates code for you until you have the knowledge and experience to read that code and know what it does.
 
shraddha sawant
Greenhorn
Posts: 12
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I also don't want to do that.
But time is too short, i have to complete project within 24 days and prepare for campus also.
So only this time, i am going to use auto generated code.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But time is too short, i have to complete ...


And you really think sorting out the quirks of a code generator that produces code incomprehensible to you (and to many of us here) is going to speed your development process? Not likely.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shraddha sawant wrote:and what i want is that when i click on New button (automatically generated using Master/Detail Form feature of Netbeans), Combobox should be filled with Y and N automatically.

If i create an enum{Y,N} in java file, where should I bind this enum to Combobox.



If that's what you want, then you would do that in the actionPerformed method of an action listener which has been added to that New button.

Edit: However you said you had something else which sort of worked, so it appears you already know where you should add that code. So I don't quite understand why you asked that question.
 
shraddha sawant
Greenhorn
Posts: 12
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Slight change in the autogenerated code gives rise to exceptions, which i am not able to understand.
This was the code for new button




Dummy is table name as well as name of the entity class generated from database.
cbd1 is combo box d1.
d1 is field name in mysql table which has enum('Y','N') data type.

Anyways thank you for replying, i am starting my project again, with plain jdbc.



 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We could help you understand the exceptions if you would post them here, along with the code that's throwing them.
 
shraddha sawant
Greenhorn
Posts: 12
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



And the Exception is

Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'd1' cannot be null
Error Code: 1048
Call: INSERT INTO showroom.dummy (Numb, d1) VALUES (?, ?)
bind => [99, null]
Query: InsertObjectQuery(jj.Dummy[ numb=99 ])

table : Dummy
numb int(3)
d1 enum('Y','N')

netbeans generates text field for each of the field in table. Without modification also it does not work as it takes d1 field as string.
I have deleted that text field and placed combobox.
I have bound combo box elements to a list1 (created by using query "SELECT DISTINCT d.d1 FROM Dummy d")
it is showing both {Y,N} as elements of combo box. (Solution to my original problem). But i understand that is not real solution because it won't work when table is empty.

and if i add that combobox.setModel() code in java.lang.newButtonActionperformed it creates error.

i understand that it should insert combobox.selectedIndex in table, instead of combobox.selectedModel. But where should i write this code.

I know this post is too long, but i can not tell you my problem without it. I have attached both file. Please have a look on that also.

thanking you in advanced.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, that's totally bizarre. Here we thought you were asking a simple question about how to add entries to a JComboBox. But now it appears your question is actually something like "If I have an Enum describing a database column, then how do I find all the possible values of that Enum?" Is that correct?
 
shraddha sawant
Greenhorn
Posts: 12
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes sir. thats what i want to ask.
reply
    Bookmark Topic Watch Topic
  • New Topic