Essaie d'ajouter un seul bouton:
1 2 3 4 5 6 7
| //Déclaration du buton
nomButton = new JButton("Open");
//Ajoute du bouton
this.add(nomButton);
//Ajoute evenement
nomButton.addActionListener(this);
nomButton.addPropertyChangeListener(this); |
Pour le cas où tu veux faire une sauvegarde, tu change le texte du bouton:
nomButton.setText("Save");
(La logique qui détermine le nom correct du bouton ("Open" où "Save") est encore à développer.)
Dans "actionPerformed" il faut comparer les chaînes par "equals" (pas par ==):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public void actionPerformed(ActionEvent evt) {
Object source = evt.getSource();
if (source == nomButton) {
String nom = nomButton.getText();
if (nom.equals("Open")) {
returnVal = choixFichier.showOpenDialog(Composant_graph.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = choixFichier.getSelectedFile();
fileinField.setText(file.getPath());
}
} else if (nom.equals("Save")) {
returnVal = choixFichier.showSaveDialog(Composant_graph.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = choixFichier.getSelectedFile();
if (!file.getName().endsWith(".xls")) {
file = new File(file.getParentFile(), file.getName() + ".xls");
}
fileinField.setText(file.getPath());
}
}
}
} |
Partager