Results 1 to 3 of 3
- 10-24-2009, 03:53 AM #1
Member
- Join Date
- Oct 2009
- Posts
- 10
- Rep Power
- 0
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.
Thanks for the help!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; } } }
- 10-25-2009, 01:06 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
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
- 10-25-2009, 02:47 PM #3
Member
- Join Date
- Oct 2009
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Storing in an Array
By Bascotie in forum New To JavaReplies: 10Last Post: 10-15-2009, 05:12 AM -
Storing high score and sorting the array
By Implode in forum New To JavaReplies: 8Last Post: 09-28-2009, 12:43 AM -
storing a string in an array
By tiyani in forum New To JavaReplies: 3Last Post: 08-12-2009, 07:25 PM -
storing strings into an array
By anthonym2121 in forum New To JavaReplies: 2Last Post: 04-04-2009, 07:32 AM -
Storing Array from HTTP Post
By kskinner in forum New To JavaReplies: 1Last Post: 09-08-2008, 06:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks