views:

297

answers:

3

Hello all.

Problem:

My program layout is fine, as below before I add JToolbar to BorderLayout.PAGE_START Here's a screenshot before JToolbar is added: alt text

Here's how it looked like after adding JToolbar: alt text

May I know what did I do wrong?

Here's the code I used:

    //Create the text pane and configure it.
    textPane = new JTextPane();
    -snipped code-
    JScrollPane scrollPane = new JScrollPane(textPane);
    scrollPane.setPreferredSize(new Dimension(300, 300));

    //Create the text area for the status log and configure it.
    changeLog = new JTextArea(5, 30);
    changeLog.setEditable(false);
    JScrollPane scrollPaneForLog = new JScrollPane(changeLog);

    //Create a split pane for the change log and the text area.
    JSplitPane splitPane = new JSplitPane(
            JSplitPane.VERTICAL_SPLIT,
            scrollPane, scrollPaneForLog);
    splitPane.setOneTouchExpandable(true);

    //Create the status area.
    JPanel statusPane = new JPanel(new GridLayout(1, 1));
    CaretListenerLabel caretListenerLabel =
            new CaretListenerLabel("Caret Status");
    statusPane.add(caretListenerLabel);

    //Create the toolbar
    JToolBar toolBar = new JToolBar();
    -snipped code-

    //Add the components.
    getContentPane().add(toolBar, BorderLayout.PAGE_START);
    getContentPane().add(splitPane, BorderLayout.CENTER);
    getContentPane().add(statusPane, BorderLayout.PAGE_END);

    //Set up the menu bar.
    actions = createActionTable(textPane);
    JMenu editMenu = createEditMenu();
    JMenu styleMenu = createStyleMenu();
    JMenuBar mb = new JMenuBar();
    mb.add(editMenu);
    mb.add(styleMenu);
    setJMenuBar(mb);

Please help, I'm new to GUI Building, and I don't feel like using Netbeans to drag and drop the UI for me... Thank you in advance.

+1  A: 
Bragboy
@Bragaadeesh: Okay... that is weird. I'll try to copy paste the one you used and test and see. Thanks.
Alex Cheng
A: 

EDIT: I understand why now.

I used Paint to give me a rough estimation of the pixels, and previously I did not know that the height from the start of the top frame title bar is counted! So that adds up to ~= 504. I get it now.

So next time when I have to set the height roughly, I think I'll use Paint.

alt text


Hmmm weird. I have to change from:

//Display the window.
frame.setSize(640, 480);

to

//Display the window.
frame.setSize(640, 504);

Then only it works.

Can somebody teach me how to estimate or set the width/height for the components? Because initially I wanted it to be 640,480 but apparently now it needs 640,504.

Alex Cheng
+2  A: 

Instead of using setSize() on the JFrame, set the preferred size of your center component as you do now and invoke pack(), which "Causes this Window to be sized to fit the preferred size and layouts of its subcomponents." Expanding on @Bragaadeesh's example,

public static void main(String[] args) {
    TestFrame frame = new TestFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.build();
    frame.pack();
    frame.setVisible(true);
}

Then, change to scrollPane.setPreferredSize(new Dimension(500, 300)) or JTextArea changeLog = new JTextArea(10, 30) to see the difference.

trashgod
@trashgod: Oh! So that's how it's supposed to be. Thank you!
Alex Cheng