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/
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
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.
You can try to use BoxLayout, like:
toolBar.setLayout(new BoxLayout(toolBar,BoxLayout.X_AXIS)).
Maybe this will work.
MainBrowser.toolbar.set(new FlowLayout(FlowLayout.LEFT));
EDIT
Sorry it is MainBrowser.toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
Related
I've got a BorderLayout going on, and am working on the North panel. Inside the North panel, I'd like to have 3 components: a picture that is on the left, and two buttons that split the remaining width of the Frame. Right now I'm attempting to accomplish this with another BorderLayout.
The Frame is resizable.
The picture is assigned to BorderLayout.WEST, and with the following code I attempt to add another panel that contains only buttons. The panel is then added to the CENTER of the Frame's NORTH layout component.
//create panel to hold buttons
JPanel btnPanel = new JPanel();
btnPanel.setLayout(new BorderLayout());
JButton btnMatrix = new JButton("Matrix View");
btnPanel.add(btnMatrix);
JButton btnList = new JButton("List View");
btnPanel.add(btnList);
add(btnPanel);
however, the buttons both try to take up the entire panel. If I leave it to a flow layout (I don't use btnPanel.setLayout(new BorderLayout()); in the above code), the buttons sit nicely in the center, but do not expand and share the btnPanel.
Thoughts? I'm new enough to Java that I could be going about this the wrong way from the start.
btnPanel.setLayout(new BorderLayout());
You didn't specify a constraint when you added the buttons to the panel. So both buttons are added to the CENTER. However, only one component can be added to the CENTER, so only the last one added is displayed.
You can try a different layout:
btnPanel.setLayout( new GridLayout(0, 2));
Then each button will be the same size and both buttons will fill the space available.
I created a form. Actually it is 10 JLabels with each JLabel having a text field next to it.
consider,
JLabel_called_Name JTextField_to_obtain_name
JLabel_called_Phone JTextField_to_obtain_phone_number
and so on..
I usually position this in a JPanel and display it in a frame. But my panel and frame have height smaller than the size required to hold 10 of these Labels and Textfields.
So I wish to add them to a JScrollPane.
But in every question I only obtained information of how to add Jlabels to a scroll pane using a Box,
or adding JLabels to a JList.
However I would like to represent it in the format I showed above. A Jlabel beside a JTextField.
How can one acheive this?
But in every question I only obtained information of how to add Jlabels to a scroll pane using a Box, or adding JLabels to a JList.
You can add any component to a JScrollPane:
JPanel = new JPanel();
panel.add( label1 );
panel.add( textField1 );
JScrollPane scrollPane = new JScrollPane( panel );
The trick is choosing the correct layout manager for you panel. Read the Swing tutorial on Layout Managers to help you decide how to design the panel. You can also nest panels to get your desired layout.
I'm working on panel which has four components: a label, a textfield that is uneditable, another label and a JTextArea. These components are aligned vertically one after the other and I am using Box Layout for this panel. What I have noticed is that when I type in the text area component, it shifts the labels character by character till it can't anymore. They labels initially are aligned to the left but as soon as I start typing they start moving to the right. I have tried so many other components but Box Layout seems to do what I want, I just have to fix this error. Any one ideas?
This is my panel code:
JPanel Panel = new JPanel();
Panel.setLayout(new BoxLayout(Panel,BoxLayout.Y_AXIS));
Panel.add(new JLabel("just a label here"));
Panel.add(textFieldComponent);
Panel.add(new JLabel("just a label here"));
Panel.add(textAreaComponent);
Use another LayoutManager e.g. GridBagLayout or
Place the JLabel in a panel with Horizontal BoxLayout (or BorderLayout) to actieve desired alignment.
another alternative:
add the textAreaComponent to a JScrollPane (set the scrollPane's alignmentX to 0.0f)
I had this issue as well and I added: textArea.setLineWrap(true). It prevented other objects from being pushed when you type in the field.
You should definitely use another Layout. One of my personal favorite is Forms from JGoodies. I've yet to see a Java Swing layout that comes anywhere close.
I want to add different buttons, vertically stacked, to a JPanel at run-time and use a JScrollPane so that all buttons will be visible (with some scrolling).
In order to do this, I have added my JPanel to a JScrollPane, after which I add buttons to my JPanel.
However, when I do this the vertical scrollbar does not allow me to see all images. For example when I add 7 buttons I can only scroll to see 5 full images and half of the 6 images.
Why doesn't my scrollbar allow me to display all 7 buttons?
Create the panel and scrollpane like:
JPanel panel = new JPanel();
JScrollPane scrollPane = new JScrollPane( panel );
When you add buttons to the panel at run time the code should be:
panel.add( button );
panel.revalidate();
As long as you are using a layout manager the preferred size will be recalculated and the scrollbar will appear.
Make scroll pane a wrapper over your panel - new JScrollPane (myPanel) and add it instead of naked panel in your panel's container.
You also may want to play with its setPreferredSize() method.
I need to create a panel where I can put some rectangles and it automatically reorder just inserting a scrollbar and growing up vertically. Also this panel can be resizable and again the rectangles must to be reordered to correctly be displayed inside the panel.
If I understand the question you want components to wrap to the next line so that the panel grows vertically while the width remains fixed.
If so then check out the WrapLayout
Note: the FlowLayout already supports the wrapping of components to a new row on the panel. This issue is that the preferred size calculation assumes all components are placed on a single row. The WrapLayout overrides the preferred size calculation to support the wrapping of components on a new row.
Use a JScrollPane. If you never want a horizontal scroll bar you can add the following:
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
(By default the scroll pane will add horizontal and vertical scroll bars when required.)
The scroll pane itself will only be resizeable if you add it to a Container with the appropriate layout manager; e.g.
JFrame frm = new JFrame();
frm.setLayout(new BorderLayout());
JScrollPane sp = new JScrollPane();
frm.add(sp, BorderLayout.CENTER); // Adding a component to the CENTER will cause the component to grow as the frame is resized.