Changer la source d'une image dans un panel
Bonjour,
J'ai une frame qui contient un panel. Dans ce panel, je veux y mettre une image. Et grace à un FileChoose, je veux pouvoir selectionner une image pour changer l'image actuellement affiché mais je n'y arrive pas après avoir passé un week end, après avec regardé les tutoriels, google, etc... Donc mon dernier recours, c'est vous ! :oops:
Voici mon code :
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 120 121 122 123 124 125 126 127 128 129 130 131 132
|
package hci2;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
public class Interface extends JFrame implements ActionListener {
//Variables
//Panel
private JPanel RootPanel = new JPanel();
private JPanel LeftMenuPanel = new JPanel();
private JPanel TopMenuPanel = new JPanel();
//Label
JLabel imageLabel = new JLabel();
//ImageIcon
//Button
JButton openButton = new JButton("Open an image...");
//JFileChooser
JFileChooser fileChooser = new JFileChooser();
//String
private String currentFile;
private static final long serialVersionUID = 1L;
public Interface() {
this.setTitle("HCI: Exercise 4");
this.setSize(400,400);
this.openButton.addActionListener(this);
this.LeftMenuPanel.add(openButton);
//Panel for the rest
RootPanel.setBorder(new BevelBorder(BevelBorder.RAISED));
RootPanel.setLayout(new GridLayout(1,1));
this.getContentPane().add("Center", RootPanel);
this.getContentPane().add("West", LeftMenuPanel);
this.getContentPane().add("North", TopMenuPanel);
}
@Override
public void actionPerformed(ActionEvent e) {
//Handle open button action.
if (e.getSource() == openButton) {
int returnVal = fileChooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
String path = file.getPath();
path = path.replace("\\","\\\\");
//This is where a real application would open the file.
currentFile = path;
System.out.println(path);
this.refresh(path);
} else {
}
}
}
public void refresh(String path) {
// Picture
/*
JLabel picture = new JLabel();
picture.setHorizontalAlignment(SwingConstants.CENTER);
Icon icon = new IconJAI();
*/
ImageIcon image = new ImageIcon(path);
imageLabel.setText(null);
imageLabel.setIcon(image);
// Container imagePanel = new JPanel(); //use FlowLayout
// imagePanel.add(imageLabel);
this.RootPanel.removeAll();
this.RootPanel.add(imageLabel);
//this.getContentPane().add(imagePanel);
//this.pack();
//this.setVisible(true);
}
static public void main(String args[]){
Interface j = new Interface();
j.setVisible(true);
j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
} |
Ce code fonctionne peu... En fait, dès que je charge un image, il faut que je resize la fenetre pour que l'image s'affiche. Et dès que je veux en charger une autre, l'application plante.
Est-ce que vous voyez d'où cela peut venir ?
Merci beaucoup,
Thom