• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

View Files

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear friends...

I have a textbox where i am writing the complete path of an image to show an image in the right side of the window...

What should i do so that the system take the path from the input textbox and display the image file in the right side of the window?....

Please help me out..
Thank you
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class TestImageFrame {
private JFrame mainFrame = null;

private JTextField imagePathTF = null;

private JButton loadImageButton = null;

private MyCanvas imageCanvas = null;

private JPanel northPanel = null;

public TestImageFrame() {
mainFrame = new JFrame();
mainFrame.setLayout(new BorderLayout());
northPanel = new JPanel();
imagePathTF = new JTextField(20);
loadImageButton = new JButton("Load Image");
imageCanvas = new MyCanvas();

loadImageButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
try {
File f = new File(imagePathTF.getText());
BufferedImage image = ImageIO.read(f);
imageCanvas.setImage(image);
imageCanvas.repaint();
} catch (Exception e) {
System.out.println(e);
}
}
});

northPanel.add(imagePathTF);
northPanel.add(loadImageButton);

mainFrame.add(northPanel, BorderLayout.NORTH);
mainFrame.add(imageCanvas, BorderLayout.CENTER);
mainFrame.setSize(500, 600);

mainFrame.setVisible(true);
}

public static void main(String[] ar) {
new TestImageFrame();
}

class MyCanvas extends Canvas {
private BufferedImage image = null;

public void paint(Graphics g) {
g.drawImage(image, 0, 0, this);
}

public void setImage(BufferedImage bufferedImage) {
image = bufferedImage;
}
}
}
 
Everyone is a villain in someone else's story. Especially this devious tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic