Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem with loading image in java with FileChooser
    primarykey
    data
    text
    <p>I have write a class filechooser where I can choose files. I had searched for a class that loads images but nothing. My filechooser class can made a panel with button open and save. When I press button open I can search in my documents and when I'm going to open the image my image load class not responding I need some class that can synchronize with me filechooser and load my image from my documents and show it in the panel.</p> <pre><code>package project; import java.io.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.SwingUtilities; import javax.swing.filechooser.*; public class FileChooserDemo extends JPanel implements ActionListener { static private final String newline = "\n"; JButton openButton, saveButton; JTextArea log; JFileChooser fc; public FileChooserDemo() { super(new BorderLayout()); //Create the log first, because the action listeners //need to refer to it. log = new JTextArea(5,20); log.setMargin(new Insets(5,5,5,5)); log.setEditable(false); JScrollPane logScrollPane = new JScrollPane(log); //Create a file chooser fc = new JFileChooser(); openButton = new JButton("Open a File..."); openButton.addActionListener(this); //Create the save button. We use the image from the JLF //Graphics Repository (but we extracted it from the jar). saveButton = new JButton("Save a File..."); saveButton.addActionListener(this); //For layout purposes, put the buttons in a separate panel JPanel buttonPanel = new JPanel(); //use FlowLayout buttonPanel.add(openButton); buttonPanel.add(saveButton); //Add the buttons and the log to this panel. add(buttonPanel, BorderLayout.PAGE_START); add(logScrollPane, BorderLayout.CENTER); } public void actionPerformed(ActionEvent e) { //Handle open button action. if (e.getSource() == openButton) { int returnVal = fc.showOpenDialog(FileChooserDemo.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); //This is where a real application would open the file. log.append("Opening: " + file.getName() + "." + newline); } else { log.append("Open command cancelled by user." + newline); } log.setCaretPosition(log.getDocument().getLength()); //Handle save button action. } else if (e.getSource() == saveButton) { int returnVal = fc.showSaveDialog(FileChooserDemo.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); //This is where a real application would save the file. log.append("Saving: " + file.getName() + "." + newline); } else { log.append("Save command cancelled by user." + newline); } log.setCaretPosition(log.getDocument().getLength()); } } /** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event dispatch thread. */ public static void createLoad() { //Create and set up the window. JFrame frame = new JFrame("FileChooserDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Add content to the window. frame.add(new FileChooserDemo()); //Display the window. frame.pack(); frame.setVisible(true); } } </code></pre> <p>this is the class file chooser i need a class that can load the picture i open</p> <p>and this is the imageload code.</p> <pre><code> import java.awt.*; import java.awt.event.*; import java.awt.image.*; import java.io.*; import javax.imageio.*; import javax.swing.*; /** * This class demonstrates how to load an Image from an external file */ public class LoadImageApp extends Component { BufferedImage img; public void paint(Graphics g) { g.drawImage(img, 0, 0, null); } public LoadImageApp() { try { img = ImageIO.read(new File("strawberry.jpg")); } catch (IOException e) { } } public Dimension getPreferredSize() { if (img == null) { return new Dimension(100,100); } else { return new Dimension(img.getWidth(null), img.getHeight(null)); } } public static void main(String[] args) { JFrame f = new JFrame("Load Image Sample"); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } }); f.add(new LoadImageApp()); f.pack(); f.setVisible(true); } } </code></pre> <p>what i want is the image code read the file.aboslutepath and load the picture when i click it</p>
    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.
 

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