Results 1 to 12 of 12
  1. #1
    matpj is offline Member
    Join Date
    Sep 2008
    Posts
    42
    Rep Power
    0

    Default Populating a combo-box in a JFrame with resultset from database

    Hi all,
    I am using Netbeans and have created a new JFrame (in design mode)
    I have added a combo box which I want to populate with the results of a database query.


    Does anyone have any code they can share which does that kind of thing?

    alternatively can anybody advise me on how to go about doing it.
    do I put the code that reads the database in the same 'class' that constructs the JFrame?

    Thanks in advance,
    Matt

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    you need to go to the Sun Swing tutorials and read up how to create and fill a JComboBox with data. There are some pretty decent examples on that site that should get you started. Good luck.

    Also, I recommend that you keep your GUI and database interacting classes separate for good OOP design.

  3. #3
    matpj is offline Member
    Join Date
    Sep 2008
    Posts
    42
    Rep Power
    0

    Default

    Thanks for the response.
    i'll check out those tutorials.

  4. #4
    matpj is offline Member
    Join Date
    Sep 2008
    Posts
    42
    Rep Power
    0

    Default

    Hi,

    I am struggling after having read the tutorials.
    I can populate a combo box with an Array I am declaring in the method, but I cannot seem to populate one with an ArrayList from an external method.

    it compiles and runs fine, but I have no values in the combobox

    This is the section of my code that does the work.
    you can see the instnace of JComboBox I have created.
    and the instance of my EntityProvider which returns an ArrayList called 'fileArray'
    I am trying to loop through this array and add an item to the combobox with each pass, but it doesn't seem to be working..


    Java Code:
    JComboBox formatBox = new JComboBox();
    
        public FormatFrame2(){
            super("Choose a Format");
            
    
            setSize(220, 150);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel pane = new JPanel();
            JLabel formatLabel = new JLabel("Output formats:");
            pane.add(formatLabel);
            EntityProvider entList = new EntityProvider();
            for(String name : entList.fileArray)
                    formatBox.addItem(name);
    am I doing something blatantly wrong??

    thanks,
    Matt

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I don't see anything that jumps out at me. You know what you have to do: break this code down into small compilable bits and test each bit to see what works and what doesn't,.... use a debugger or multiple println debug strings, until you isolate the problem...

  6. #6
    matpj is offline Member
    Join Date
    Sep 2008
    Posts
    42
    Rep Power
    0

    Default

    hmm, well, the fileArray IS being populated as I wrote a simple class to print the contents of it to the screen.

    The JFrame is being created with a combobox the size of the actual frame, but it is just an empty combobox, so not adding the items to it.
    With a breakpoint it gets to the FOR loop:
    Java Code:
     //Fill JComboBox with values retrieved from EntityProvider Class
            for(String name :comboFill.fileArray){
                  entity.addItem(name);
            }
    but jumps straight over it.
    I have checked the local variables in the debugger and i'm not sure if that is supposed to show me the contents of fileArray array or not.
    if so, there isn't anything in it.

    the line that creates the new instance of the EntityProvider is as follows:
    Java Code:
    EntityProvider comboFill = new EntityProvider();
    entityProvider DOES populate 'fileArray' when it is called from my testing class for printing to the screen.

    any help will be gratefully received as i'm going to start to cry in a minute..
    thanks in advance,
    Matt

  7. #7
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    for(String name : entList.fileArray)
    The .fileArray looks suspicious to me, although I am not familiar with EntityProvider. Why would EntityProvider expose a property? Or did you forget the ()?

    You're using enhanced for, and the second entry must provide some sort of a list. The Apache EntityProvider has an iterator() method, which is a good sign. Try taking the ".fileArray" off.

  8. #8
    matpj is offline Member
    Join Date
    Sep 2008
    Posts
    42
    Rep Power
    0

    Default

    EntityProvider is my class which reads a list of financial entities from our Database and returns an ArrayList called fileArry:
    Java Code:
    public class EntityProvider {
        ArrayList <String> fileArray = new ArrayList <String>();
    
        public ArrayList readEntities() throws SQLException {
    
            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
            int count = 0;
    
            String serverName = "francisca";
            int port = 1521;
            String user = "niku";
            String password = "niku";
            String SID = "nikuuat";
            String URL = "jdbc:oracle:thin:@" + serverName + ":" + port + ":" + SID;
    
            Connection conn = DriverManager.getConnection(URL, user, password);
            String SQL = "SELECT DISTINCT level2_name " +
                    "FROM niku.nbi_dim_obs " +
                    "WHERE level1_name = 'DSTi Global' " +
                    "AND level2_name is not null";
    
            String newLine = System.getProperty("line.separator");
    
    
            Statement stat = conn.createStatement();
            ResultSet rs = stat.executeQuery(SQL);
            
    
            while (rs.next()) { //cycle through result set
                
                fileArray.add(rs.getString(1));
           }
           stat.close();
           conn.close();
    
            return fileArray;
      }
    }
    the enhanced FOR that I am using now is as follows:
    Java Code:
    //Fill JComboBox with values retrieved from EntityProvider Class
            for(String name :comboFill.fileArray){
                  entity.addItem(name);
            }
    before I get to this FOR loop in the de-bugger I cannot see anything in fileArray.

  9. #9
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    OK. I didn't know where EntityProvider was coming from, so I googled it and found an Apache project with a class by that name.

    Here are a couple of suggestions:

    • Avoid accessing properties of a class directly. Use a getter method instead.
    • In an enhanced for, you will find using a local field easier to debug than using a value returned by a method call. The reason is that you can't see the object returned by the method call. Personally, I add extra local fields for all kinds of similar situations, such as "return object;", simply because I have been burned so many times during debugging.

    Now that I made you read my advice, the problem is that you never called

    EntityProvider.readEntities()

    You only constructed the object. Therefore, the ArrayList is empty.

  10. #10
    pinks_70986 is offline Member
    Join Date
    Feb 2009
    Posts
    18
    Rep Power
    0

    Default keylistener

    Can you tell me what if I want to populate the combo box on keypress event how should I do it?I tried writing combobox.addKeyListener(keypressed(...)...) but it doesnt enter in the method if I press any key.

  11. #11
    pinks_70986 is offline Member
    Join Date
    Feb 2009
    Posts
    18
    Rep Power
    0

    Default

    bounce.........

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    pinks, you should start a new thread if you are asking a question rather than tag on the end of an old question. Also, you may wish to go into more detail on just what you are wishing to achieve here.

Similar Threads

  1. Populating a drop down list from a database
    By matpj in forum New To Java
    Replies: 0
    Last Post: 01-19-2009, 01:14 PM
  2. creation of one combo box form another combo box
    By er.tyagigaurav in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-09-2008, 04:14 PM
  3. Pre-Populating an Outlook Email
    By KeithHenn in forum Advanced Java
    Replies: 3
    Last Post: 10-23-2008, 04:13 PM
  4. graph + combo box
    By abelah in forum New To Java
    Replies: 7
    Last Post: 10-13-2008, 06:36 PM

Posting Permissions

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