0

I have a JFrame with 10 JPanel components that were put using Swing. I have one panel created from code. I added JLabel and stuff on it.

The problem is when I try to center all the panels using the GridBagLayout on frame. The panels are centered, but the one that I created manually doesn't appear anymore.

How to make the manually created one appear?

Code in main:

Interface f = new Interface();
f.setExtendedState(f.getExtendedState() | JFrame.MAXIMIZED_BOTH); 
f.setLayout(new GridBagLayout());
f.setVisible(true);

Code to add the manually created panel to JFrame:

this.getContentPane().add(hotelPanel);
hotelPanel.setSize(800, 1000);
hotelPanel.setLocation(300, 1);
hotelPanel.setVisible(false);
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Nicu Mihai
  • 35
  • 5
  • Because it's not visible?: `hotelPanel.setVisible(false)`. Also, `setLocation` and `setSize` won't do much, because you're not using a `null` layout. Btw, `f.setVisible(true)` should be called after you added all components. – Lukas Rotter May 07 '16 at 12:05
  • 1
    1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson May 07 '16 at 12:09
  • @LuxxMiner When i click a button it become true. But does not aprear on my frame. If i remove the layout on the frame it apears. – Nicu Mihai May 07 '16 at 12:10
  • Tip: Add @LuxxMiner (or whoever, the `@` is important) to *notify* the person of a new comment. – Andrew Thompson May 07 '16 at 12:10
  • Is there any way to center the other panels without setting the posizion manually. For example panel.setPozision(200,300);. – Nicu Mihai May 07 '16 at 12:12
  • *"Is there any way,,"* Yes. Is there any chance of seeing that MCVE & ASCII art? – Andrew Thompson May 07 '16 at 12:13
  • @AndrewThompson Yes it is. But i don't know what the minimal. This is the minimal. I add the panels from swing. This is the only onw created. I have on the hotelPanel lots of labels an a picture in the background. The problem is when i try to center all the other panel using GridBagLayout. The hotelPanel does't apear . When I change the layout on the frame to null it apears – Nicu Mihai May 07 '16 at 12:20
  • *"This is the minimal."* Did you notice there were ***4*** letter in MCVE? It is not the 'minimal' that is the problem, it is the other 3. Note that an MCVE might be 100-200 LOC, So long as it is C, V and an E.. – Andrew Thompson May 07 '16 at 12:28
  • @AndrewThompson do you want to put the hole code ? there are 2K of line. The concrete thing is that i want to center all the panels in null layout. I told you about the example. How can you verify the code ? – Nicu Mihai May 07 '16 at 12:38
  • *"do you want.."* I want you to follow that link, read it, and ask specific questions about anything in it you do not understand. It is considered clear enough to list in 2 'question close reasons'! – Andrew Thompson May 07 '16 at 12:43
  • @AndrewThompson , i'll do my best to solve this. I don't konw what do you want from me – Nicu Mihai May 07 '16 at 12:44
  • `do you want to put the hole code ? there are 2K of line.` - your question is about displaying components on a pane. It does not take 2K to do that. It takes 2 statements to create a panel and add it to the frame. It takes two more statements to create a component and add it to the panel. So if you want 5 components on the panel it will take you 12 statements plus a few more statements to create an display the frame. So to display a frame with a panel with 5 components will take maybe 20 lines of code. Because you are using a GridBagLayout you need a few more statements for the constraints. – camickr May 07 '16 at 15:32
  • So create a simple example with just the frame and the components. Your application code is irrelevant to the problem of "centering" components on a frame. Then once you understand how the layout manager works you change your real application. This is called an `MCVE` or `SSCCE`. – camickr May 07 '16 at 15:34

0 Answers0