cs_aurel_kb
Messages postés17Date d'inscriptionsamedi 25 novembre 2006StatutMembreDernière intervention27 décembre 2012
-
17 déc. 2012 à 23:00
cs_Julien39
Messages postés6414Date d'inscriptionmardi 8 mars 2005StatutModérateurDernière intervention29 juillet 2020
-
18 déc. 2012 à 10:49
Bonjour,
j'ai un bouton parcourir sur ma fenêtre java, ainsi qu'un Jpanel afin d'aficher l'image obtenu grace au bouton. j'affiche la boite de dialogue de parcour, recupere bien le chemin du fichier image, mais j'arrive pas à l'afficher sur mon panel.
btnParcourir = new JButton("Parcourir...");
btnParcourir.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//Create a file chooser
final JFileChooser fc = new JFileChooser();
//int returnVal = fc.showOpenDialog(btnParcourir);
int returnVal = fc.showDialog(btnParcourir,"Attach");
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
String chemin = fc.getSelectedFile().getAbsolutePath().toString();
System.out.println(chemin);
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
fich window = new fich();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public fich() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
//@SuppressWarnings("deprecation")
private void initialize() {
frame = new JFrame();
//Définit un titre pour notre fenêtre
frame.setTitle("Ajout d'un bien immobilier");
//Définit sa taille : 400 pixels de large et 100 pixels de haut
frame.setBounds(100, 100, 553, 350);
//Nous demandons maintenant à notre objet de se positionner au centre
frame.setLocationRelativeTo(null);
//Termine le processus lorsqu'on clique sur la croix rouge
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//Garder la fenêtre en premier plan
frame.setAlwaysOnTop(true);
//Ne pas redimentionner la fenêtre
frame.setResizable(false);
//design du panel pan global
pan = new JPanel();
pan.setBackground(Color.GRAY);
pan.setLayout(null);
//design du panel top
JPanel ptop = new JPanel();
//ptop.setBackground(Color.LIGHT_GRAY);
ptop.setBounds(5, 6, 539, 262);
pan.add(ptop);
ptop.setLayout(null);
//Création des champs du formulaire
JLabel lblRef = new JLabel("Référence :");
lblRef.setBounds(8, 17, 75, 14);
ptop.add(lblRef);
TxtRef = new JTextField();
TxtRef.setBounds(170, 11, 86, 20);
ptop.add(TxtRef);
TxtNom = new JTextField();
TxtNom.setBounds(170, 41, 123, 20);
ptop.add(TxtNom);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(8, 145, 285, 102);
ptop.add(scrollPane);
TxtDesc = new JTextArea();
scrollPane.setViewportView(TxtDesc);
JLabel lblAjouterSiPossible = new JLabel("Ajouter si possible une image");
lblAjouterSiPossible.setBounds(334, 34, 195, 14);
ptop.add(lblAjouterSiPossible);
panImg = new JPanel();
//panImg.setBackground(Color.LIGHT_GRAY);
panImg.setBounds(324, 53, 205, 162);
ptop.add(panImg);
btnSupprimer = new JButton("Supprimer");
btnSupprimer.setBounds(433, 224, 96, 23);
ptop.add(btnSupprimer);
TxtPrix = new JTextField();
TxtPrix.setBounds(169, 72, 123, 20);
ptop.add(TxtPrix);
TxtPrix.setColumns(10);
JLabel lblPrixDuBien = new JLabel("Prix du bien :");
lblPrixDuBien.setBounds(8, 78, 120, 14);
ptop.add(lblPrixDuBien);
//design du panel droit pour les boutons
JPanel pbottom = new JPanel();
pbottom.setBounds(5, 270, 539, 43);
pan.add(pbottom);
pbottom.setLayout(null);
btnAnnuler = new JButton("Annuler");
//Fermeture de la fenêtre
btnAnnuler.addActionListener(this);
btnAnnuler.setBounds(268, 11, 89, 23);
pbottom.add(btnAnnuler);
btnValider = new JButton("Valider");
btnValider.addActionListener(this);
btnValider.setBounds(169, 11, 89, 23);
pbottom.add(btnValider);
//Et enfin ajout du panel pan,et rendre visible la frame
frame.setContentPane(pan);
//frame.show();
frame.setVisible(true);
}
//Parcours du bouton pour ajouter une image
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()== btnParcourir){
//Create a file chooser
final JFileChooser fc = new JFileChooser();
//int returnVal = fc.showOpenDialog(btnParcourir);
int returnVal = fc.showDialog(btnParcourir,"Attach");
if (returnVal == JFileChooser.APPROVE_OPTION) {
//File file = fc.getSelectedFile();
//This is where a real application would open the file.
String chemin = fc.getSelectedFile().getAbsolutePath().toString();
System.out.println(chemin);
public void paintComponent(Graphics g)
{
//super.paintComponent(g);
g.drawImage(img,0,0,null);
}
}
J'ai testé avec les deux mais rien ne s'affiche dans le panel. Je ne sais pas si c'est probleme avec le chemin de l'image, ou j'appel mal les classe. Ou est ce qu'on utilise vraiment la procedure PaintComponent?
Merci!!