Can not add a picture to the JFrame from an ActionListener class

As topic says, I can not add a picture to the JFrame from an ActionListener class which is a class inside the JFrame class.
I have a Map.java class where I load an image with ImageIcon chosen with JFileChooser.
I my window class (main class), I have following:
class OpenImage_Listener implements ActionListener
     public void actionPerformed(ActionEvent e)
          int ans = open.showOpenDialog(MainProgram.this);     // "open" is the JFileChooser reference
          if(ans == open.APPROVE_OPTION)
               File file = open.getSelectedFile();                    
               MainProgram.this.add(new Map(file.getName()), BorderLayout.CENTER);     // this line does not work - it does not add the choosen picture on the window,
                        //but if I add the picture outside this listener class and inside the MainProgram constructor, the picture apperas, but then I cannot use the JFileChooser.
                        showMessageDialog(MainProgram.this, fil.getName() ,"It works", INFORMATION_MESSAGE);  // this popup works, and thereby the ActionListener also works.
}So why can�t I add a picture to the window from the above listener class?

The SSCCE:
Ok, I think I solved it with the picture, but now I cannot add new components after adding the picture.
Look at the comment in the actionlistener class:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class Test extends JFrame{
     JButton b = new JButton("Open");
     JFileChooser jfc = new JFileChooser(System.getProperty("user.dir"));
     Picture pane;
     Test(){
          super("Main Program");
          setLayout(new BorderLayout());
          JPanel north = new JPanel();
          add(north, BorderLayout.NORTH);
          north.add(b);
          b.addActionListener(new Listener());
          setVisible(true);
          setSize(500,500);
          pane = new Picture("");
          add(pane, BorderLayout.CENTER);
     class Listener implements ActionListener {
          public void actionPerformed(ActionEvent e){
               int ans = jfc.showOpenDialog(Test.this);
               if(ans == jfc.APPROVE_OPTION)
                    File file = jfc.getSelectedFile();
                    Test.this.add(new Picture(file.getName()), BorderLayout.CENTER);
                    pane.add(new JButton("NEW BUTTON")); // Why does this button not appear on the window???
                    pane.repaint();
                    pane.revalidate();
     public static void main(String[] args)
          Test t = new Test();
class Picture extends JPanel
     Image pic;
     String filename;
     Picture(String filename)
          setLayout(null);
          this.filename = filename;
          pic = Toolkit.getDefaultToolkit().getImage(filename);
        protected void paintComponent(Graphics g)
            super.paintComponent(g);
            g.drawImage(pic,0,0,getWidth(),getHeight(),this);
            revalidate();
}

Similar Messages

  • Lightroom Book: I have saved a book in progress and now I can not add anymore photos to the filmstrip.  How can I change this?  Help! (and thank-you).

    Lightroom Book: I have saved a book in progress and now I can not add anymore photos to the filmstrip.  How can I change this?  Help! (and thank-you).

    How to add photos to a saved book?

  • I can not resume when I downloaded the application from the Apple Store. If I have to start from the beginning of a solution to this too please.

    I can not resume when I downloaded the application from the Apple Store. If I have to start from the beginning of a solution to this too please. ?

    Are  you away from your home country?
    Settings > General > International > Region Format > set to correct location?
    Settings > Phone > Dial Assist > ON

  • How can I add a picture to the Automator Action "Watermark PDF Documents.action"

    Looks like a bug: under 10.7.2 i cannot add a picture to the Automator Action "Watermark PDF Documents.action".
    Works perfectly under 10.6.8.

    Its a verified bug (Automator - Watermark PDF Documents).
    Please report to http://www.apple.com/feedback/macosx.html
    As a workaround, if you saved the Action in SL, option-click the action, navigate to the file "document.wflow", open in TextEdit, search for the key "fileNames" and replace your old image with the new

  • Help me. I can not add credit card in the AppStore  iPhone 4

    Hi
    I use the Iphone 4 , iOS 4.3.1, but recently I can not use my credit card with the iPhone appstore. itunes store with online payment and credit card is used as normal, but I can not use credit cards to buy the game directly in my iPhone. please help me find the cause of it ..
    Thanks

    What happens when you try to buy them, does the 'buy' button not work, do you get any error messages ... ?
    If you are getting a message to contact iTunes Support then you can do so via this link and ask them for help (we are fellow users on these forums, we won't know why the message is appearing) : http://www.apple.com/support/itunes/contact/ - click on Contact iTunes Store Support on the right-hand side of the page, then Purchases, Billing & Redemption
    If it's a different problem ... ?

  • How can i add a picture to the album?

    i have a picture for the album just dont know how to import it on the iTunes?

    In your iTunes Library, select the album. Then click on File/Get Info/Info and paste the artwork into the box named Artwork (in the lower right corner). Click OK

  • Can you add one picture at a time from a camera to iPhoto?

    Is it possible to enter one picture at a time from a camera to iphoto?

    I use my Eye-Fi SD card to upload one picture at a atime through its "Direct Mode" option. You can connect an iPhone or iPad to the card's own Wi-Fi network and download pictures.
    http://www.eye.fi/
    http://www.eye.fi/how-it-works/features/direct-mode
    -Doug

  • Where are my pictures i can not see my pictures with the new update Yosemite

    iPhoto not compatible with the new update yosemite

    I would be very careful, I selected the upgrade photos and now I have a 200GB library file but no way to access the photos, it appears to be something to do with the 10.10.2 upgrade and the use of an external Thunderbolt drive to store my photos on, but I am nearing desperation in trying to get back my years of photos (last effective backup - despite having time machine turned on) - appears to be 2012.
    This thread seems to have the most information
    Yosemite, iPhoto 9.6 upgrade, large photo library is not recognised.
    But it is outrageous that a 'minor' upgrade that was not meant to be about iPhoto could have such drastic outcomes.  It must be relating to the file service changes they were doing or the work they were preparing for the new photo app - either way, not happy!

  • Can not add new contact to the phone, screen gives no place to type in name

    In the past had no issued adding a new contact to iPhone 5s, now when that screen is opened there is no possibility to type names phone number etc.
    How do I fix this?

        Let's get your contacts updated, Scoop29! May I ask what software version are you running? Check through Settings>General>About. Are you given any " " or Edit option for existing contacts? I am eager to get to the bottom of this!
    Thank you,
    YaleK_VZW
    Follow us on Twitter @VZWsupport

  • Can not add games to iPod touch 4g...

    Can not add games to iPod touch 4g from ITunes (PC) it worked befour new update but why not now..

    I can now transfer apps including games to my ipod from windows pc in Itunes. I click on ipod tab on the left hand side then click on the apps tab then tick the box at the top to sync and now i can drag and drop apps. No apps or music was lost, still all on my ipod touch, but it did mix my apps up so i had to sort them out. I don't know why it did not work, no setting where change, but the update might of change it.

  • Can not add new printer

    I reset my printer list on my mother's MacBook and tried to add a new printer. There are no printers to add in the Add Printer list. I ran Repair Disk Permissions and still I can not add any printers through the System Preferences>Print & Fax. Why are there no printers to select?

    Try reinstalling the printer drivers (software) that came with the printer or go to the printer manufacture web site and download the drivers there.

  • Can not add pictures to my website

    When I edit my website on (bluedomino.com) I can not add pictures I receive an error message - an error has interrupted communication between your browser and the website server. Bluedomino looked at the problem and could not resolve said it was firefox issue. I switched over to Int.Explore and I have no problem it also seems to run faster. I have no problem viewing my web sites all the pictures display I can't add new ones. I'm able to upload the pictures to bluedomino but can not insert them on the edit page or post them.

    Hello solidroq,
    This can be worked around by creating a version of the video that is compatible with your iPod.
    Select a video and choose File > Create New Version > Create iPad or Apple TV Version.
    iTunes: Videos may be unable to sync to iPhone, iPad, or iPod
    http://support.apple.com/kb/TS1497
    Cheers,
    Allen

  • Can not add item to the shopping Cart with FireFox 8.0

    I can not add items to the shopping cart with Firefox 8.0. Switch to IE everything works then.

    Such details are stored in a cookie, so make sure that you do not block cookies on that site.
    *Tools > Page Info > Permissions
    You can inspect and manage the permissions for all domains on the <b>about:permissions</b> page via the location bar.
    *http://kb.mozillazine.org/Cookies
    *http://kb.mozillazine.org/Websites_report_cookies_are_disabled

  • Can not add items to the dock

    I can not add icons to the dock, I am able SOMETIMES to edit the dock (moving icons around or delete them), there is no rule, sometimes I can grab the icon on the dock, and sometimes I just can't (I have the pop up when I can't) , I deleted users/username/library/preferences/com.apple.dock.plist and issued a killall dock command, with no difference.
    any help will be appreciated

    Welcome to Apple Discussions.
    The Dock did not return to default on that command?
    Try again and also delete username/Library/Preferences/com.apple.finder.plist
    If it's still no-go create a new User go to System Preferences >> Accounts >> "+" (make it an admin acct) and test the Dock in this new account, if it works the problem is isolated to your User and not systemwide.
    Let us know.
    EDIT: Also are you running any enhancement software?
    -mj
    Message was edited by: macjack

  • My computer crashed. So does this mean I can not add any more music to my ipod from a different computer without all the songs being erased? Can I sync my ipod to a new computer if I allow all the songs to be erased?

    My computer crashed. So does this mean I can not add any more music to my ipod from a different computer without all the songs being erased? The ipod works fine and now i got a new computer. So it seems like my only options are leaving my ipod in a state where i cant add anymore songs or erasing all my songs and trying to use my ipod from my new computer. Can someone please inform me on how to have my ipod work from a different computer, even if i have to erase my songs. Thanks.

    Yes, if you choose the Erase and Sync option (or restore your iPod) you will be allowed to sync your iPod with the new computer.
    If you would like to save the content that's currently on your iPod, see this older thread from another forum member Zevoneer discussing the different ways to copy the content on an iPod back to your PC.
    https://discussions.apple.com/thread/2417169?start=0&tstart=0
    B-rock

Maybe you are looking for