Code:
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Test extends JFrame{
//polja podataka
JButton dugme = new JButton("Pokazi FileChooser");
JFileChooser fileChooser = new JFileChooser();
public Test() {
super("Ovo dugme stavis na panel gde ti treba da pozivas FileChooser");
Container conPane = getContentPane();
conPane.setLayout(new FlowLayout());
conPane.add(dugme);
dugme.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int stanje = fileChooser.showOpenDialog(null);
//ili showSaveDialog ako ti treba za snimanje
File fajl = fileChooser.getSelectedFile();
if(fajl != null && stanje == JFileChooser.APPROVE_OPTION) {
JOptionPane.showMessageDialog(null, fajl.getPath());
//umesto ovoga ubacis deo gde radis sa fajlom
}
else if(stanje == JFileChooser.CANCEL_OPTION) {
JOptionPane.showMessageDialog(null, "Otkazano!");
//ili ubacujes sta ti vec treba
}
}
});
}
public static void main(String args[]) {
JFrame frejm = new Test();
frejm.setBounds(300, 300, 300,100);
frejm.setVisible(true);
frejm.setDefaultCloseOperation(EXIT_ON_CLOSE);
frejm.addWindowListener(new WindowAdapter() {
public void windowClosed(WindowEvent e) {
System.exit(0);
}
});
}
}
Ako sam dobro skapirao samo ovo ti je trebalo?
I've seen things you people wouldn't believe. Attack ships on fire off the shoulder of Orion. I watched C-beams glitter in the dark near the Tannhauser gate. All those moments will be lost in time, like tears in rain. Time to die.