Comment puis-je contrôler ce qui se passe avec la fenêtre après avoir cliqué sur les boutons JOPtionPane? J'essaye d'implémenter le sélecteur simple de dossier. Dans mon cadre j'ai 3 boutons (OK, Annuler, Parcourir). Le bouton Parcourir ouvre la fenêtre de recherche de fichiers et, après avoir sélectionné les fichiers, doit revenir au cadre principal. Cliquer sur OK ouvrira un cadre avec le contenu du fichier. Maintenant, le porblem a l'air de cette façon. Avec le code ci-dessous, je peux choisir le fichier, mais directement après qu'un nouveau cadre est créé, et mon cadre avec boutons dissapears:
alt text http://img20.imageshack.us/img20/7614/windowh.png
alt text http://img267.imageshack.us/img267/1953/emptywindow.pngComment fonctionne JOptionPane
import java.io.File;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.awt.*;
import javax.swing.*;
import java.io.*;
public class Main {
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
show("Window");
}
});
}
public static void show(String frame_name){
JFrame frame = new JFrame(frame_name);
frame.setPreferredSize(new Dimension(450, 300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel top = new JPanel();
top.setLayout(new BoxLayout(top, BoxLayout.Y_AXIS));
JFileChooser fc = new JFileChooser(new File("."));
JPanel creator = new JPanel();
creator.setLayout(new BoxLayout(creator, BoxLayout.Y_AXIS));
creator.add(top);
String[] buttons = {"OK", "Cancel", "Browse"};
int rc = JOptionPane.showOptionDialog(
null,
creator,
frame_name,
JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
buttons,
buttons[0]
);
String approveButt = "";
switch(rc){
case 0:
break;
case 1:
break;
case 2:
approveButt = buttons[rc];
int retVal = fc.showDialog(null, approveButt);
if (retVal == JFileChooser.APPROVE_OPTION)
System.out.println(approveButt + " " + fc.getSelectedFile());
break;
}
frame.pack();
frame.setVisible(true);
}
}
Avec le second code je peux revenir à mon menu, mais en aucun cas je suis capable de faire apparaître ce nouveau cadre, qui est apparu avec le premier code. Comment contrôler cela? Qu'est-ce que je rate ?
public class Main {
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
show("Window");
}
});
}
public static void show(String frame_name){
JFrame frame = new JFrame(frame_name);
frame.setPreferredSize(new Dimension(450, 300));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel top = new JPanel();
top.setLayout(new BoxLayout(top, BoxLayout.Y_AXIS));
JFileChooser fc = new JFileChooser(new File("."));
JPanel creator = new JPanel();
creator.setLayout(new BoxLayout(creator, BoxLayout.Y_AXIS));
creator.add(top);
String[] buttons = {"OK", "Cancel", "Browse"};
String approveButt = "";
Plane m = null;
int rc = -1;
while (rc != 0) {
rc = JOptionPane.showOptionDialog(
null,
creator,
frame_name,
JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
buttons,
buttons[0]
);
switch (rc) {
case 0:
m = new Plane();
case 1:
System.exit(0);
case 2:
approveButt = buttons[rc];
int retVal = fc.showDialog(null, approveButt);
if (retVal == JFileChooser.APPROVE_OPTION)
System.out.println(approveButt + " " + fc.getSelectedFile());
break;
default:
break;
}
}
addComponents(frame.getContentPane(), m);
frame.pack();
frame.setVisible(true);
}
private static void addComponents(Container c, Plane e) {
c.setLayout(new BoxLayout(c, BoxLayout.Y_AXIS));
c.add(e);
}
}
class Plane extends JPanel {
public Plane(){
}
@Override
public void paint(Graphics g){
g.setColor(Color.BLUE);
g.fillRect(0, 0, 400, 250);
}
}