how do I pass dimensions to a Swing JPanel creator class?

There are some problems in your code :

Firstly, if using frame.setLayout(new FlowLayout(FlowLayout.LEFT));(FlowLayout) then hereframe.getContentPane().add(toolBar.createToolBar(), BorderLayout.NORTH); why are you specifying BorderLayout.NORTH which has no effect

again case with frame.getContentPane().add(LabelCreator.createLabel(frame, "Java Swing", "purple"),BorderLayout.NORTH);

For your contentpane problem, firstly looking at following code(demo) :

JFrame frame= new JFrame();
frame.getContentPane().setPreferredSize(new Dimension(width, height));

When you pass different values for width and height you will see whole size of frame will get effected as i have not specified setSize anywhere, the reason for this is

  1. To appear onscreen, every GUI component must be part of a containment hierarchy. A containment hierarchy is a tree of components that has a top-level container as its root. We'll show you one in a bit.

  2. Each GUI component can be contained only once. If a component is already in a container and you try to add it to another container, the component will be removed from the first container and then added to the second.

  3. Each top-level container has a content pane that, generally speaking, contains (directly or indirectly) the visible components in that top-level container's GUI.

  4. You can optionally add a menu bar to a top-level container. The menu bar is by convention positioned within the top-level container, but outside the content pane. Some look and feels, such as the Mac OS look and feel, give you the option of placing the menu bar in another place more appropriate for the look and feel, such as at the top of the screen.

So the conclusion is that changing contentpane size will change frame size.

As your statement frame.setContentPane(ContentPaneCreator.createContentPane("darkseagreen", 800, 255)); is making the panel as contentpane, so again changing its size is to change frame size

But the other way around is :

frame.getContentPane().add(ContentPaneCreator.createContentPane("darkseagreen", 800, 255));

add a container to jframe and then add your components to that container( jpanel in your case )

Hope this will help and one thing more i also used to code like you creating methods and classes for every little purpose, but sometimes they raise complexities instead of simplicity, so my suggestion to you is to follow a design pattern like MVC or anyother you like


To illustrate my point: I have Name Age and Address of 4 people [Sam, 23, nj], [Nome, 25, ny], [Sim, 20, pa], [Jack, 12, pa] I need to send these 4 rows to the java side. I am familiar with java ...

I'm involved in a project where we generate pdf-files using iText. I want to generate superscipt text and I know that methods exists for converting chunks of text to superscript however, I am in a ...

I am using Spring boot. I want to check which logging implementation is printing the message - I know with Spring boot default is Logback, and I have excluded it as mentioned in this post so mostly ...

I Wonder if there is any anyway of loading a template dynamically using playframework2 and scala using reflection. In play 1.x i used to do something like following: public static void template(...