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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
| public class DemoPanel extends JPanel {
private final JTextField[] fields;
private final JButton[] buttonsPaste;
private final JButton[] buttonsShow;
private File lastDir;
private JButton clearAllButton;
public DemoPanel(int nbRows) {
super(new GridBagLayout());
fields = new JTextField[nbRows];
buttonsPaste = new JButton[nbRows];
buttonsShow = new JButton[nbRows];
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets=new Insets(2, 2, 2, 2);
for(int i=0; i<nbRows; i++) {
gbc.gridy=i;
// label
JLabel label = new JLabel("Champ " + (i+1) + " : ");
gbc.gridx=0;
gbc.anchor=GridBagConstraints.EAST;
gbc.fill=GridBagConstraints.NONE;
gbc.weightx=0;
add(label, gbc);
// field
JTextField field = new JTextField();
label.setLabelFor(field);
gbc.gridx=1;
gbc.anchor=GridBagConstraints.CENTER;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.weightx=1;
add(field, gbc);
fields[i]=field;
// button1
JButton buttonPaste = new JButton("Paste");
gbc.gridx=2;
gbc.anchor=GridBagConstraints.CENTER;
gbc.fill=GridBagConstraints.NONE;
gbc.weightx=0;
add(buttonPaste, gbc);
buttonPaste.addActionListener(e-> pasteTo(field));
buttonsPaste[i]=buttonPaste;
// button2
JButton buttonShow = new JButton("Show");
gbc.gridx=3;
gbc.anchor=GridBagConstraints.CENTER;
gbc.fill=GridBagConstraints.NONE;
gbc.weightx=0;
add(buttonShow, gbc);
int index=i;
buttonShow.addActionListener(e-> showImage(index));
buttonShow.setEnabled(false);
buttonsShow[i]=buttonShow;
//
field.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
changedUpdate(e);
}
@Override
public void insertUpdate(DocumentEvent e) {
changedUpdate(e);
}
@Override
public void changedUpdate(DocumentEvent e) {
buttonShow.setEnabled(!field.getText().trim().isEmpty());
boolean enabled = false ;
for(JTextField field : fields) {
if ( !field.getText().isEmpty() ) {
enabled = true;
break;
}
}
clearAllButton.setEnabled(enabled);
}
});
}
clearAllButton = new JButton("Clear all fields");
gbc.gridx=2;
gbc.gridy=-1;
gbc.gridwidth=2;
gbc.anchor=GridBagConstraints.CENTER;
gbc.fill=GridBagConstraints.HORIZONTAL;
gbc.weightx=0;
add(clearAllButton, gbc);
clearAllButton.addActionListener(e->clearAllFields());
clearAllButton.setEnabled(false);
}
private void clearAllFields() {
for(JTextField field : fields) {
field.setText("");
}
}
private void showImage(int index) {
JTextField field = fields[index];
String text = field.getText();
if ( !text.trim().isEmpty() ) {
File file = new File(text);
if ( file.exists() ) {
try {
BufferedImage image = ImageIO.read(file);
int width = image.getWidth();
int height = image.getHeight();
Image fimage;
float scale = -1;
if ( width>300 || height>300 ) {
scale = 300f/Math.max(width, height);
fimage = image.getScaledInstance((int)(width*scale), (int)(height*scale), Image.SCALE_SMOOTH);
}
else {
fimage = image;
}
JPanel imagePanel = new JPanel(new BorderLayout());
JLabel imageLabel = new JLabel(new ImageIcon(fimage));
imagePanel.add(imageLabel, BorderLayout.NORTH);
Map<String, String> prop = new LinkedHashMap<>();
prop.put("Largeur×Hauteur, en pixels : ", width + " × " + height );
prop.put("Fichier : ", file.getName());
prop.put("Emplacement : ", file.getParentFile().getAbsolutePath().toString() );
prop.put("Taille fichier, en octets : ", String.valueOf(file.length()) );
if ( scale>0 ) {
prop.put("Echelle d'affichage : ", NumberFormat.getPercentInstance().format(scale) );
}
JTable proptable = new JTable(new AbstractTableModel() {
@Override
public Object getValueAt(int row, int col) {
for(Map.Entry<String, String> entry : prop.entrySet() ) {
if ( row==0 ) {
if ( col==1 ) {
return entry.getValue();
}
else {
return entry.getKey();
}
}
row--;
}
return null;
}
@Override
public Class<?> getColumnClass(int col) {
return String.class;
}
@Override
public String getColumnName(int col) {
if ( col==1 ) {
return "Valeur";
}
else {
return "Nom";
}
}
@Override
public int getRowCount() {
return prop.size();
}
@Override
public int getColumnCount() {
return 2;
}
});
JScrollPane scrollPane = new JScrollPane(proptable);
proptable.setFillsViewportHeight(true);
proptable.setPreferredScrollableViewportSize(new Dimension(500,75));
imagePanel.add(scrollPane, BorderLayout.CENTER);
JOptionPane.showMessageDialog(this, imagePanel, "Image " + (index+1), JOptionPane.INFORMATION_MESSAGE);
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "Impossible de visualiser l'image.\n\nFormat non supporté ou erreur de lecture.", "Erreur", JOptionPane.ERROR_MESSAGE);
}
}
else {
JOptionPane.showMessageDialog(this, "Impossible de visualiser l'image.\n\nFichier " + file.getName() + " non trouvé.\n\nChemin complet : " + file.getAbsolutePath(), "Erreur", JOptionPane.ERROR_MESSAGE);
}
}
}
private void pasteTo(JTextField field) {
JFileChooser chooser = new JFileChooser(lastDir);
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setMultiSelectionEnabled(false);
chooser.setFileFilter(new FileFilter() {
@Override
public boolean accept(File f) {
return f.isDirectory() || isImageExtension(f.getName());
}
@Override
public String getDescription() {
return "Fichier image";
}
});
chooser.setDialogTitle("Choisir une image");
if ( chooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION ) {
File selected = chooser.getSelectedFile();
if ( selected.exists() && isImageExtension(selected.getName()) ) {
field.setText(selected.toString());
}
lastDir = selected.getParentFile();
}
}
private boolean isImageExtension(String name) {
return MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(name).startsWith("image/");
}
public static void main(String[] args) {
JFrame frame = new JFrame("Démo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new DemoPanel(10));
frame.setSize(640, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
} |
Partager