0

I've been struggling with this problem for weeks to no avail. Other Java forums have not been able to help me with this so I'm hoping I can find someone here with new eyes. I have a JFrame in which, among other things, I am trying to insert an image. I have put together an SSCCE that reproduces the problem.

    public class ImageTest {
   public static void main(String[] args) {
    SplashScreen.ShowWindow();
   }
    }

    public class SplashScreen {

private JFrame frmKcbsEventsSearch;

public SplashScreen() {
    initialize();
}

public static void ShowWindow() {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                SplashScreen window = new SplashScreen();
                window.frmKcbsEventsSearch.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public void initialize() {
    frmKcbsEventsSearch=new JFrame();
    frmKcbsEventsSearch.getContentPane().setBounds(new Rectangle(200, 200, 520, 450));
    frmKcbsEventsSearch.getContentPane().setFont(new Font("Times New Roman", Font.PLAIN, 16));
    frmKcbsEventsSearch.setTitle("KCBS Events Search");
    frmKcbsEventsSearch.setBounds(200,200,520,450);
    frmKcbsEventsSearch.getContentPane().setLayout(new FlowLayout());
    frmKcbsEventsSearch.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    JLabel logo;
    java.net.URL logoURL=getClass().getResource("KCBSLogo.jpg");
    if (logoURL!=null) {
        try {
            logo = new JLabel();
            logo.setIcon(new ImageIcon(logoURL));
        }
        catch (Exception e) {
            logo = new JLabel("KCBS Logo");
        }
    }
    else {
        logo = new JLabel("KCBS Logo missing");
    }
    logo.setHorizontalAlignment(SwingConstants.CENTER);
    logo.setVerticalAlignment(SwingConstants.CENTER);
    frmKcbsEventsSearch.getContentPane().add(logo);
}
    }

It has been mentioned elsewhere that the layout is important to make this work. I've not had much luck getting layout managers to do what I want but in this case I just want to get the image displayed. Once I have that I'll work through getting it formatted the way I want.

  • 4
    Don't set component sizes. The key here, which I'm sure has been mentioned in other fora, is that you need to use proper layout managers for this to work. Also, if any of this is being done after the GUI has been rendered, then you must take care to have the layout managers re-layout their components by calling `revalidate()` on a container, and also suggest that they be repainted by calling `repaint()`. For more detailed and better help, consider creating and posting an [sscce](http://sscce.org). – Hovercraft Full Of Eels Oct 06 '12 at 19:04
  • Where else have you posted this question? – Hovercraft Full Of Eels Oct 06 '12 at 19:06
  • 1
    Take a look at his answer http://stackoverflow.com/questions/12762894/how-to-remove-jframe-border-as-to-let-an-image-touch-the-edge/12763654#12763654, I use a 'JLabel' as a background image. Whats the layout manager of the content pane? – MadProgrammer Oct 06 '12 at 20:40
  • 1
    1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) `logo` is added after the bounds are set. 3) ***Use Layouts.*** This is one of the many reasons to do so. If you have the logic to position one or many components, put it in a `IKnowExactlyWhereToPutSizeLayerTheComponentsSoQuitHasslingMeLayout` – Andrew Thompson Oct 06 '12 at 23:00
  • Go for the SSCCE as suggested by others. Two things can only happen if you do so: 1. You will find your mistake by trying to reproduce the problem (50% chance). 2. You post an actual SSCCE and it is very likely that within a few hours (if not minutes), someone will provide you with a valid explanation. – Guillaume Polet Oct 07 '12 at 00:39

1 Answers1

0

This code works perfectly fine! I changed the KCBSLogo.jpg to a source that I had named tile.png. When I ran it displayed perfectly. Make sure the KCBSLogo.jpg is in your SRC folder in the project.

https://i.stack.imgur.com/sYfYF.png

As you can see the tile image was shown.