Results 1 to 10 of 10
  1. #1
    ntf
    ntf is offline Member
    Join Date
    Nov 2014
    Posts
    5
    Rep Power
    0

    Default passing JComboBox to another class , JAVA GUI

    Java Code:
      createConnection(); 
      
          try{   
        String str = "select * from Stocks";
        stmt = conn.prepareStatement(str);
        ResultSet rs = stmt.executeQuery();
     
    	while(rs.next())     
    	    jcbStock.addItem(rs.getString("stockID"));        
                    }catch(Exception e){
                    e.printStackTrace();           
            }
    I want to put this code in a class then call it in another class , but i do not how to do that. Anyone can help me?
    This code is working fine in my main class , but i want to write this code to another class for call it in main class.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: passing JComboBox to another class , JAVA GUI

    Make it a method, pass a reference to the class it is in to the other class. In the other class use the reference to call the method in the first class.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ntf
    ntf is offline Member
    Join Date
    Nov 2014
    Posts
    5
    Rep Power
    0

    Default Re: passing JComboBox to another class , JAVA GUI

    Java Code:
       public void combobox(JComboBox jcb){ 
            try{   
        String str = "select * from stocks";
        stmt = conn.prepareStatement(str);
        ResultSet rs = stmt.executeQuery();
    
    	while(rs.next())     
    	    jcb.addItem(rs.getString("stockID"));        
                    }catch(Exception e){
                    e.printStackTrace();           
                    }
        }
    Is it look like this?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: passing JComboBox to another class , JAVA GUI

    What happened when you compiled and executed it?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    ntf
    ntf is offline Member
    Join Date
    Nov 2014
    Posts
    5
    Rep Power
    0

    Default Re: passing JComboBox to another class , JAVA GUI

    NullPointerException

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: passing JComboBox to another class , JAVA GUI

    What variable had the null value? Why didn't that variable have a good value?
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    ntf
    ntf is offline Member
    Join Date
    Nov 2014
    Posts
    5
    Rep Power
    0

    Default Re: passing JComboBox to another class , JAVA GUI

    the combobox in the main class cannot get the value

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: passing JComboBox to another class , JAVA GUI

    Why is that happening? What was null that caused the exception?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    ntf
    ntf is offline Member
    Join Date
    Nov 2014
    Posts
    5
    Rep Power
    0

    Default Re: passing JComboBox to another class , JAVA GUI

    Thanks for trying to help me , i already solved the problem. Thanks again.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: passing JComboBox to another class , JAVA GUI

    Glad you figured it out.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: 01-08-2014, 05:09 PM
  2. Passing instance of derived class as base class
    By goatjugsoup in forum New To Java
    Replies: 7
    Last Post: 08-01-2012, 05:15 AM
  3. Replies: 7
    Last Post: 06-18-2012, 09:31 PM
  4. Help with passing String[] from GUI class to GUI class
    By X75TIGER75X in forum New To Java
    Replies: 9
    Last Post: 05-01-2012, 06:55 PM
  5. Replies: 8
    Last Post: 04-14-2010, 05:49 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •