Witam. Mam problem z dodaniem do swojego programu prostej opcji. Chciałbym żeby to wyglądało mniej więcej tak: Tworzę okienko w JFrame, i po naciśnięciu przycisku pobierz plik, pojawia się opcja wyboru pliku, który ma potem trafić do programu. Zrobiłem to tak:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import MyFrame6.RownanieGUI;
import edu.duke.FileResource;
public class VigenereGUI extends JFrame implements ActionListener {
private JLabel lSource;
private JButton bGetFile, bDecipher;
private JTextField tOutput;
public VigenereGUI()
{
setSize(600,400);
setTitle("Szyfr Wiginera - kodowanie i łamanie");
setLayout(null);
lSource = new JLabel("Podaj źródło:");
lSource.setBounds(10,30,100,20);
add(lSource);
bGetFile = new JButton("Pobierz plik");
bGetFile.setBounds(10,60,120,20);
add(bGetFile);
bGetFile.addActionListener(this);
bDecipher = new JButton("Rozszyfruj");
bDecipher.setBounds(150,60,120,20);
add(bDecipher);
bGetFile.addActionListener(this);
tOutput = new JTextField("");
tOutput.setBounds(10,90,400,250);
add(tOutput);
}
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if(source==bGetFile)
{
FileResource fr = new FileResource();
}
}
public static void main(String[]args)
{
VigenereGUI app = new VigenereGUI();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setVisible(true);
}
}
Po naciśnięciu "pobierz plik" wywala mi błędy, które zaczynają się tak:
Exception in thread "AWT-EventQueue-0" java.lang.Error: Cannot call invokeAndWait from the event dispatcher thread at java.awt.EventQueue.invokeAndWait(EventQueue.java:1331) at java.awt.EventQueue.invokeAndWait(EventQueue.java:1324) at javax.swing.SwingUtilities.invokeAndWait(SwingUtilities.java:1353) at edu.duke.FileSelector.selectFiles(FileSelector.java:89)
Ktoś mi może podpowiedzieć o co chodzi? Jak wywołuję FireResource w klasie main, to wszystko działa jak powinno. Może z resztą jest jakiś inny lepszy sposób na wybór pliku.