wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes jcombobox is not auto updating with database. 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 is not auto updating with database." Watch "jcombobox is not auto updating with database." New topic
Author

jcombobox is not auto updating with database.

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
hello all,
i have a problem, actually i have posted this problem earlier, and michael dunn helps me solve that problem.
Below is the problem.
http://www.coderanch.com/t/579226/GUI/java/auto-update-jcombobox

but now i m getting the same problem in auto updating jcomboboxes.
i have two code one is updating jcombobox with arraylist, and working properly.
one is with db that is not working.
code for both are same, instead of arraylist i am just using database.
i also have done debugging by putting bunch system.out.println to check the flow, both giving the same sequence of statements, i mean flow of both are same.
i don't know what wrong is going there, can anyone please help me in that.?
below is the code with db which is not working.


with arraylist, it's working.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
you have this
JComboBox studentNames = new JComboBox();

at both line 16 and 54. remove the one at line 16.
doesn't look like it will make any difference, but, just in case.

the problem could also be a threading issue - the db stuff should be
in a separate thread (or SwingWorker).
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
hi michael, thanks for replying.
can i do something like this??
i will repaint the my components after changes being made??
panel.repaint();
panel.revalidate();
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
reverse the order

panel.revalidate();
panel.repaint();
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
actually i tried this, but this also not works:

d1.panel1.revalidate();
d1.panel1.repaint();

d1 is object of Display1 class.
i am revalidating and repainting panel1 because jcombobox is loading in panel1.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
revalidate/repaint will only reconfirm the components,
it will have no effect on the data.

unless someone posts what they can see as the solution,
you should try my earlier suggestion.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
yes i m reading swingworker.
i little confusion.
i need to do something like this?? :-
create a object of swing worker class.
i m calling savestudent() in my actionlistener, this should be done in doInBackground.
i m calling update() in my actionlistener, this should be done in done.
then i will execute SwingWorker.


Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
Actually i m not getting exactly how do i implement it in my code.
i m trying it like this, in my Display class:

am i doing in wrong way?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> am i doing in wrong way?

can't see execute(), but I've just noticed something in trying to see a better way.

when you say it's not working (using db), are you getting multiple entries each time you save/load the names?

if that's the problem, it's because of reading the db names into
ArrayList<String> resultSetList = new ArrayList<String>();

and not clearing the arrayList's contents before reloading the student names
e.g.
your program opens and loads 10 students into 'resultSetList' (which end up displayed in the comboBox)
now you add 1 student, so, added to 'resultSetList' in loadStudents() is 11 more names, so the comboBox will have 21 names
add another and the comboBox will now have 21 + 12 names etc

if the problem is not the above, can you explain how it's not working.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
Thank you for replying michael.
can't see execute(), but I've just noticed something in trying to see a better way.

putted execute in my actionlistener.



if the problem is not the above, can you explain how it's not working.

working in the sense, it's not getting updated automatically.
i am getting the values into combobox properly, but to see last inserted value i have to restart my application.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
is it possible:
i add one refresh button with jcombobox, and client needs to press that button, when he inserted new record, to get updated values in jcombobox.
i know it's not efficient way to do this, but i am not getting how do i solve this problem.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
seems working..
but i m getting blank values.
i mean i am inserting values into text field, but in combo i am getting blank for that value.
i tried by printing text field value in doInBackground and it's printing nothing.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
have you added the loadStudents code to a SwingWorker?

the main problem is the update-loading, after the save
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
have you added the loadStudents code to a SwingWorker?


no i haven't.
i have only put this code in swingworker.


and it saving blank values into database.
values are not even going into database.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
okay..
it's gets works fine.
but i am not able to distribute code into multiple files.
i need to distribute it in this way:
the main frame, have a jmenubar, with 2 menuitems,
-> when i click on register it's opening the registration panel, right now, it has a jtextfield and button.
-> when i click on display it's openinig the display page, which has jcombobox.
i tried to distribute but my jcombobox showing nothing, when i am distributing.
should i post the code.?

Thank You
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> i tried to distribute but my jcombobox showing nothing, when i am distributing.

as the comboBox displays the db data, it is likely to be a db connection problem.

when distributed:
do thay have access to the db
do they have the driver
etc

the db experts are in the JDBC forum, so you might be better off putting up
a post there, about what's required when distributing, and include a link to
this thread (if they want to see the history and/or some code). If they want to
see the current code, post it there.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
no the problem is not the connection and all.
i am quite sure, the problem is somewhere in the distribution.
i mean the flow is breaking somewhere.
and quite confused with passing arguments and all.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
if the connection is fine, create another .java file and remove all the db stuff.
hard code a couple of values for the comboBox to display (like in the ArrayList simulation earlier)
now distribute that and see if the same problem occurs. if so, you can post that code here.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
okay..
i am doing this, will post here if same problem occurs..

Thank you michael.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
i tried..
what i tried to do:
MainMenu class: which has jmenubar.
Display class: which has the code for displaying the jtextbox and jbutton.
Display1 class: which has the code for displaying jcombobox with updated values.
menu bar has menus, open(to open panel which has textbox and button) save(to open panel which has combobox).
display class has cardlayout, as you did earlier, which hiding and showing the textbox and combobox on button click.
but i want that combo on display1 class.
i mean right now, when i am clicking on button, it showing combobox(adding on same panel), but i want in other class.
Let me explain with code, please have look to the code.

MainMenu.java


right now, if i click directly on Save menu, it showing me a blank combo, but when i am click on Open menu, and inserting value in jtextbox and pressing button, it showing me updated combobox.
but i want when i directly click on save it should show me updated combobox.

Display.java


Display1.java


Rest of the classes are same and working properly.
Thank you.

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
if I'm reading this right, you have your cardLayout set up in the wrong class.
it should be with the menu items in MainMenu()

as this is just display, I've stripped everything but the 'visual' stuff
comments/changes should be self-explanatory

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
okay done..
one last small problem.
the last inserted values appearing two times, at first position in the combo box and at last position in the combo box.
i have created ArrayList in the MainMenu class, which i am using when calling update method:


also i am passing this to Display class, by passing argument in display class constructor.
i have also cleared the array list, but nothing happened.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
can only guess this one:

you're clearing resultSetList
adding the new name to resultSetList, as well as adding to the db
loading the db into resultSetList.

that way, the new name would appear first and last.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 1000
    
    2
done.
i was clearing arraylist, after inserting records.
but it should be cleared before.

Thank you michael.
 
GeeCON Prague 2014
 
subject: jcombobox is not auto updating with database.