salve, sono nuovo nel mondo del Java, ho problemi con il richiamare un oggetto all'esterno del metodo in cui è stato creato. forse non mi sono spiegato bene XD, quindi ecco il codice:
il problema è che istanzio l'oggetto "a" di tipo DatiFile dentro il JFileChooser salvando i dati del file, ma quando vado a richiamarlo fuori per stampare nome e path nella label non mi riconosce l'oggetto...dove sbaglio?codice:import java.awt.BorderLayout; public class frame1 extends JFrame { private JPanel contentPane; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { frame1 frame = new frame1(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public frame1() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JButton Selezione = new JButton("Seleziona File"); Selezione.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent arg0) { JFileChooser scelta= new JFileChooser(); FileFilter documentoWord = new FileNameExtensionFilter("DOC file", "doc"); FileFilter documentoPdf = new FileNameExtensionFilter("PDF file", "pdf"); FileFilter documentoOdt = new FileNameExtensionFilter("ODT file", "odt"); scelta.addChoosableFileFilter(documentoPdf); scelta.addChoosableFileFilter(documentoOdt); scelta.setFileFilter(documentoWord); int sel=scelta.showDialog(null, "Scegli File.."); if(sel==JFileChooser.APPROVE_OPTION){ File scelto = scelta.getSelectedFile(); String FileN=scelto.getName(); String FileP=scelto.getPath(); DatiFile a =new DatiFile(); a.setDati(FileN, FileP); } } }); Selezione.setBounds(303, 23, 105, 23); contentPane.add(Selezione); JLabel stringaPath = new JLabel("Nessun file selezionato"); stringaPath.setForeground(new Color(0, 0, 0)); stringaPath.setBackground(Color.LIGHT_GRAY); stringaPath.setBounds(33, 22, 234, 24); contentPane.add(stringaPath); if ((a.getNome()==null)||(a.getPath==null)) stringaPath.setText("Nessun File Selezionato"); else stringaPath.setText(a.getNome + " " + a.getPath); } public class DatiFile{ public String NomeFile; public String PathFile; public String getNome(){ return NomeFile; } public String getPath(){ return PathFile; } public void setDati(String Nome, String Path) { this.NomeFile= Nome; this.PathFile= Path; } } }

Rispondi quotando