Salve,
dovrei realizzare un'applicazione che utilizza un jfilechooser, ma vorrei visualizzare l'anteprima del file che sto aprendo.
Ho scritto in un Jpanel che implementa PropertyChangeListener questo
ovviamente l'ho trovato in rete, ho fatto un semplice copia e incollacodice:public void propertyChange(PropertyChangeEvent evt) { String propertyName = evt.getPropertyName(); // Make sure we are responding to the right event. if (propertyName.equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) { File selection = (File) evt.getNewValue(); String name; if (selection == null) { return; } else { name = selection.getAbsolutePath(); } /* * Make reasonably sure we have an image format that AWT can * handle so we don't try to draw something silly. */ if ((name != null) && name.toLowerCase().endsWith(".jpg") || name.toLowerCase().endsWith(".jpeg") || name.toLowerCase().endsWith(".gif") || name.toLowerCase().endsWith(".png")) { icon = new ImageIcon(name); image = icon.getImage(); scaleImage(); repaint(); } } }
Mentre nell'azione ho scritto questo
codice:private String acquisisciFile() { JFileChooser fileChooser = this.controllo.getVista().getJfileChooser(); PannelloMiniatura pMiniatura = new PannelloMiniatura(this.controllo.getVista()); pMiniatura.getLabelCarica().setHorizontalTextPosition(SwingConstants.CENTER); pMiniatura.add(pMiniatura.getLabelCarica(), BorderLayout.CENTER); fileChooser.setAccessory(pMiniatura); fileChooser.addPropertyChangeListener(pMiniatura); fileChooser.setApproveButtonText("Apri Foto"); fileChooser.setDialogTitle("Selezionare un file"); int codice = fileChooser.showOpenDialog(this.controllo.getVista()); if (codice == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); percorso = file.getPath(); return percorso; } else if (codice == JFileChooser.CANCEL_OPTION) { logger.info("Comando apri annullato"); } return null; } public void actionPerformed(ActionEvent e) { PannelloMiniatura pannelloM = (PannelloMiniatura) this.controllo.getVista().getSottoVista(Costanti.PANNELLO_MINIATURA); String nomeFile = acquisisciFile(); if (nomeFile != null) { icon = new ImageIcon(nomeFile); pannelloM.getLabelCarica().setIcon(icon); }
Solo che non mi apre l'anteprima... dove sbaglio? qualcuno pò darmi suggerimenti?
Grazie

Rispondi quotando