Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Dynamic JComboBoxes

  1. #1
    Member
    Join Date
    Apr 2011
    Location
    Niperia
    Posts
    30
    My Mood
    Amazed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Dynamic JComboBoxes

    Am trying to change the contents of a JComboBox - phoneModelCombo based on items selected by in another JComboBox - brandCombo. I will appreciate any resource, help that would help me fix this issue.
    Here is an excerpt of what i have tried to no avail:

            brandCombo.addActionListener(new ActionListener(){
            	public void actionPerformed(ActionEvent ae){
            	  try{
                     try{
            		Class.forName("com.mysql.jdbc.Driver").newInstance();
            	     }catch(Exception e){
            	       JOptionPane.showMessageDialog(null, "Exception: "+e.getMessage());
            	     }
            	Connection con = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/phoneshopsystem","root", dbLogin.db());
     final String query2 = "SELECT phoneModels FROM `phones` WHERE phoneBrand = ?";
    PreparedStatement pst = con.prepareStatement(query2, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        ResultSet rset = pst.executeQuery();
            	while(rset.next()){
            	    JComboBox bx = (JComboBox)ae.getSource();
            		String b = bx.getSelectedItem().toString();
            		  pst.setString(2, b);
            		   phoneModelCombo.addItem(rset.getString                   ("phoneModels"));
    }
           con.close();
    }catch(SQLException sql){
     }
       }
         });


  2. #2
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Dynamic JComboBoxes

    and what is the results when you run the code?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Dynamic JComboBoxes

    Can you make a small, complete program that compiles, executes and shows the problem. It shouldn't require a DB. Use an array as the source for the rset data items.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Member
    Join Date
    Apr 2011
    Location
    Niperia
    Posts
    30
    My Mood
    Amazed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Dynamic JComboBoxes

    @Muhammad: the phoneModelComboBox remains blanks Even when i select an item from brandCombo.
    Thanks @Mr. Norm

  5. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Dynamic JComboBoxes

    are you sure of ur db connections and data presence?
    Im actually not good at JDB.
    But i think the problem is in an event you are listening to.
    If your intention is that phoneModelComboBox get filled with data when you select an item from brandCombo,
    you should add ItemListener instead.

    That's:
    brandCombo.addItemListener(new ItemListener(){
            	public void itemStateChanged(ItemEvent ie){

    intead of:

    brandCombo.addActionListener(new ActionListener(){
            	public void actionPerformed(ActionEvent ae){


    --- Update ---

    are you sure of ur db connections and data presence?
    Im actually not good at JDB.
    But i think the problem is in an event you are listening to.
    If your intention is that phoneModelComboBox get filled with data when you select an item from brandCombo,
    you should add ItemListener instead.

    That's:
    brandCombo.addItemListener(new ItemListener(){
            	public void itemStateChanged(ItemEvent ie){

    intead of:

    brandCombo.addActionListener(new ActionListener(){
            	public void actionPerformed(ActionEvent ae){

  6. #6
    Member
    Join Date
    Apr 2011
    Location
    Niperia
    Posts
    30
    My Mood
    Amazed
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Dynamic JComboBoxes

    I Tried that before the ActionListener and it gave me the same thing, which was nothing! I'll keep trying though!

    --- Update ---

    Am sure of my data presence and DB Connection, because i have used the DefaultComboBox model to populate ID values from my database and it didnt give me any problems!

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Dynamic JComboBoxes

    For help from others see post#3
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. dynamic dispatch
    By navi in forum Member Introductions
    Replies: 1
    Last Post: April 12th, 2012, 07:50 AM
  2. Dynamic JComboBoxes ?
    By shad3d in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 16th, 2011, 03:34 PM
  3. Dynamic UI
    By ganeshkumar.salvi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 8th, 2011, 04:54 AM
  4. JTable JComboBoxes
    By aussiemcgr in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 8th, 2010, 02:46 PM
  5. JComboBoxes & Events
    By KevinGreen in forum AWT / Java Swing
    Replies: 3
    Last Post: April 21st, 2010, 05:11 AM