Results 1 to 3 of 3
  1. #1
    DJCali is offline Member
    Join Date
    Oct 2009
    Posts
    10
    Rep Power
    0

    Default Storing a record set into an array

    Hello Again,

    Yet another road block in my destination to completing my program. I wanted to create a MultiColumn Combobox and needed to store my Database values into an array so I get get them into the Combobox. I'm having trouble getting this code to work properly. It seems that the the combobox wont accept the variable items maybe because it is outside the scope? I tried to move the code around to get it to work but it don't.

    here is the code.

    Java Code:
    package pms;
     
    import java.awt.*;
     
    import javax.swing.*;
     
    import java.sql.*;
     
    public class Test3 extends JFrame {
     
        public static void main(String[] args) {
     
            new Test3();
     
        }
     
        public Test3() {
     
            Container c = getContentPane();
     
            c.setLayout(new BorderLayout());
     
            c.setBackground(Color.gray);
     
            DBConnection db = new DBConnection();
            try {
     
     
     
                Connection conn = DriverManager.getConnection(db.connUrl);
                Statement sql = conn.createStatement();
                ResultSet rs = sql.executeQuery("SELECT * FROM Referrals ORDER BY Referral_Text");
     
                while (rs.next()) {
                    Item[] items = {
                        new Item(rs.getString(2), rs.getString(2)),};
     
                }
     
                JComboBox jcb = new JComboBox(items);
                //Close connections.
                rs.close();
                sql.close();
                conn.close();
     
     
            } catch (SQLException e) {
     
                e.getStackTrace();
            }
     
     
            jcb.setPreferredSize(new Dimension(24, 24));
     
            jcb.setRenderer(new ItemRenderer());
     
            c.add(jcb, BorderLayout.NORTH);
     
            setSize(200, 100);
     
            setLocationRelativeTo(null);
     
            setDefaultCloseOperation(EXIT_ON_CLOSE);
     
            setVisible(true);
     
        }
     
    // The items to display in the combobox...
        class Item {
     
            String itemName = "";
            String itemValue = "";
     
            public Item(String name, String value) {
     
                itemName = name;
     
                itemValue = value;
     
            }
        }
     
    // The combobox's renderer...
        class ItemRenderer extends JPanel implements ListCellRenderer {
     
            private JLabel nameLabel = new JLabel(" ");
            private JLabel valueLabel = new JLabel(" ");
     
            public ItemRenderer() {
     
                setOpaque(true);
     
                setLayout(new GridLayout(1, 2));
     
                add(nameLabel);
     
                add(valueLabel);
     
            }
     
            public Component getListCellRendererComponent(
                    JList list,
                    Object value,
                    int index,
                    boolean isSelected,
                    boolean cellHasFocus) {
     
                nameLabel.setText(((Item) value).itemName);
     
                valueLabel.setText(((Item) value).itemValue);
     
                if (isSelected) {
     
                    setBackground(Color.black);
     
                    nameLabel.setForeground(Color.white);
     
                    valueLabel.setForeground(Color.white);
     
                } else {
     
                    setBackground(Color.white);
     
                    nameLabel.setForeground(Color.black);
     
                    valueLabel.setForeground(Color.black);
     
                }
     
                return this;
     
            }
        }
    }
    Thanks for the help!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default

    You create a new Item array inside your while( ... ) loop so it's gone after the loop has finished. There are more errors but this is the reason your compiler complains.

    kind regards,

    Jos

  3. #3
    DJCali is offline Member
    Join Date
    Oct 2009
    Posts
    10
    Rep Power
    0

    Default

    Thanks Jos. I just need to figure out how to properly fix this code. I've tried a couple of different ways but they don't work.

Similar Threads

  1. Storing in an Array
    By Bascotie in forum New To Java
    Replies: 10
    Last Post: 10-15-2009, 05:12 AM
  2. Storing high score and sorting the array
    By Implode in forum New To Java
    Replies: 8
    Last Post: 09-28-2009, 12:43 AM
  3. storing a string in an array
    By tiyani in forum New To Java
    Replies: 3
    Last Post: 08-12-2009, 07:25 PM
  4. storing strings into an array
    By anthonym2121 in forum New To Java
    Replies: 2
    Last Post: 04-04-2009, 07:32 AM
  5. Storing Array from HTTP Post
    By kskinner in forum New To Java
    Replies: 1
    Last Post: 09-08-2008, 06:00 AM

Posting Permissions

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