I have a GUI which I created using the Netbeans editor, basically what I want to know is:
- I have 2 buttons, one opens a file chooser and gets a File (named file)
- The second button loads the file into memory as a buffered image
- I have written the code in the action listeners, the second button listener needs access to the File from the first event, how do I do that?
private void chooseActionPerformed(java.awt.event.ActionEvent evt) {
message.setText("Choosing a file");// TODO add your handling code here:
//Handle open button action.
if (evt.getSource() == choose) {
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(histogramGUI.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
//This is where a real application would open the file.
System.out.print("Opening: " + file.getName() + ".");
} else {
System.out.print("Open command cancelled by user.");
}
}
}
private void convertActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
ImageInputStream is = ImageIO.createImageInputStream(file);
Iterator iter = ImageIO.getImageReaders(is);
if (!iter.hasNext())
{
System.out.println("Cannot load the specified file "+ file);
System.exit(1);
}
ImageReader imageReader = (ImageReader)iter.next();
imageReader.setInput(is);
try {
BufferedImage image = imageReader.read(0);
} catch (IOException ex) {
Logger.getLogger(histogramGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}

New Topic/Question
Reply




MultiQuote






|