import java.awt.EventQueue; import java.awt.FlowLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.border.EmptyBorder; public class simplefenetre extends JFrame { private JPanel contentPane; private static JTextField textField; private static JTextField textField_1; private static JTextField textField_2; private static JTextField textField_3; private JTextField textField_4; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { simplefenetre frame = new simplefenetre(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /* Le constructeur */ public simplefenetre() { setTitle("Traiter vos fichiers"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 450, 300); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(new GridBagLayout()); // Button "OK" JButton btnOk = new JButton("OK"); btnOk.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { // On lit les 5 champs String zoneB = textField.getText(); String zoneE = textField_1.getText(); String valueB = textField_2.getText(); String valueE = textField_3.getText(); String filename = textField_4.getText(); // On appelle la méthode de l'autre classe new Finally().traiterFichier(zoneB, zoneE, valueB, valueE, filename); } }); textField_4 = new JTextField(); contentPane.add(textField_4, new GridBagConstraints(0,0,2,1,1,0,GridBagConstraints.BASELINE_TRAILING, GridBagConstraints.HORIZONTAL, new Insets(2,2,2,2), 0, 0)); // Button "PARCOURIR" JButton btnParcourir = new JButton("PARCOURIR"); btnParcourir.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { browseFile(); // sert au bouton PARCOURIR d'aller chercher un fichier x } }); // On créer les composants de saisies . JLabel zoneBLabel = new JLabel("ZONE_B"); contentPane.add(zoneBLabel, new GridBagConstraints(0,1,1,1,0,0,GridBagConstraints.BASELINE_TRAILING, GridBagConstraints.NONE, new Insets(2,2,2,2), 0, 0)); JLabel zoneELabel = new JLabel("ZONE_E"); contentPane.add(zoneELabel, new GridBagConstraints(0,2,1,1,0,0,GridBagConstraints.BASELINE_TRAILING, GridBagConstraints.NONE, new Insets(2,2,2,2), 0, 0)); JLabel valueBLabel = new JLabel("VALUE_B"); contentPane.add(valueBLabel, new GridBagConstraints(0,3,1,1,0,0,GridBagConstraints.BASELINE_TRAILING, GridBagConstraints.NONE, new Insets(2,2,2,2), 0, 0)); JLabel valueELabel = new JLabel("VALUE_E"); contentPane.add(valueELabel, new GridBagConstraints(0,4,1,1,0,0,GridBagConstraints.BASELINE_TRAILING, GridBagConstraints.NONE, new Insets(2,2,2,2), 0, 0)); textField = new JTextField(Finally.ZONE_B); contentPane.add(textField, new GridBagConstraints(1,1,2,1,1,0,GridBagConstraints.BASELINE_LEADING, GridBagConstraints.HORIZONTAL, new Insets(2,2,2,2), 0, 0)); textField_1 = new JTextField(Finally.ZONE_E); contentPane.add(textField_1, new GridBagConstraints(1,2,2,1,1,0,GridBagConstraints.BASELINE_LEADING, GridBagConstraints.HORIZONTAL, new Insets(2,2,2,2), 0, 0)); textField_2 = new JTextField(Finally.VALUE_B); contentPane.add(textField_2, new GridBagConstraints(1,3,2,1,1,0,GridBagConstraints.BASELINE_LEADING, GridBagConstraints.HORIZONTAL, new Insets(2,2,2,2), 0, 0)); textField_3 = new JTextField(Finally.VALUE_E); contentPane.add(textField_3, new GridBagConstraints(1,4,2,1,1,0,GridBagConstraints.BASELINE_LEADING, GridBagConstraints.HORIZONTAL, new Insets(2,2,2,2), 0, 0)); JPanel buttonPane = new JPanel(new FlowLayout(FlowLayout.TRAILING)); contentPane.add(buttonPane, new GridBagConstraints(0, 5, 3, 1, 1, 1, GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(2,2,2,2), 0, 0)); buttonPane.add(btnOk); contentPane.add(btnParcourir, new GridBagConstraints(2,0,1,1,0,0,GridBagConstraints.BASELINE_LEADING, GridBagConstraints.NONE, new Insets(2,2,2,2),0,0)); // Button "ANNULER" JButton btnAnnuler = new JButton("ANNULER"); btnAnnuler.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); buttonPane.add(btnAnnuler); } private void browseFile() { JFileChooser fileChooser = new JFileChooser(); fileChooser.setDialogTitle("Choisir le fichier..."); fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); fileChooser.setMultiSelectionEnabled(false); if ( !textField_4.getText().isEmpty() ) { // faire bien attention : bien faire correspondre notre textField choisi, ici le textField_4 fileChooser.setSelectedFile(new File(textField.getText())); } // il y a plein d'autres méthodes pour customiser le composant (filtre sur les fichiers par exemple) : voir doc. if ( fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION ) { textField_4.setText( fileChooser.getSelectedFile().getAbsolutePath().toString() ); } } }