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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
public class DynamicFormExample extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private List<ComponentHandler> textFields = new ArrayList<>(); // une liste pour retrouver ses champs
public DynamicFormExample() {
super(new BorderLayout());
// création des composants statiques
JPanel buttonPanel = new JPanel(new BorderLayout()); // un panel pour le champ de saisie du nombre et le bouton +
JSpinner spinner = new JSpinner(new SpinnerNumberModel(1,0,10000,10)); // un champ pour saisir le nombre de champs à ajouter
JButton buttonAdd = new JButton("+");
JButton buttondelete = new JButton("-");
JPanel pane= new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
JLabel label = new JLabel("Name:"); // une étiquette pour le champ
c.gridx = 0;
c.weightx = 0;
c.fill = GridBagConstraints.NONE;
pane.add(label, c);
JTextField textField = new JTextField(""); // le champ
c.gridx = 1;
c.weightx = 1;
c.fill = GridBagConstraints.HORIZONTAL;
pane.add(textField, c);
JLabel labelt = new JLabel("Type:"); // une étiquette pour le champ
c.gridx = 0;
c.weightx = 0;
c.fill = GridBagConstraints.NONE;
pane.add(labelt, c);
JTextField textFieldt = new JTextField(""); // le champ
c.gridx = 1;
c.weightx = 1;
c.fill = GridBagConstraints.HORIZONTAL;
pane.add(textFieldt, c);
buttonPanel.add(spinner, BorderLayout.CENTER);
JPanel pane1 = new JPanel();
pane1.add(buttonAdd);
pane1.add(buttondelete);
buttonPanel.add(pane1, BorderLayout.EAST);
buttonPanel.add(pane, BorderLayout.NORTH);
add(buttonPanel, BorderLayout.NORTH);
JPanel mainPanel = new JPanel(new GridBagLayout()); // un panel pour ajouter les champs dynamiquement
JPanel containerPanel = new JPanel(new BorderLayout()); // on a besoin d'un panel intermédiaire pour que les champs soient plaqués en haut de la fenêtre (le + simple pour le faire)
containerPanel.add(mainPanel, BorderLayout.NORTH);
add(new JScrollPane(containerPanel), BorderLayout.CENTER); // on encapsule le panel des champs dans un scrollpane pour qu'on puisse accèder aux champs qui sont dans la partie non visible de la fenêtre
JPanel p= new JPanel(new BorderLayout());
ImageIcon img= new ImageIcon("images/notification.png");
JButton b1= new JButton("Commit");
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e )
{
}});
ImageIcon imgprev= new ImageIcon("images/previous.png");
JButton b2= new JButton("Cancel");
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}});
JPanel panell= new JPanel();
panell.add(b1);
panell.add(b2);
p.add(panell,BorderLayout.CENTER);
JButton butt= new JButton(imgprev);
butt.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e)
{
Algorithms al= new Algorithms();
al.visibl1();
//jif.dispose();
}
});
butt.setBorderPainted(false);
butt.setFocusPainted(false);
butt.setContentAreaFilled(false);
JButton buttt= new JButton(img);
buttt.setBorderPainted(false);
buttt.setFocusPainted(false);
buttt.setContentAreaFilled(false);
JPanel pan= new JPanel();
pan.add(butt);
pan.add(buttt);
p.add(pan,BorderLayout.WEST);
add(p,BorderLayout.SOUTH);
buttonAdd.addActionListener(e-> addButtons(mainPanel, (Integer)spinner.getValue()));
buttondelete.addActionListener(e-> removeButtons(mainPanel, (Integer)spinner.getValue()));
}
private void addButtons(JPanel panel, int number) {
if ( number>0 ) {
//int count = panel.getComponentCount()/2; // on compte le nombre de composants déjà ajoutés
int count = textFields.size();
GridBagConstraints gbc = new GridBagConstraints(0, GridBagConstraints.RELATIVE, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(2,4,2,4), 0, 0);
for(int i=0; i<number; i++) {
JLabel label = new JLabel(getLabelText(count+i)); // une étiquette pour le champ
gbc.gridx = 0;
gbc.weightx = 0;
gbc.fill = GridBagConstraints.NONE;
panel.add(label, gbc);
JTextField textField = new JTextField("parameter"); // le champ
//textField.setPreferredSize( new Dimension( 500, 24 ) );
JButton btn = new JButton("help");
JPanel pane= new JPanel(new BorderLayout());
pane.add(textField);
pane.add(btn, BorderLayout.EAST);
gbc.gridx = 1;
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(pane, gbc);
textFields.add(new ComponentHandler(pane, label));
btn.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
new Thread() {
public void run() {
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec(new String[] {"C:\\Program Files (x86)\\Windows NT\\Accessories\\wordpad.exe"} );
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*try {
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
try {
while((line = reader.readLine()) != null) {
// Traitement du flux de sortie de l'application si besoin est
}
} finally {
reader.close();
}
} catch(IOException ioe) {
ioe.printStackTrace();
}*/
}
}.start();
}
});
}
// on a modifié l'UI alors qu'elle était déjà affichée : on revalide (pour le layout) et on repeint (pour l'affichage)
panel.revalidate();
panel.repaint();
}
}
private void removeButtons(JPanel panel, int x) {
if ( x>textFields.size() ) {
x=textFields.size();
}
List<ComponentHandler> listeTextFieldASupprimer = textFields.subList(textFields.size() - x, textFields.size()); // on obtient une liste des éléments à supprimer
System.out.println("avant"+textFields.size());
for(ComponentHandler textFieldASupprimer : listeTextFieldASupprimer ){ // on les parcourt
// donc là on supprime
panel.remove(textFieldASupprimer.pane);
panel.remove(textFieldASupprimer.label);
}
// il reste à supprimer les éléments de la liste textfields
listeTextFieldASupprimer.clear();
System.out.println("aprés"+textFields.size());
panel.revalidate();
panel.repaint();
}
/**
* Cette méthode permet de récupérer toutes les valeurs saisies dans chaque champ (dans l'ordre)
* @return
*/
//public List<String> getTexts() {
//return Collections.unmodifiableList(textFields.stream().map(f->f.getText()).collect(Collectors.toList()));
//}
/**
* méthode pour déterminer le nom du champ d'index index
* @param index
* @return
*/
protected String getLabelText(int index) {
return "Param " + index;
}
} |
Partager