I'm beginning with GUI and listeners. I want to choose and set file from pc into the "File zvolenysubor" in class Hlavna by another class implementing ActionListener. I can choose file in the listener's actionPerformed method and set it into "File subor" but I fail in saving it into the "File zvolenysubor" in my main (and all of my ideas how to do it failed too). What should I change/add there please?
Here are my classes:
public class Hlavna {
public static void main(String[] args) {
File zvolenysubor = null;
JFrame frame = new JFrame("ABCDE");
JButton vybersuboru = new JButton("vyber");
vybersuboru.setBounds(220, 15, 200, 20);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(640, 480);
frame.add(vybersuboru);
frame.setLayout(null);
VyberListener list1 = new VyberListener(zvolenysubor);
vybersuboru.addActionListener(list1);
vybersuboru.setText("vyber subor");
}
}
public class VyberListener implements ActionListener {
private File subor;
public VyberListener(File subor){
this.subor = subor;
}
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
System.out.println("lol");
FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
chooser.showOpenDialog(null);
subor = chooser.getSelectedFile();
System.out.println(subor.getAbsolutePath());
}
}