Question

Hi i have a j list where i want to put some database value and want to create jlist automatically but when i try to do this i am not able to achieve this when do i got only one value from database to jlist how can i achieve this?

here is my code

   public class RootSelection1  {
    private String connectionURL = "jdbc:mysql://localhost:3306/Trainpis";
    private String s1="";
    private String s2="";
    private final Map<String, ImageIcon> imageMap;

    public RootSelection1() {

            try{
                 Class.forName("com.mysql.jdbc.Driver");
                 Connection conn = DriverManager.getConnection(connectionURL, "root", "");
                 Statement st=conn.createStatement();
                 ResultSet rs=st.executeQuery("Select route,fromr from route");
                 while(rs.next()){
                    s1=rs.getString("route");
                    s2=rs.getString("fromr");
                 }
            } catch(Exception e) {
            }

            String[] nameList={s1,s2};
            imageMap = createImageMap(nameList);
            JList list = new JList(nameList);
            list.setCellRenderer(new MarioListRenderer());

            JScrollPane scroll = new JScrollPane(list);
            scroll.setPreferredSize(new Dimension(300, 400));

            JFrame frame = new JFrame();
            frame.add(scroll);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }

        public class MarioListRenderer extends DefaultListCellRenderer {

            Font font = new Font("helvitica", Font.BOLD, 24);

            @Override
            public Component getListCellRendererComponent(
                    JList list, Object value, int index,
                    boolean isSelected, boolean cellHasFocus) {

                JLabel label = (JLabel) super.getListCellRendererComponent(
                        list, value, index, isSelected, cellHasFocus);
                label.setVerticalTextPosition(JLabel.TOP);
                label.setHorizontalTextPosition(JLabel.CENTER);
                label.setBorder(new MatteBorder( 0, 0, 2, 0, Color.GRAY));
                label.setIcon(imageMap.get((String) value));
                label.setHorizontalTextPosition(JLabel.RIGHT);
                label.setFont(font);
                return label;
            }
        }

        private Map<String, ImageIcon> createImageMap(String[] list) {
            Map<String, ImageIcon> map = new HashMap<>();
            for (String s : list) {
                map.put(s, new ImageIcon("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\yellow.png"));
            }
            return map;
        }

         public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    new RootSelection1 ();

                   // ComboboxDemo cb=new ComboboxDemo();
                    //System.out.println(cb.a);
                }
            });
        }
    }

Thanks in advance

Was it helpful?

Solution

You got only one value (one row) because

while(rs.next()){
    s1=rs.getString("route");
    s2=rs.getString("fromr");
    }

has looped already thru the whole record set and s1,s2 hold the values of the last row in the record set.

Try something like this:

        List<String> nameList = new ArrayList<String>();
        try{
             Class.forName("com.mysql.jdbc.Driver");
             Connection conn = DriverManager.getConnection(connectionURL, "root", "");
             Statement st=conn.createStatement();
             ResultSet rs=st.executeQuery("Select route,fromr from route"
             while(rs.next()){
                String s1=rs.getString("route");
                String s2=rs.getString("fromr");
                nameList.add(s1+" "+s2);
             }
        } catch(Exception e) {
        }

        JList list = new JList(nameList.toArray());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top