• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

how to auto update jcombobox?

 
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have a application, which has one registration panel, and one search panel..
i have a menu and on click of that menu i m changing the jpanel (registration or search).
on the search page i have one combo which displaying all the names of students who are registered or exist in the database.
but when i register a student and than click on search menu, it wiill not update the combo box, i mean the recently registered student is not appear in the list until i restart my application.
what i need to do to update the combo box automatically??

 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there are a few ways this could be done, this is one way:

create a method for when the program opens loadStudents()
in this you retrieve the student names
you load those names into a DefaultComboBoxModel()
you set the comboBox's model to the new DefaultComboBoxModel

you will also have a method to save the new student to the database saveStudent()

so, at the end of them registration process method, you call those methods

unless you have a huge quantity in the db, the above should work fine
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for reply michael..
after getting all the values, i m sending them with insert method for insert into db, like this in my registration page,:


in my search page i m fetching values something like this:


and here is my method which fetches names from database.


so can i use this method in my registration page also, if i use my problem is still remains same.
also can i use JComboBox instead of DefaultComboBoxModel, as i m doing..
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
instead of

use the 'model'
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not working..
i am doing something like this:
In my registration page:


in my SearchByregistration page:

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> not working..

please read the text from this link --> It-Doesnt-Work-Is-Useless

easiest way is to show you a simple demo (using arraylist to simulate database)
enter some text into the textfield, click the button, open the comboBox


 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hii, i read this..


please read the text from this link --> It-Doesnt-Work-Is-Useless


thank you for the demo..it's working, i am able to updating values from database, but when i am distributing the code i m getting some problems, it's going to be an infinite loop, can you please help me in this also.
here is what i am doing.
i m distributing this into three classes.
1.> which has methods to fetch from database and insert into database.
2.>which has my registration page.
3.>which has my display page.

here is how i am distributing the code:

CustomerLayout.java(Registration page)


Db.java (Database operations)


SearchByName.java


(all are parts of my classes, not the whole class).
can you please tell me m i doing some mistake in distributing the code??
Thank you
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if the problem of "how to auto update jcombobox?" is resolved, your next problem should be a separate topic.
that way you'll get more people looking at the problem, particularly if the subject is clearly defined.

also, you need to post a demo program of the problem, exactly like I posted - compilable/runnable, not snippets.
Like many here, I rarely read snippets (takes too much time). Strip everything unrelated to the problem.
if its nothing db related, simulate the db as an arraylist (as in my demo).
If the problem is db - it should be posted in the jdbc forum.

Basically, post a compilable/runnable demo, and tell us what it's supposed to do, what its actually doing,
and the steps to reproduce the problem

 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually i need to distribute your demo code into two classes one will display textbox and button and second will display the combo box..
i m trying like this:
Testing.java

SearchTesting.java


but i m getting exception:

 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here's a very simple example of that error
run it, confirm same error, if so, work out why, then look for similarities in your code

 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay..
it's going to be an infinite loop..
because in class a i m calling class b and in class b i am calling class a..
no break condition..
it will continue until stack full.
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but how do i distribute it in two classes??
i tried it allot..
but many times there is no exceptions but second class combo is not updating.
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
load student method is loading records into combobox.
save student is saving record of text field.
if want records to be loaded in another panel of other class.
and records which is going to be saved of the text field value is in another class.
so one class will have loadstudent method.
and other will have save student.
so in this case i have to create object of both class in each other.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you seem to have just copied bits and pieces and put them together in any fashion,
which will never work.

e.g. in the class SearchTesting, presumably you want the students loaded when the
program starts, but loadStudents() is in 'public void buildGUI()' - which is called by ??

There are so many errors in the way you've done it, that just fixing them is not going
to help you. You need to find them/understand them/fix them.

couple of options:
1)
print out the 2 classes you've created and after each line write what that line is to do,
or if a method line e.g. public void loadStudents(), what it is to do and/or return.

2)
start again, using my demo as a guide only. if you want a separate class to load the
students, create one. if you want a separate class to save a student, create one. The
class that displays it all (the frame etc), is a separate class to the other 2, so that's now
3 classes. You may need to pass references between the classes.

personally, (2) is the better option.
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay..
i tried to do with the second option, but getting one exception.


i m doing something like this:
Display.java

SaveStudent.java

LoadStudents.java


can i do like this:


i mean this block is returning null value, therefore the exception is.
but don't know how to solve it.
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You declare a LoadStudents object in line 15. You use this object to get the DefaultComboBoxModel in line 36. In between you never call its loadStudents1 method. That's why the DefaultComboBoxModel remains null.
You do call loadStudents1 inside class SaveStudent, but that uses a completely different LoadStudents object. The two are not related, and calling loadStudents1 on one will not set the model of the other.
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you mean i need to pass the reference also..??
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> you mean i need to pass the reference also..??

but not between LoadStudents and SaveStudent - they (should) know nothing about each other.
I had it that way in the demo, when they were just methods in a single class, but it won't
work that same way when in separate classes (it can, but it gets messy)

you need to pass a reference of the comboBox to LoadStudents (via the LoadStudents constructor),
so that after all students are added to the model, the model is set as the comboBox model.

you have at line 32
ss.saveStudent(values);//you need to also pass a reference of the 'database' in the SaveStudent constructor, not create a separate 'database'
have, at line 33
ls.loadStudents1();//the 'database' reference should be part of the LoadStudents constructor
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you very very much...
it's working now..
but i have a question..
i need to connect it with database..
that time also i need arraylist??
flow would be same that time also??
i mean i m persisting value into database and then fetching again from database, why arraylist is required there??
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the arraylist was used to simulate a resultset from a database.

you could probably still use an ArryList, if you wanted to create a db connector class.

just remember to have your db queries in a separate thread, or SwingWorker, so you don't lock up the GUI
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you michael, i have done some modifications.
but it's not updating again..
sorry but i am again doing mistake somewhere..
here is my two classes (save student and load student)..




i also tried without arraylist, but it was again not working.
and also while fetching records in jcombobox, i am not using arraylist anywhere, is this the mistake, but don't know where to use..

Insert1(): method to insert value into database.
NameValues(): method to fetch data.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see any comboBox in that code - the part where you set the model
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is that code..


whenever i am uncommenting this line
//dcbm = ls.ld();
it giving me null pointer exception...
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have no idea what you're doing in these latest snippets, the comboBox snippet
doesn't indicate what method it belongs in.

this
model.addElement((res.getString("names")));
changes to
dcbm.addElement(res.getString("names"));
so, is this another method? or same method with a name change - if so, why? - it's confusing

go back to where you had it working, then add the changes one line at a time.
if you need to change a line, comment it out, don't delete it, so you can see
the old replaced by the new.

perhaps, in the changes you've made, you've added things in the wrong order.
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


dcbm.addElement(res.getString("names"));


it's comboboxmodel.
but here i am directly fetching data from database, no other method i am calling, i m getting confused.
i know it's annoying michael, but can i please post part of my classes to show you what i am doing..
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if the only changes you've made are in SaveStudent and LoadStudents, just post those 2 classes.
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually my saveStudent and loadStudent both are same to those i posted in my third last post, in this post:


thank you michael, i have done some modifications.
but it's not updating again..
sorry but i am again doing mistake somewhere..
here is my two classes (save student and load student)..



but i am doing some mistake when i m calling them..
below is the part of my registration class, i m posting the actionListener which i added to my button:



below is the part of class, where i am loading values into combobox.

i m posting those part of class, which is related to this.
and below is my two methods of Db class.



DbConnection class has the database connection only..
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in SearchByName you create a new comboBox
JComboBox studentName = new JComboBox();

you create a new JPanel
JPanel coursePanel = new JPanel();

you add the comboBox to the panel
coursePanel.add(studentName);

now what do you do with it?
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in my SearchByName class i have to take a argument, DefaultComboBoxModel and set that into JComboBox, but if i pass a argument(DefaultComboBox) to the constructor of SearchByName class, that where i am creating instance of that class, i have to pass a DefaultComboBoxModel.

so i tried like this: i created one method in my class SearchByName (say, Display()), that we earlier have in the Display1 class, and in this method i did all the work which i was doing in constructor of SearchByNameClass.


and i called this method in my Register class which implementing the ActionListener, but not working.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it seems you've brewed up some spaghetti code - it's all over the place

here's a breakdown of what should be where (I've removed the ArrayList stuff, add it back later if you really need it)

this assumes the Button's actionListener class (Register) can 'see' an instance of SaveStudent() and LoadStudents()
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


[LoadStudents() reference].loadStudents1(comboBox reference);//this is where you load the students



you mean by, this will come in my SearchByName class, because i am loading my combobox there, so instead of Register class i put this into my SearchByName class, like this:


others i did in the same manner as you said, but again not updating..
don't know what's going wrong...
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is my comment from last post
//======================================
public class SearchByName
{
//this class needs to be scrapped, but you need to add the panel/combo elsewhere in the program
JComboBox studentName = new JComboBox();
JPanel coursePanel = new JPanel();
coursePanel.add(studentName);
}
//======================================

so, at this stage scrap the class (add the panel/comboBox to the gui some other way).
The name of the class indicates this is additional functionality you're trying to build in.
If so, do that later. Just get the progarm working where:
1) type a name into the textfield
2) click a button
3) the typed-in name is saved to the db
4) the newly saved name appears in the comboBox (indicating it has been saved to the db)

when you have the above working, add your code to search.
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually SearchByName is the class where my JComboBox is in which data is coming from the database, and based on combobox selected value, related data is filled into the textfields, which are also in the same class.


1) type a name into the textfield
2) click a button
3) the typed-in name is saved to the db
4) the newly saved name appears in the comboBox (indicating it has been saved to the db)


this all is working fine, my data is saved into db, and also it appears into the combobox, but for that i have to restart my application.
newly saved name is appears after restarting my application.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> newly saved name is appears after restarting my application.

then something is wrong in LoadStudents - are you sure you're loading them
into the comboBox on the screen, not just loading them into a new comboBox?
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes i m loading that.


view plaincopy to clipboardprint?
public class SearchByName
{
//this class needs to be scrapped, but you need to add the panel/combo elsewhere in the program
ls.loadStudents1(studentNames); // here i am loading it.---------------------------------->
JComboBox studentName = new JComboBox();
JPanel coursePanel = new JPanel();
coursePanel.add(studentName);
}

 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i also tried by returning the comboboxmodel, from my loadstudent1() method, and setting this comboboxmodel to combobox in SearchByName class.
but again the problem is still remains.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as long as you continue using the SearchByName class, you will have problems.

post back when you've modified your code to remove that class.
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay i removed all the code from my four classes in which i m implementing this.
CustomerLayout.java


SearchByName.java


LoadStudents.java

SaveStudent.java


the db class i have posted earlier.
apart from these five class(including db), i have one main class in which i m hiding and showing these panels, on some event.
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


post back when you've modified your code to remove that class.


i can't remove that class, that class has my search form.
and i have to load that combobox into that class.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, can't do anything more when only half-classes are posted
e.g. in CustomerLayout() everything is added to 'JPanel panel', but what is
'panel' added to? - nothing, so it can't possibly have displayed anything.

this is getting very similar to one of your other topics, it just goes around in circles
 
So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic