Bonjour tous le monde,
Je cherche moyenne pour insérer une image dans une application Java à partir d'un dossier précis.(parcourir mon DD) et merci pour les réponses par avance :)
Version imprimable
Bonjour tous le monde,
Je cherche moyenne pour insérer une image dans une application Java à partir d'un dossier précis.(parcourir mon DD) et merci pour les réponses par avance :)
Utilise un JFileChooser pour parcourir ton DD.
Récupère le chemin vers l'image choisi et exploite là (image de fond d'un JPanel par exemple)
comme déja dit, tu peux utiliser les JFileChooser :
Code:
1
2
3
4
5
6
7
8
9
10
11 JFileChooser choix = new JFileChooser(); int retour=choix.showOpenDialog(parent); if(retour==JFileChooser.APPROVE_OPTION){ // un fichier a été choisi ( sortie par OK) // nom du fichier choisi choix.getSelectedFile().getName()); // chemin absolu du fichier choisi choix.getSelectedFile(). getAbsolutePath(); }else ... ;// pas de fichier choisi
Bonjour,
voila j'ai ajouter un bouton et une jpanel a mon interface ,mais le probleme que j'ai c'est quand je click sur le bouton et je navigue sur mon disque dure et je choisie l'image le jpanel ne m'affiche pas cette image comme fond,voila mon code du bouton :
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { int ok = choix.showOpenDialog(null); if(ok==JFileChooser.APPROVE_OPTION){ choix.getSelectedFile().getName(); icon = new ImageIcon(choix.getSelectedFile().getAbsolutePath()); img = new JLabel(icon); jPanel1.add(img); jPanel1.setVisible(true); } else System.out.println("pas image !!"); }
Code:
1
2 jPanel1.repaint();
oû je doit mettre ce code jPanel1.repaint(); ??
slim_java: rien ne sert d'appeler directement repaint() il me semble. En l'occurence, je pencehrais plutot sur le fait que le jpanel1 n'est pas présent dans un conteneur quelconque.
la method repaint ne fonction pas :(
comme a dis tchize,peut être que tu n'a pas ajouter ton panneau au conteneur(en rouge) :
Code:
1
2
3 jPanel1.add(image jPanel1.repaint(); tonconteneur. add(jPanel1);
toujours pas d'affichage d'image sur mon jpanel1 :voila mon code de jframe:
Code:
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 package favico; import java.awt.Container; import javax.swing.ImageIcon; import javax.swing.JFileChooser; import javax.swing.JLabel; public class NewJFrame2 extends javax.swing.JFrame { JFileChooser choix = new JFileChooser(); public String a; private Container c; private ImageIcon icon; public JLabel img; private void initialize(final String filePath) { c = getContentPane(); icon = new ImageIcon(filePath); img = new JLabel(icon); jPanel1.add(img); jPanel1.repaint(); c.add(jPanel1); } private String Exemple(){ int ok = choix.showOpenDialog(null); if(ok==JFileChooser.APPROVE_OPTION) { choix.getSelectedFile().getName(); a=choix.getSelectedFile().getAbsolutePath(); return a; } else return null; } public NewJFrame2() { initComponents(); } @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jPanel1 = new javax.swing.JPanel(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jPanel1.setBackground(new java.awt.Color(153, 0, 153)); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 100, Short.MAX_VALUE) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 100, Short.MAX_VALUE) ); jButton1.setText("jButton1"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addGap(50, 50, 50) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 143, Short.MAX_VALUE) .addComponent(jButton1) .addGap(34, 34, 34)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(82, 82, 82) .addComponent(jButton1)) .addGroup(layout.createSequentialGroup() .addGap(48, 48, 48) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(152, Short.MAX_VALUE)) ); pack(); }// </editor-fold> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { String chaine=Exemple(); initialize(chaine); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new NewJFrame2().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JPanel jPanel1; // End of variables declaration }
salut. tu as pas donner une dimension a ton Jlabel() avec setBounds :
Code:
1
2
3
4
5
6
7
8 private void initialize(final String filePath) { icon = new ImageIcon(filePath); img = new JLabel(icon); img.setBounds(0, 0, jPanel1.getWidth(), jPanel1.getHeight()); jPanel1.add(img); jPanel1.repaint(); }
Merci ca ma aidée t'a réponse mais le problème que j'ai maintenant c quand je click une deuxième fois sur le bouton pour changer l'image ca ne change pas sur la jpanel, l'ancien reste toujours a l'affiche,pourquoi ?
rien à voir slim_java. setBounds est utilisé par le layoutmanager, on appelle pas setBounds sois même ça ne sert à rien. De même l'appel à repaint() ne sert à rien.
En l'occurence ici, comme on ajoute un composant à JPanel1, il faut maintenant lui indiquer qu'on a fini d'en ajouter pour le moment et qu'il peut lancer ses calculs de layout, via un appel à jpanel1.validate()
Enfin, le add(jPanel1) est aussi erroné car il a déjà du être placé lors de l'initialisation des grouplayout.Citation:
validate
public void validate()
Validates this container and all of its subcomponents.
The validate method is used to cause a container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.
ce qui donne au final:
Code:
1
2
3
4
5
6 private void initialize(final String filePath) { icon = new ImageIcon(filePath); img = new JLabel(icon); jPanel1.add(img); jPanel1.validate(); }
si tu va ajouter plusieurs images.... et peut être du traitement.....vaudrait mieux oublier les JLabel et penser à rédéfinier la méthode paintComponent de ton panneau :
exemple :
Code:
1
2
3
4
5
6
7
8 public class MonPanel extends JPanel{ private Image bg; public void paintComponent(Graphics g) { g.drawImage(bg,0,0,null); } }
tchize ,ton code ne fonction pas , je me suis arrive au premier problème: le jpanel1 n'affiche plus l'image.
si j'ai bien comprit,tu me demande de changer mon jpanel1 par une autre panel(MonPanel),et dedans je redéfini la méthode paintComponent ?
ca on ne peux pas le savoir. Le JPanel prendra un taille qui est fonction du layoutmanager + de la taille de l'image dans le JLabel! Il va donc a priori passer de 0pixels preferred à XXX pixels.
Pour ce qui est de faire le changement plusieurs fois, il suffit de changer le JLabel:
Code:
1
2
3
4
5
6
7 private void initialize(final String filePath) { icon = new ImageIcon(filePath); img = new JLabel(icon); jPanel1.removeAll(); jPanel1.add(img); jPanel1.validate(); }
Commente ces lignes là, sans quoi il va ignorer le contenu de JPanel1. Tu y défini un groupLayout que ne prend aucun compte de la présence de l'image (-> poubelle). Le Layout par défaut du JPanel suffit amplement.
Code:
1
2
3
4
5
6
7
8
9
10 javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 100, Short.MAX_VALUE) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 100, Short.MAX_VALUE) );