943,469 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 9567
  • Java RSS
Jun 2nd, 2003
0

Filling a ComboBox....

Expand Post »
Hi,
I'm in a IntrotoOOP class at my school, and my instructor asked me to look up how to fill a Java Swing Class ComboBox with data from a MySql Database. Does anyone know? This question has been quite hard to find any answers on and I have been really bugged by it recently.
Thanks!
Fyrelion
P.S.Any help at all would be really appreciated!!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Fyrelion is offline Offline
2 posts
since May 2003
Jul 17th, 2009
-1

Re: Filling a ComboBox....

JAVA Syntax (Toggle Plain Text)
  1. private static void queryCountry(){
  2. JC_country.addItem("(Choose)");
  3. try {
  4. String query = DBConnect.Select("addr_ref", "country, description",
  5. "region = ''", "country");
  6. Statement stmt = DBConnect.getConnection().createStatement();
  7. ResultSet rs = stmt.executeQuery(query);
  8. while (rs.next()){
  9. JC_country.addItem(rs.getString(1)+"-"+rs.getString(2));
  10. }
  11. rs.close();
  12. stmt.close();
  13. } catch (SQLException e) {
  14. e.printStackTrace();
  15. }
  16. }
Reputation Points: 6
Solved Threads: 0
Newbie Poster
oliver_lundag is offline Offline
19 posts
since Jul 2009
Jul 17th, 2009
0

Re: Filling a ComboBox....

[code = JAVA]
private JComboBox getJC_country(){
if(JC_country== null){
JC_country = new JComboBox();
JC_country.setSize(new Dimension(260, 20));
JC_country.setLocation(new Point(80, 230));
JC_country.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
try{
if (JC_country.getSelectedItem().toString().equalsIgnoreCase("(Choose)") == true){
JC_country.removeAllItems();
queryCountry();
}//end of if
if (JC_country.getSelectedItem().toString().equalsIgnoreCase("(Choose)") == true){
Country_Code = "Nothing";
JC_region.removeAllItems();
queryRegion();
JC_region.setSelectedIndex(0);
JC_province.setSelectedIndex(0);
}//end of if
Country_Code = JC_country.getSelectedItem().toString().substring(0, JC_country.getSelectedItem().toString().indexOf("-"));
String Filter_By_Code = "select city_municipality,description from addr_ref " +
"where country = '"+Country_Code +"'" +
"and"+
" region != '' " +
"and"+
" province != '' " +
"and"+
" city_municipality != '' "+
"and"+
" brgy_village = '' " +
"and"+
" street = '' ";
try{
tableMain.setQuery(Filter_By_Code);
}catch(Exception exp){
}//end of try catch
JC_region.removeAllItems();
queryRegion();
JC_region.setSelectedIndex(0);
JC_province.setSelectedIndex(0);
}catch(Exception e){
}//end of try catch
}// end of actionPerformed
});
}//end of JC_country.addActionListener(new ActionListener(){
return JC_country;
}//end of method getJC_country()
[/code]
Reputation Points: 6
Solved Threads: 0
Newbie Poster
oliver_lundag is offline Offline
19 posts
since Jul 2009
Jul 17th, 2009
0

Re: Filling a ComboBox....

[code = JAVA]

private JComboBox getJC_country(){
if(JC_country== null){
JC_country = new JComboBox();
JC_country.setSize(new Dimension(260, 20));
JC_country.setLocation(new Point(80, 230));
JC_country.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0) {
try{
if (JC_country.getSelectedItem().toString().equalsIgnoreCase("(Choose)") == true){
JC_country.removeAllItems();
queryCountry();
}//end of if
if (JC_country.getSelectedItem().toString().equalsIgnoreCase("(Choose)") == true){
Country_Code = "Nothing";
JC_region.removeAllItems();
queryRegion();
JC_region.setSelectedIndex(0);
JC_province.setSelectedIndex(0);
}//end of if
Country_Code = JC_country.getSelectedItem().toString().substring(0, JC_country.getSelectedItem().toString().indexOf("-"));
String Filter_By_Code = "select city_municipality,description from addr_ref " +
"where country = '"+Country_Code +"'" +
"and"+
" region != '' " +
"and"+
" province != '' " +
"and"+
" city_municipality != '' "+
"and"+
" brgy_village = '' " +
"and"+
" street = '' ";
try{
tableMain.setQuery(Filter_By_Code);
}catch(Exception exp){
}//end of try catch
JC_region.removeAllItems();
queryRegion();
JC_region.setSelectedIndex(0);
JC_province.setSelectedIndex(0);
}catch(Exception e){
}//end of try catch
}// end of actionPerformed
});
}//end of JC_country.addActionListener(new ActionListener(){
return JC_country;
}//end of method getJC_country()
[/code]
Reputation Points: 6
Solved Threads: 0
Newbie Poster
oliver_lundag is offline Offline
19 posts
since Jul 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: problem with controlling JRadio button
Next Thread in Java Forum Timeline: tabbedpane





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC