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 5 of 5

Thread: Having Problem with my JComboBox

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Having Problem with my JComboBox

    I'm trying to populate my JComboBox with data in my database with the use of arraylist but sadly there must be a mistake in my code can some help me Please and btw my connection is working well.
    In my table (tblcuri) i have 3 fields (fldCurId,fldSy,fldDescp). i want to fldDescp to be displayed in my JComboBox but i don't know how.. Please Help me i'm only a 3day old java use

    ---------------------------------------------------------------------------------

    Code in my dbFunction class:

    public List getCurriculum() {
    ResultSet rsRec;
    List curriculum = new ArrayList();
    String sql;
    sql = "select * from tblcuri order by fldDescp";
    try {
    rsRec = st.executeQuery(sql);
    while (rsRec.next()) {

    curriculum.add(rsRec.getString("fldDescp"));
    }
    rsRec.close();
    } catch (SQLException ex) {
    ex.printStackTrace();
    }
    return curriculum;

    }

    ----------------------------------------------------------------------------------------------

    Code: (student Jframe Class) name of the JCombox(cbCurr)

    public student() {
    initComponents();
    List cList = dbFunctions.getCurriculum();
    cbCurr = new JComboBox();

    Iterator<List> iterator = cList.iterator();
    while (iterator.hasNext()) {
    List list = iterator.next();

    cbCurr.addItem(iterator.next());


    }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,332
    Thanks
    181
    Thanked 829 Times in 772 Posts
    Blog Entries
    5

    Default Re: Having Problem with my JComboBox

    For future reference, please flank you code with the [highlight=java][/highlight] tags or code tags. There are a few problems with the code you posted. It boils down to what you retrieve from the database and what the client tries to use. I presume you wish to add Strings to your JComboBox...your dbFunction retrieves a list of String values and places them in a list, but the client (JFrame class) tries to retrieve a List containing Lists. Using Generics will better enforce these constraints at compile time.
    Iterator<String> iterator = cList.iterator();
    while (iterator.hasNext()) {
    String string = iterator.next();

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Having Problem with my JComboBox

    Sorry for not highlighting my code and Thank you sir for the fast respond i already change it but my JComboBox doesn't show anything only a blank Jcombobox sir

    DbFunction Class
    import java.util.*;
    import java.sql.*;
    import javax.swing.DefaultComboBoxModel;
    import javax.swing.JComboBox;
     
    public class DbFunctions {
     
        private static Connection dbConn;
        private static Statement st;
        private static String dbURL = "jdbc:odbc:studODBC";
        private static String dbUsername = "root";
        private static String dbPassword = "";
     
        public DbFunctions() {
            DbFunctions.connectDB();
        }
     
        public static boolean connectDB() {
            try {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                dbConn = DriverManager.getConnection(dbURL, dbUsername, dbPassword);
                st = dbConn.createStatement();
                return true;
     
            } catch (Exception ex) {
                ex.printStackTrace();
                return false;
            }
        }
         public List getCurriculum() {
            ResultSet rsRec;
            List curriculum = new ArrayList();
            String sql;
            sql = "select * from tblcuri order by fldDescp";
            try {
                rsRec = st.executeQuery(sql);
                while (rsRec.next()) {
                  /*  List row = new ArrayList();
                    row.add(rsRec.getString("fldCurId"));
                   row.add(rsRec.getString("fldSy"));
                   row.add(rsRec.getString("fldDescp"));
                    */
                   curriculum.add(rsRec.getString("fldDescp"));
                }
                rsRec.close();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            return curriculum;
     
        }

    Student (Jframe) class
    import java.awt.Component;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.Connection;
    import java.sql.SQLException;
    import java.sql.Statement;
    import javax.swing.table.DefaultTableModel;
    import java.util.*;
    import java.util.List;
    import javax.swing.DefaultComboBoxModel;
    import javax.swing.JComboBox;
    import javax.swing.*;
     
    public class student extends javax.swing.JFrame {
     
        /** Creates new form student */
        public student() {
            initComponents();
            List cList = dbFunctions.getCurriculum();
     
     
          cbCurr = new JComboBox();
     
            Iterator<String> iterator = cList.iterator();
            while (iterator.hasNext()) {
     
                String string  = iterator.next();
     
             cbCurr.addItem(iterator.next());
     
            }
        }
     public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
     
                public void run() {
                    new student().setVisible(true);
     
                }
            });
        }
    }
    Last edited by dazzl3r; December 8th, 2010 at 10:20 PM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,332
    Thanks
    181
    Thanked 829 Times in 772 Posts
    Blog Entries
    5

    Default Re: Having Problem with my JComboBox

    There are missing portions of your code. It helps both you and us to boil the problem down to a short, concise, and compilable example that can demonstrate the problem. I'd suggest first playing with the JComboBox, adding 'dummy' values to verify it is working in the context you have. Then expand that to incorporate you database values. When in doubt, add some System.out.println statements to make sure the values are what you expect. In other words, one of the keys to debugging is to break the problem down into individual components, get them to work, then expand them into each other
    Last edited by copeg; December 8th, 2010 at 10:53 PM.

  5. The Following User Says Thank You to copeg For This Useful Post:

    dazzl3r (December 8th, 2010)

  6. #5
    Junior Member
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Having Problem with my JComboBox

    Thank Very Much sir my JcomboBox is working now thanks again

Similar Threads

  1. Problem with reset on JComboBox
    By WorkingMan in forum AWT / Java Swing
    Replies: 4
    Last Post: April 25th, 2013, 12:19 PM
  2. Problem getting selected item as string from JComboBox
    By lost in forum AWT / Java Swing
    Replies: 1
    Last Post: October 26th, 2010, 03:22 AM
  3. [SOLVED] JCombobox help
    By sman36 in forum AWT / Java Swing
    Replies: 10
    Last Post: August 11th, 2010, 04:11 AM
  4. JComboBox
    By nasi in forum AWT / Java Swing
    Replies: 1
    Last Post: April 29th, 2010, 08:40 AM
  5. JComboBox and Textfield
    By Nexusfactor in forum AWT / Java Swing
    Replies: 3
    Last Post: November 9th, 2009, 11:57 AM