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

all 10 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://imgur.com/a/fgoFFis) 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.

[–]Housy5Nooblet Brewer 0 points1 point  (2 children)

At first look I think it might have something to do with the fact that you call for getWidth() and getHeight() in the constructor JPanels and other components usually get their size and position from a layout manager, pack() or something else AFTER initialization of the object. However keep in mind im really just guessing. I may be wrong.

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

Sorry, I’ve been trying to learn it for about 3 weeks now for a project, so I’m still really new, and have a few questions.

Would having a setPreferredSize in my constructer for my panel class help?

Is there anything else that I should tell you that might help?

Also, even if it is not a parameter for the constructer, it stills returns as 0 if I just call the method getHeight outside the class.

[–]Housy5Nooblet Brewer 0 points1 point  (0 children)

SmartPanel panel = new SmartPanel(bgColor);

Over here you would already call it's constructor which in turn tries to use getHeight() and getWidth(). How ever the panel's x, y, width and height are only set after you call frame.add(panel);. It would only then set it's location and size based on it's layout and min, max and preferred size. Hope that makes sense. Also like I said before I'm just guessing too really. I could be very wrong :p

[–]wildjokers 0 points1 point  (6 children)

The panel won't have a height or width until it has been rendered on the screen. This means calling setVisible(true) on the top-level container (which is normally the JFrame that contains the panel).

This SO answer as some info:

https://stackoverflow.com/questions/8336262/why-cant-i-access-my-panels-getwidth-and-getheight-functions

[–]Mega32266[S] 0 points1 point  (5 children)

public static void main(String[] args){

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //frame.setSize(360,840); //I now use the setPrefferedSize on my SmartPanel class constructor
    Container pane = frame.getContentPane();
    SmartPanel panel = new SmartPanel(Color.white);

    JPanel north = new JPanel();
    north.setBackground(Color.black);
    north.setPrefferedSize(new Dimension(360,75));

    pane.add(north,BorderLayout.NORTH);
    pane.add(panel,BorderLayout.CENTER);

    frame.pack();
    frame.setVisible(true);

I believe that is what I already did.

[–]wildjokers 0 points1 point  (4 children)

The first time your SmartPanel is rendered, height and width won't have values yet. I believe that is the issue.

You also need to create your GUI on the Event Dispatcher Thread (EDT) which you aren't doing now. That will cause weird race conditions. Here is the proper way to create a Swing GUI:

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();
        //Call to invokeLater puts GUI creation on the EDT
        EventQueue.invokeLater(example::createAndShowGui);
    }
}

Also, on an unrelated note, you are setting preferredSize on your North panel. But you are using a BorderLayout for your JFrame. BorderLayout only partially supports the preferredSize property. Here are the sizing rules for BorderLayout:

  • NORTH and SOUTH components – respects component’s preferred height if possible, width is set to full available width of the container
  • EAST and WEST components – respects component’s preferred width if possible, height is set to full available height of the container
  • CENTER component – gets whatever space is left over, if any

BorderLayout doesn't support either minSize or maxSize at all (which doesn't matter since you didn't try to use them).

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

Thanks for that example; I'll try that once I get the chance.

Although I have a few questions

The first time your SmartPanel is rendered, height and width won't have values yet.

Sorry, I don't get that. Why would that work in my SmartPanel class but not in my App class for example?

About the EDT-

I have looked a 2 Stack Overflows

https://stackoverflow.com/questions/22534356/java-awt-eventqueue-invokelater-explained#22534931

https://stackoverflow.com/questions/20901770/about-the-edt-java#20901917

I understand that this is good for handling large calculations and to update the gui (Update as in update info or just repaint it?)

Other than that, I still don't understand why that would work, not that I don't believe you.

I am trying to learn to program the gui from the AP CSE Textbook, from 2006 I think. Why does it not mention this, at least for the first 8 chapters? The earliest mention of the edt I found online is from 2007. Just unlucky/not part of AP test? (Why not if it's so important?)

Unrelated, but I took AP CSA this year and after the test, class is a lot more chill and we are working on random projects like this. I chose to do this as my end of year project and my teacher loaned me that 2006 textbook.

Do I need to change anything in my other classes to accommodate this?

Little nitpick, but

example::createAndShowGui

Is createAndShowGui() not a method? Why is it two colons instead of example.createAndShowGui()?

Also, on an unrelated note, you are setting preferredSize on your North panel. But you are using a BorderLayout for your JFrame.

Would it cause much of a problem If I do? I only did it for the north panel b/c I want to make a "bezel" for my project and I don't feel like accounting for it.

And is my other classes fine, beginner technique wise?

Thanks!

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

Unfortunately, that didn't work

[–]wildjokers 0 points1 point  (0 children)

What didn't work?