Skip to content
Advertisement

My JPanel doesn’t show after adding the paint method

I recently created my first board for my first board game and draw the board multiple ways on my JPanel.I finally decided to use Label, but since I needed to add pictures to that label, I had to change it into JLabel.The syntax is correct, but the Jlabels do not show. I tried cleaning my paint method and It started to show,but the background of the labels doesn’t change.

I tried putting the code inside my paint method and It didn’t work. I also tried adding the container to the frame after my draw method and it didn’t work either.

Here are some essential parts of my code:

JavaScript

I also create a new object of this class in another one of my classes and when I run it It shows errors for like 1sec and then wipes them off so I don’t know what those errors are for.

So this is my reduced code:

JavaScript

Advertisement

Answer

  1. When posting code don’t use the “Code Snippet”. Instead you paste your code, select the code and then use the {} button to highlight the code.

  2. Post a proper minimal reproducible example when posting code. This is minimal code that directly demonstrates the stated problem. The code should be in a single file and we should be able to copy/paste/compile and test the code. The point of this is to force you to eliminate all unnecessary code so it is easy to understand the problem. Most time you will find your own problem. You have been asked for an MRE is previous questions. Every question should have an MRE so we don’t have to guess what you are doing.

  3. It looks to me like you have a 2D grid. Don’t use the GroupLayout. This tells me you are using the IDE to generate your code. You are spending time learning the IDE and not learning Swing. You can easily use a GridLayout for a 2D grid.

  4. Don’t use static variables. Your width, height, boardWidth and boardHeight variables are not needed. Each Swing component should be responsible for determining its own preferred size. Then after all components are added to the frame, you pack() the frame before making it visible. The frame will then determines its appropriate size. In this case you can use setPreferredSize(…) for each of the JLabels to make them the size of your tile. So outside the loop you create an single instance of a Dimension object to be shared by all labels.

  5. Don’t use magic numbers in the setBounds() method. In you last question you were given the solution without using magic numbers. In fact you should not even be using the setBounds() method. It is the job of the layout manager to set the size/location of the component.

  6. Don’t use a Thread for animation. Animation should be done using a Swing Timer. All updates to Swing components should be done on the Event Dispatch Thread (EDT). The Swing Timer will execute on the EDT.

  7. Don’t create multiple instance of your Icon. An Icon can be shared by multiple components. So you create a single instance of the Icon outside the looping code and use that instance for all components.

  8. Same with the LineBorder. You only need a single instance.

  9. In the comments from your last question you were given a suggestion on how to write your randomBarrier() method using a single statement. The code you post here is completely unnecessary. The variable from the first 4 statement are not even used.

but the Jlabels do not show.

As I suggested earlier you can use a GridLayout on your panel and add the labels to the panel and the panel to the frame. Read the Swing tutorial on Layout Manager for more information and working examples.

I tried cleaning my paint method

There is no reason to use a custom paint method. You are using Swing components (JLabel) now and Swing will do all the painting for you. Also, as mentioned in your last question, it you ever do need to do custom painting you override the paintComponent() method. We should not have to keep repeating the same advice.

the background of the labels doesn’t change.

A JLabel is the only Swing component that is not opaque by default. So you need to use:

JavaScript

when you create each JLabel.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement