aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes JComboBox with enum Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JComboBox with enum" Watch "JComboBox with enum" New topic
Author

JComboBox with enum

shraddha sawant
Greenhorn

Joined: Sep 06, 2011
Posts: 12

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.


shraddhasav
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1085
    
  10

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


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
shraddha sawant
Greenhorn

Joined: Sep 06, 2011
Posts: 12

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.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
shraddha sawant
Greenhorn

Joined: Sep 06, 2011
Posts: 12

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

Joined: May 03, 2008
Posts: 4642
    
    5

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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18675
    
    8

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

Joined: Sep 06, 2011
Posts: 12

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.



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

We could help you understand the exceptions if you would post them here, along with the code that's throwing them.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
shraddha sawant
Greenhorn

Joined: Sep 06, 2011
Posts: 12




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
Bartender

Joined: Oct 14, 2005
Posts: 18675
    
    8

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

Joined: Sep 06, 2011
Posts: 12

yes sir. thats what i want to ask.
 
 
subject: JComboBox with enum