0

I was developing a Swing App where I have a menu with different options, based on which the App show an image, which is contain into a JScrollPane of another JPanel.

Finally when yo make double click into the Image, appearance a description about the Image.

My problem is that when I click the Button, The ScrollPaint appears, but doesn't load the Image.

Here my code:

VentanaPrincipal

public class VentanaPrincipal extends JFrame {
public VentanaPrincipal() {
        super();
        setTitle("Formulario de Reservas – Manuel Lucas Sánchez");
        createMenu();
        initsComponents(); // Here I setUp the components.
        
    }
    public void cargaPanel(JPanel panel, String ruta) {
        scrollPane.setVisible(true);
        scrollPane.setViewportView(panel);
        
        pFoto = new PanelFoto(ruta);
        scrollPane.add(pFoto);
    }
    
    public PanelFoto getpFoto() {
        return pFoto;
    }
    public void setControlador(Controlador controlador) {
        mntmhbDoble.addActionListener(controlador);
        mntmhbPresidencial.addActionListener(controlador);
        mntmhbSimple.addActionListener(controlador);
        mntmhbSuite.addActionListener(controlador);
        mntmhbSuperior.addActionListener(controlador);
        mntmSalir.addActionListener(controlador);
        mntmseGym.addActionListener(controlador);
        mntmsePisc.addActionListener(controlador);
        mntmseRest.addActionListener(controlador);
    }
}

PanelFoto

public class PanelFoto extends JPanel {

    private Image img;
    private JTextArea txtAreaDescripcion;
    private JScrollPane conteinerFoto;
    private JLabel imageLabel;
    private ImageIcon ii;
    public PanelFoto(String ruta) {
        initsComponents(ruta);
    }
    public PanelFoto() {
        
    }
    private void initsComponents(String ruta) {
    
        setSize(550,600);
        setLayout(null);
/*
        ImageIcon ii = new ImageIcon(file_path);
        jScrollPane1 = new JScrollPane(new JLabel(ii));*/
    
        
        txtAreaDescripcion = new JTextArea();
        txtAreaDescripcion.setBounds(0, 449, 550, 151);
        add(txtAreaDescripcion);
        
        ii = new ImageIcon(ruta);
        conteinerFoto = new JScrollPane(new JLabel(ii));
        conteinerFoto.setBounds(0, 0, 550, 449);

        add(conteinerFoto);
    }

    public JTextArea getTxtAreaDescripcion() {
        return txtAreaDescripcion;
    }
    
    public JScrollPane getConteinerFoto() {
        return conteinerFoto;
    }
    
    public void setControlador(Controlador controlador) {
        this.addMouseListener((MouseListener) controlador);
    }

    public Image getImg() {
        return img;
    }

    public void setImg(Image img) {
        this.img = img;
    }
}

Controlador

public class Controlador implements ActionListener, MouseListener {

    private VentanaPrincipal vPrincipal;
    private PanelFoto pFoto;
    
    
    public Controlador(VentanaPrincipal vPrincipal, PanelFoto pFoto) {
        this.vPrincipal=vPrincipal;
        this.pFoto=pFoto;
    }
    
    @Override
    public void actionPerformed(ActionEvent ae) {
        
        if(ae.getSource() instanceof JMenu || ae.getSource() instanceof JMenuItem) {
            
            if(((JMenuItem) ae.getSource()).getText().equals(VentanaPrincipal.MNTM_HABSIMPLE)) {
                vPrincipal.cargaPanel(pFoto, "img/simple.jpg");
                vPrincipal.getpFoto().addMouseListener(new MouseListener() {
                    
                    @Override
                    public void mouseReleased(MouseEvent e) {
                    }
                    
                    @Override
                    public void mousePressed(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseExited(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseEntered(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if(e.getSource().equals(vPrincipal.getpFoto())) {
                            vPrincipal.getpFoto().getTxtAreaDescripcion().setText("Superficie de la habitacion: 20 metros\n" + 
                                    "Ducha\n" + 
                                    "Toallas\n" + 
                                    "Secador de  pelo\n" + 
                                    "TV\n" + 
                                    "Politica de humo: no se puede fumar\n" + 
                                    "");
                        }
                    }
                });
                Image img = null;
                try {
                    img = ImageIO.read(new File("img/simple.jpg"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //ImageIcon ImgIcon = new ImageIcon(Img);
                pFoto.setImg(img);
                
            }else if (((JMenuItem) ae.getSource()).getText().equals(VentanaPrincipal.MNTM_HABDOBLE)) {
                    vPrincipal.cargaPanel(pFoto, "img/doble.jpg");
                    
                    vPrincipal.getpFoto().addMouseListener(new MouseListener() {
                    
                    @Override
                    public void mouseReleased(MouseEvent e) {
                    }
                    
                    @Override
                    public void mousePressed(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseExited(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseEntered(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if(e.getSource().equals(vPrincipal.getpFoto())) {
                            vPrincipal.getpFoto().getTxtAreaDescripcion().setText("Superficie de la habitacion: 30 m\n" + 
                                    "Ducha \n" + 
                                    "Toallas y albornoces\n" + 
                                    "Secador de  pelo\n" + 
                                    "TV LED\n" + 
                                    "Dos camas dobles");
                        }
                    }
                });
                Image Img = null;
                try {
                    Img = ImageIO.read(new File("img/doble.jpg"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //ImageIcon ImgIcon = new ImageIcon(Img);
                pFoto.setImg(Img);
                
            }else if (((JMenuItem) ae.getSource()).getText().equals(VentanaPrincipal.MNTM_HABSUITE)) {
                vPrincipal.cargaPanel(pFoto, "img/suite.jpg");
vPrincipal.getpFoto().addMouseListener(new MouseListener() {
                    
                    @Override
                    public void mouseReleased(MouseEvent e) {
                    }
                    
                    @Override
                    public void mousePressed(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseExited(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseEntered(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if(e.getSource().equals(vPrincipal.getpFoto())) {
                            vPrincipal.getpFoto().getTxtAreaDescripcion().setText("Superficie de la habitacion: 50 metros\n" + 
                                    "Banera hidromasaje\n" + 
                                    "Todo tipo de amenities\n" + 
                                    "Caja fuerte\n" + 
                                    "Terraza vistas mar\n" + 
                                    "Cama King Size");
                        }
                    }
                });
                Image Img = null;
                try {
                    Img = ImageIO.read(new File("img/suite.jpg"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //ImageIcon ImgIcon = new ImageIcon(Img);
                pFoto.setImg(Img);
            }else if (((JMenuItem) ae.getSource()).getText().equals(VentanaPrincipal.MNTM_HABPRESI)) {
                vPrincipal.cargaPanel(pFoto, "img/presidencial.jpg");
                    vPrincipal.getpFoto().addMouseListener(new MouseListener() {
                    
                    @Override
                    public void mouseReleased(MouseEvent e) {
                    }
                    
                    @Override
                    public void mousePressed(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseExited(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseEntered(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if(e.getSource().equals(vPrincipal.getpFoto())) {
                            vPrincipal.getpFoto().getTxtAreaDescripcion().setText("Superficie de la habitacion: 90 metros\r\n" + 
                                    "SPA privado\r\n" + 
                                    "Servicio de habitacion personal\r\n" + 
                                    "Jardin privado con piscina\r\n" + 
                                    "HomeCinema\r\n" + 
                                    "Cama King Size");
                        }
                        
                    }
                });
                Image Img = null;
                try {
                    Img = ImageIO.read(new File("img/presidencial.jpg"));
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                //ImageIcon ImgIcon = new ImageIcon(Img);
                pFoto.setImg(Img);
                
            }else if (((JMenuItem) ae.getSource()).getText().equals(VentanaPrincipal.MNTM_SERREST)) {
                vPrincipal.cargaPanel(pFoto, "img/restaurante.jpg");
    vPrincipal.getpFoto().addMouseListener(new MouseListener() {
                    
                    @Override
                    public void mouseReleased(MouseEvent e) {
                    }
                    
                    @Override
                    public void mousePressed(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseExited(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseEntered(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if(e.getSource().equals(vPrincipal.getpFoto())) {
                            vPrincipal.getpFoto().getTxtAreaDescripcion().setText("El ambiente informal y acogedor de nuestro restaurante esta decorado con un estilo mediterraneo moderno. Pruebe una especialidad local o internacional acompañado de un te JING o de un vino fino. La tranquila terraza es ideal para realizar eventos sociales y fiestas con cocteles.");
                        }
                    }
                });
                Image Img = null;
                try {
                    Img = ImageIO.read(new File("img/restaurante.jpg"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //ImageIcon ImgIcon = new ImageIcon(Img);
                pFoto.setImg(Img);
            }else if (((JMenuItem) ae.getSource()).getText().equals(VentanaPrincipal.MNTM_SERPISC)) {
                vPrincipal.cargaPanel(pFoto, "img/spa.jpg");
vPrincipal.getpFoto().addMouseListener(new MouseListener() {
                    
                    @Override
                    public void mouseReleased(MouseEvent e) {
                    }
                    
                    @Override
                    public void mousePressed(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseExited(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseEntered(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if(e.getSource().equals(vPrincipal.getpFoto())) {
                            vPrincipal.getpFoto().getTxtAreaDescripcion().setText("Nuestro spa es un lugar para relajarse y beneficiarse de las propiedades del agua mineromedicinal en sus confortables e innovadoras instalaciones. Nuestro equipo trabaja en la incorporacion de las mejores tecnicas, combinadas con la aplicacion de nuestras aguas mineromedicinales y productos cosmeticos de alta gama, para crear tratamientos unicos para nuestros clientes.\r\n" + 
                                    "");
                        }
                    }
                });
                Image Img = null;
                try {
                    Img = ImageIO.read(new File("img/spa.jpg"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //ImageIcon ImgIcon = new ImageIcon(Img);
                pFoto.setImg(Img);
            }else if (((JMenuItem) ae.getSource()).getText().equals(VentanaPrincipal.MNTM_SERGYM)) {
                vPrincipal.cargaPanel(pFoto, "img/gym.jpg");
vPrincipal.getpFoto().addMouseListener(new MouseListener() {
                    
                    @Override
                    public void mouseReleased(MouseEvent e) {
                    }
                    
                    @Override
                    public void mousePressed(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseExited(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseEntered(MouseEvent e) {
                    }
                    
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        if(e.getSource().equals(vPrincipal.getpFoto())) {
                            vPrincipal.getpFoto().getTxtAreaDescripcion().setText("El gimnasio del hotel Loving Java pone a su disposicion todas sus maquinas de ultima generacion:\r\n" + 
                                    "Cintas de correr\r\n" + 
                                    "Elipticas\r\n" + 
                                    "Bicicletas estaticas\r\n" + 
                                    "Piscina climatizada\r\n" + 
                                    "Maquinas de musculacion.");
                        }
                        
                    }
                });
                Image Img = null;
                try {
                    Img = ImageIO.read(new File("img/gym.jpg"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //ImageIcon ImgIcon = new ImageIcon(Img);
                pFoto.setImg(Img);
                
            }else if (((JMenuItem) ae.getSource()).getText().equals(VentanaPrincipal.MNTM_SALIR)) {
                vPrincipal.confirmarSalida();
            }
        }else if (ae.getSource() instanceof JButton) {
        }else if (ae.getSource() instanceof JTextField) {
            if(ae.getSource().equals(vPrincipal.getTxtNombre())) {
                if(vPrincipal.getTxtNombre().getText().length()<3) {
                    vPrincipal.getTxtNombre().setBackground(Color.RED);
                }
                
            }else if (ae.getSource().equals(vPrincipal.getTxtApellidos())) {
                if(vPrincipal.getTxtApellidos().getText().length()<3) {
                    vPrincipal.getTxtApellidos().setBackground(Color.RED);
                }
            }
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        if(e.getSource().equals(pFoto)) {
            pFoto.getTxtAreaDescripcion().setVisible(true);
            pFoto.getTxtAreaDescripcion().setEnabled(true);
            pFoto.getTxtAreaDescripcion().setEditable(false);
            pFoto.getTxtAreaDescripcion().setText("Descripcion prueba.");
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }
}

IniciarApp

public class InciarApp {

    public static void main(String[] args) {
        
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                VentanaPrincipal vPrincipal = new VentanaPrincipal();
                PanelFoto pFoto = vPrincipal.getpFoto();
                Controlador controlador = new Controlador(vPrincipal, pFoto);
                vPrincipal.setControlador(controlador);
                pFoto.setControlador(controlador);
                vPrincipal.hacerVisible();
            }
        });
    }
}

I hope you can help, and in that case take thanks for advance!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Manuel Lucas
  • 636
  • 6
  • 17
  • You know what will save you a lot of trouble, is to create a different action listener for each of your menu items instead of delegating based on the the text. – matt Jan 28 '21 at 15:39
  • 2
    You shouldn't be using things like "setSize" and "setBounds" your layout manager should handle that for you. Maybe you should reduce this down to a worker example with just a JFrame/JScrollPane and JPanel with image and description. That will be much easier for somebody to help you. – matt Jan 28 '21 at 15:43
  • 3
    The code you posted is not a [mcve]. Since you are asking for help in debugging your code, I can only do that if I can reproduce your problem and I can't, based on the code in your question. – Abra Jan 28 '21 at 16:18
  • 1) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. 2) But if that advice fails to solve the problem for you.. When preparing the MRE suggested by @Abra, note that one way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). .. – Andrew Thompson Jan 28 '21 at 18:12
  • .. E.G. The code in [this answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). – Andrew Thompson Jan 28 '21 at 18:12
  • Forgot to mention before, but in case it is not clear, an MRE would try to load a *single* image successfully, **not *seven.*** Once you've figured out the problem with loading one image - the solution for the other 6 should be obvious. – Andrew Thompson Jan 28 '21 at 18:59

0 Answers0