2 Replies - 177 Views - Last Post: 09 September 2011 - 08:30 AM Rate Topic: -----

#1 l0ckz0r  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 04-June 09

Passing varibles between action's - JAVA GUI

Posted 08 September 2011 - 11:56 PM

So it's been about a year since I've studied Java and I've had to use it again and I'm at my wits end trying to remember some basic things

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);
        }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Passing varibles between action's - JAVA GUI

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1984
  • View blog
  • Posts: 4,827
  • Joined: 10-September 10

Re: Passing varibles between action's - JAVA GUI

Posted 09 September 2011 - 12:17 AM

Not sure what you mean by your third bullet "needs access to the File." Do you mean physical access, or do you just mean there needs to be awareness of the File name? If simply awareness of the file name, use a class that maintains your program's data, sometimes referred to as the Model, and set a field with the File's name, making it available to other program elements using a 'getter.'
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9038
  • View blog
  • Posts: 33,526
  • Joined: 27-December 08

Re: Passing varibles between action's - JAVA GUI

Posted 09 September 2011 - 08:30 AM

You're just reading the Image in the specific actionPerformed() method. What do you want to do with that Image? It will just be garbage collected at the end of the method.

Also, avoid using the NetBeans (or any other) GUI Builder. It produces horrid, unmaintainable code that humans can neither read nor maintain.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1