0

Hi I would like to print to a cell in a table image that works as a url. I have the url stored in my database table data as text. I put the method I'm using to print the rest of data in the other columns, I would like to add another column with the image link.

any help would be good.

 public void SearchMovie() throws SQLException {

        try {
            Connection con = null;
            ResultSet rs = null;
            Statement st = null;

            String Genre = ComboGenero.getSelectedItem().toString();
            String Era = ComboEra.getSelectedItem().toString();
            String Clsssification = ComboClasification.getSelectedItem().toString();

            String sql = "select Poster,Title,Year,Country ,imdb ,Trailer from movie where Genre ='" + Genre + "'";

            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost/whichmovie", "Asis", "dekrayat24");
            System.out.println("Conectado a la base de datos SQLite");

            st = con.createStatement();
            rs = st.executeQuery(sql);

            jTable1.setDefaultRenderer(Object.class, new IconCellRenderer());
            DefaultTableModel model = new DefaultTableModel();
            this.jTable1.setModel(model);


            // Donde 50 es el tamaño que querramos en la celda según lo q necesitemos
            jTable1.setRowHeight(55);
            jTable1.setCellSelectionEnabled(true);

            ResultSetMetaData rsMD = rs.getMetaData();
            int numcolumnas = rsMD.getColumnCount();

            for (int x = 1; x <= numcolumnas; x++) {
                model.addColumn(rsMD.getColumnLabel(x));

            }

            if (!rs.next()) {
                ResultadosLabel.setText("No Movies found ");
            } else {
                do {

                    Object[] fila = new Object[numcolumnas];
                    for (int i = 0; i < numcolumnas; i++) {
                        fila[i] = rs.getObject(i + 1);

                        byte[] imagedataCover = rs.getBytes("Poster");
                        format = new ImageIcon(imagedataCover);

                        byte[] imagedataCountry = rs.getBytes("Country");
                        format2 = new ImageIcon(imagedataCountry);

                        fila[0] = new JLabel(format);
                        fila[3] = new JLabel(format2);
                        ResultadosLabel.setText(i - 2 + " " + "Movies found ");
                    }
                    model.addRow(fila);
                    setAnchoColumnas();
                } while (rs.next());
                rs.close();
                st.close();
                con.close();

            }

        } catch (ClassNotFoundException ex) {
            System.out.println(ex.getMessage());

        }

    }
user3325719
  • 75
  • 4
  • 14

1 Answers1

0

You need to define the number of colums when you define a table model Instead of using the default constructor for DefaultTableModel. you can use the following.

public DefaultTableModel(Object[][] data, Object[] columnNames) {
        setDataVector(data, columnNames);
}

This is just a snippet from DefaultTableModel class, see the java docs for more info.

you can also refer this example which does something similar, might help you

Most simple code to populate JTable from ResultSet

Community
  • 1
  • 1
rahul pasricha
  • 931
  • 1
  • 14
  • 35