1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
| 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 java.util.Collection;
import java.util.Vector;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
public class simplefenetre extends JFrame {
private static final String PATH = null;
private JPanel contentPane;
private static JTextField textField;
private static JTextField textField_1;
private static JTextField textField_2;
private static JTextField textField_3;
private static String ZONE_B;
private static String ZONE_E;
private static String VALUE_B;
private static String VALUE_E;
private JTextField textField_4;
private AbstractButton btnOk;
public static void main(String[] args) {
String path;
if ( args.length>0 ) {
path = args[0];
}
else {
path = PATH;
}
Collection<String> mots = new Finally().traiterFichier(ZONE_B, ZONE_E, VALUE_B, VALUE_E, path);
if (mots != null) {
for (String mot : mots) {
System.out.println(mot);
}
} else {
System.out.println("Aucune chaine trouvée comprise entre " + ZONE_B
+ " et " + ZONE_E);
}
}
/* 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"
btnOk.addActionListener(new ActionListener() {
private AbstractButton zoneB;
private AbstractButton zoneE;
private AbstractButton valueB;
private AbstractButton valueE;
public void actionPerformed(ActionEvent arg0) {
// On lit les 5 champs
String ZONE_B = zoneB.getText();
String ZONE_E = zoneE.getText();
String VALUE_B = valueB.getText();
String VALUE_E = valueE.getText();
String PATH = textField_4.getText();
// On appelle la méthode de l'autre classe
Collection<String> mots = new Finally().traiterFichier(ZONE_B, ZONE_E, VALUE_B, VALUE_E, PATH);
if ( mots!=null ) {
Collection<? extends String> collection = null;
JOptionPane.showMessageDialog(null, new JScrollPane(new JList<String>(new Vector<String>(collection))));
}
else {
JOptionPane.showMessageDialog(null, "Rien trouvé...");
}
}
});
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() );
}
}
} |
Partager