Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think what you want is the chosen image to be shown, if so change you <code>jButton1ActionPerformed</code> method to this:</p> <pre><code>private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser(); chooser.showOpenDialog(null); File f = chooser.getSelectedFile(); String filename = f.getAbsolutePath(); jTextField1.setText(filename); try { ImageIcon ii=new ImageIcon(scaleImage(120, 120, ImageIO.read(new File(f.getAbsolutePath()))));//get the image from file chooser and scale it to match JLabel size jLabel1.setIcon(ii); } catch (Exception ex) { ex.printStackTrace(); } } public static BufferedImage scaleImage(int w, int h, BufferedImage img) throws Exception { BufferedImage bi; bi = new BufferedImage(w, h, BufferedImage.TRANSLUCENT); Graphics2D g2d = (Graphics2D) bi.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY)); g2d.drawImage(img, 0, 0, w, h, null); g2d.dispose(); return bi; } </code></pre> <p>an even better solution would see you using a <a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html" rel="nofollow">SwingWorker</a> to load the image so UI doesnt become unresponsive (if the image is large):</p> <pre><code>private ImageIcon ii; private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { JFileChooser chooser = new JFileChooser(); chooser.showOpenDialog(null); final File f = chooser.getSelectedFile(); String filename = f.getAbsolutePath(); if (f == null) { return; } jTextField1.setText(filename); SwingWorker sw = new SwingWorker() { @Override protected Object doInBackground() throws Exception { Thread.sleep(5000);//simulate large image takes long to load ii = new ImageIcon(scaleImage(120, 120, ImageIO.read(new File(f.getAbsolutePath())))); return null; } @Override protected void done() { super.done(); jLabel1.setIcon(ii); } }; sw.execute(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload