• Post Reply Bookmark Topic Watch Topic
  • New Topic

Loading Values into JComboBox, depending on selection from another JComboBox?

 
md nayeem
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Loading Values into JComboBox, depending on selection from another JComboBox?
if i hava a jcombobox value with Division likeDhaka,Rajshahi,Rangpur.then i want to load another combobox ciity values like Motijheel,Mirpur,Rajbari .if i select Dhaka from the first jcombobox then automatically the city Motijheel will be selected in the second jcombobox.I need detail explaination about this kind of programming.I am now learning java programming from Scratch.
Untitled.png
[Thumbnail for Untitled.png]
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

What you need is to listen to the selection change in division combo box and (re)populate your city combo accordingly.
Using an ActionListener should do the trick.
Check out https://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#listeners for sample code.
 
Rob Camick
Ranch Hand
Posts: 2714
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example:


Note this example uses the JComboBox.isTableCellEditor property so ActionEvents are not generated when the up/down arrows are used on the combo box. The ActionEvent is only generated when the item is actually selected.
 
Knute Snortum
Sheriff
Posts: 3329
84
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a jar for SwingUtils?
 
Rob Camick
Ranch Hand
Posts: 2714
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops. Sorry, that part of the code is not required for this solution. In the past I was testing something else and forgot to remove it. By the way the SwingUtils code doesn't work in this example although it can be helpful in many other situations.

I'll comment out the code but leave it here in case you are interested in its functionality.

I don't have a jar but I have created a blog for some of my tips/ideas. So you can check out Swing Utils
 
It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!