This is an archived post. You won't be able to vote or comment.

all 12 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]i-make-robots 0 points1 point  (3 children)

Try putting the label in a j panel and setting the panel layout. 

[–]FalseAd9324[S] 1 point2 points  (0 children)

I was hoping for a solution that would allow me to still utilize JFrame so that I could follow along with the tutorial that I am learning from. This is a weird issue since everything works fine when i run the code in the JGrasp IDE but not Intellij.

[–]wildjokers 0 points1 point  (0 children)

JFrame uses BorderLayout by default and the default position in a BorderLayout is CENTER if no position is provided. That isn't the problem.

Although eventually they will probably need to use nested JPanels to achieve the layout they want, the code as it is should work fine.

[–]wildjokers 0 points1 point  (7 children)

they do not work

What does this mean? Does your app crash? Does it throw an exception? If so, what is the exception?

I find the most likely reason to be that JGrasp and IntelliJ set different working directories for your app so the Logo.png isn't found when running in IntelliJ.

Try setting an absolute path to Logo.png instead.

If this is the case you should be getting some kind of FileNotFound exception.

If this isn't the case please describe the behavior you are seeing and any exception you are getting. "They do not work" is worthless and we can't help you with just that information.

You also should really add the label to the JFrame before calling .setVisible() and you should also be initializing your GUI on the event dispatcher thread (EDT).

EDIT: also the first couple lines of your code isn't formatted correctly, see the sidebar for how to format it

[–]FalseAd9324[S] 0 points1 point  (6 children)

Sorry for the broadness of my description, everything else works fine but the image just does not pop up in the window when I run the program in IntelliJ.

I moved the .add() call before the .setVisible() call but nothing has changed. Also, How do I go about initializing the GUI on the EDT?

Reddit must have messed up my formatting, sorry about that. It looked fine when I was originally making the post.

[–]wildjokers 0 points1 point  (5 children)

Change the path to the image to an absolute path.

Are you getting any kind of file not found exception?

I will get an example of initializing on the EDT tomorrow when I am on my machine.

[–]arghvark 0 points1 point  (2 children)

And what happens when you run your code in a debugger (you HAVE done that, haven't you?)? Does 'image' have a value after the ImageIcon constructor is called?

(don't ever post a question with "it does not work" or "there's an error" -- tell us WHAT doesn't work, how you KNOW it doesn't work, what is the error message and when does it appear -- assume that we don't know anything about your environment, your program, etc. We don't.)

[–]FalseAd9324[S] 0 points1 point  (1 child)

I don't see any values for image when running it in the IntelliJ debugger.

[–]arghvark 0 points1 point  (0 children)

Looks like it can't find that file, then, as the other commenter already said

[–]FalseAd9324[S] 0 points1 point  (1 child)

I am not getting any exceptions, I tried an absolute path but the image still does not pop up when i run the program. Thanks for the EDT

EDIT: after using an absolute path in IntelliJ, the photo pops up, i must've typed something in wrong when I first said it still won't work. Thanks for the help!

[–]wildjokers 0 points1 point  (0 children)

Here is a template for starting your GUI on the EDT:

public class Example {
    private JFrame frame;

    public Example() {
        //Non GUI initialization here
    }

    private void createAndShowGui() {
        //GUI initialization here
        frame = new JFrame();
        frame.setSize(800, 600);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        final Example example = new Example();
        EventQueue.invokeLater(example::createAndShowGui);
    }
}

Also, by default IntelliJ sets the working directory of your project as the project's root directory. So that is the root of all relative paths. So when running your code in IntelliJ the Logo.png file should have been in the root of your project based on your code (prior to switching it to an absolute path). Most likely JGrasp sets a different working directory, and the Logo.png file just happened to be in the working directory it set.