1

I am trying to pass dimensions to a Java Swing JPanel creator class. No luck so far. I even put the contentPane.setPreferredSize(new Dimension(intWidth, intHeight)); in a try / catch block and I did not get any system output. I am passing a pretty substantial value, frame.setContentPane(ContentPaneCreator.createContentPane("darkseagreen", 800, 255));, but the application is still as small as the components it is around. What can be done to fix it? Thanks.

import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JPanel;

public final class ContentPaneCreator {

    public static Container createContentPane(String color, int intWidth, int intHeight) {

        JPanel contentPane = new JPanel();
        // Pass the colour
        contentPane.setBackground(ColorFactory.getInstance().getColor(color));
        // Pass the dimensions
        try {
        contentPane.setPreferredSize(new Dimension(intWidth, intHeight));
        }
        catch (Exception ex) {
            System.out.println(ex);
        }
        return contentPane;
    }
}

StickyNotes()

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class StickyNotes extends JFrame {

    private static final long serialVersionUID = 1L;

    public static String appName;
    public static String fileConfirmSave;
    public static String txtConfirmChange;


    private void createStickyNotesGUI() {

        ConfigProperties config = new ConfigProperties();
        // ConfigProperties config2 = new ConfigProperties().getFileIn();

        appName = config.getConfigProperties("appName").toUpperCase();
        fileConfirmSave = config.getConfigProperties("fileConfirmSave");
        txtConfirmChange = config.getConfigProperties("txtConfirmChange");

        // Create and set up the window.
        JFrame frame = new JFrame();
        frame.setTitle(appName);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout(FlowLayout.LEFT));
        /* BEGIN ROOT Pane: */

        /* BEGIN Layered Pane: */
        // Add Main Menu
        MainMenuBar mainMenu = new MainMenuBar();
        frame.setJMenuBar(mainMenu.createMainMenuBar(frame));

        /* BEGIN CONTENT Pane(s): */

        // Add JPanel Content // can I pass a layout object?
        frame.setContentPane(ContentPaneCreator.createContentPane("darkseagreen", 800, 255));

        // Add Tool Bar /* needs to be run AFTER we .setContentPane() in order for it to be layerd on top */
        ToolBarCreator toolBar = new ToolBarCreator();
        frame.getContentPane().add(toolBar.createToolBar(), BorderLayout.NORTH);


        // TODO
        /*
         * Pass dynamic color values to LabelCreator() once you get
         * newStickyNote() working
         */
        frame.getContentPane().add(
                LabelCreator.createLabel(frame, "Java Swing", "purple"),
                BorderLayout.NORTH);

        // Display the window here with JFrame//
        //frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
        // TODO
        /* set configuration to remember frame size */
        frame.pack();
        frame.setVisible(true);
    }

    public void doExit(JFrame frame) {
        boolean fDirty = true;
        if (fDirty)
            switch (JOptionPane.showConfirmDialog(StickyNotes.this,
                    fileConfirmSave, txtConfirmChange,
                    JOptionPane.YES_NO_OPTION)) {
            case JOptionPane.YES_OPTION:
                // if (doSave())
                frame.dispose();
                break;
            case JOptionPane.NO_OPTION:
                frame.dispose();
            }
        else
            frame.dispose();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new StickyNotes().createStickyNotesGUI();
            }
        });
    }
}
JoJo
  • 4,643
  • 9
  • 42
  • 65

2 Answers2

1

Try:

contentPane.setSize(intWidth, intHeight);

I'm pretty sure the above is sufficient, but you can also try:

contentPane.setPreferredSize(new Dimension(intWidth, intHeight));
contentPane.setMinimumSize(new Dimension(intWidth, intHeight));
contentPane.setMaximumSize(new Dimension(intWidth, intHeight));
Zong
  • 6,160
  • 5
  • 32
  • 46
  • 1
    Yes, perhaps a better answer is to avoid having to use these methods at all. But his method signature is `createContentPane(String color, int intWidth, int intHeight)`, so I think this is a practical solution for whatever he/she's trying to do. – Zong Jul 31 '12 at 01:00
1

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);

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

JFrame frame= new JFrame();
frame.getContentPane().setBackground(Color.red);
frame.getContentPane().setPreferredSize(new Dimension(width, height));
frame.pack();
frame.setVisible(true);

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

Harmeet Singh
  • 2,555
  • 18
  • 21
  • Thank you! I have so much to digest here, but you helped me get the whole application started with your corrections. I was wondering why can I use this instead, and it still works: frame.add(ContentPaneCreator.createStickyNote("darkseagreen", 600, 755)); – JoJo Jul 31 '12 at 23:29