Corzuu
Corzuu

Reputation: 91

Filling a JPanel

I have a few items in a Jpanel which is then pushed to the top and used as a toolbar for a basic search engine. I'm having an issue where my last combobox isn't displaying as there isn't enough room. However, there's a lot of empty space on the left side and I need everything to move across to fill the JPanel so then this can display. So my question is how would I make these items start from the far left and go to right, thanks.

    //Labels for combo boxes
    JLabel Bookmarklbl = new JLabel("Bookmarks:");
    JLabel Historylbl = new JLabel("History:");

    FlowLayout flowLayout = new FlowLayout();
    MainBrowser.toolBar.setLayout(flowLayout);

    //Adding items to Panel
    MainBrowser.toolBar.add(Bookmarklbl);
    MainBrowser.toolBar.add(BookmarkList);
    MainBrowser.toolBar.add(bookmarkbtn);
    MainBrowser.toolBar.add(back);
    MainBrowser.toolBar.add(forward);
    MainBrowser.toolBar.add(MainBrowser.addressbar);
    MainBrowser.toolBar.add(home);
    MainBrowser.toolBar.add(reload);
    MainBrowser.toolBar.add(Historylbl);
    MainBrowser.toolBar.add(historyList);

    //Set the things added from left to right
    MainBrowser.main.setComponentOrientation(
            ComponentOrientation.LEFT_TO_RIGHT);

    //Add Panel to main frame 
    MainBrowser.main.add(MainBrowser.toolBar,BorderLayout.NORTH);

How the bar looks:http://postimg.org/image/l314iw6eh/

Upvotes: 0

Views: 217

Answers (4)

camickr
camickr

Reputation: 324088

The default for a FlowLayout is CENTER. If there is not enough space to display all the components, then the components are wrapped to the next line. Changing the alignment to LEFT won't fix this problem (just the default alignment of components).

showing the combobox bar is very large is there anyway I can limit the width?

You can limit the width of the combo box by using:

comboBox.setPrototypeDisplayValue( "XXXXXXXXXX" );

This will limit the preferred size of the combo box so it can display on your toolbar.

However, you will still want to see the full text of the items when the popup is displayed. For this you can use the Combo Box Popup.

Upvotes: 1

Ved
Ved

Reputation: 369

Maybe this will work.

MainBrowser.toolbar.set(new FlowLayout(FlowLayout.LEFT));

EDIT

Sorry it is MainBrowser.toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));

Upvotes: 0

m.cekiera
m.cekiera

Reputation: 5395

You can try to use BoxLayout, like:

toolBar.setLayout(new BoxLayout(toolBar,BoxLayout.X_AXIS)).

Upvotes: 0

Shrikant Havale
Shrikant Havale

Reputation: 1290

Assuming toolbar is JPanel and is using FlowLayout, this code might help you,

    JPanel panel = new JPanel(); // your toolbar panel
    FlowLayout flowLayout = (FlowLayout) panel.getLayout(); // flowlayout
    flowLayout.setAlignment(FlowLayout.LEFT); // alignment to left
    contentPane.add(panel, BorderLayout.NORTH); // adding this panel to original frame

Hope this helps

Upvotes: 1

Related Questions