Sorry for not putting specific code in previous post. I realized I was doing something wrong, but now am having another problem. I am trying to place a JPanel at a specific place on a JFrame after clicking a button but nothing is happening, here is my code for my Actionperformed method.
public void actionPerformed(ActionEvent e)
{
JPanel pa=new JPanel();
frame.getContentPane().setLayout(null);
pa.setBackground(Color.yellow);
pa.setLocation(50,150);
pa.setSize(150,100);
pa.setBounds(50,150,150,100);
pa.setOpaque(true);
frame.add(pa);
}
There are many ways you can do this.
Use setLocation(x, y) and setSize(length, width)
Use setBounds(x, y, length, width)
If 1 and 2 don't work, look into layout Managers:
I highly recommend that you take a look at the Layout Managers JavaDoc. below are links for the each one, and the ones I would recommend for your case have a asterisk(*) next to them.
BorderLayout* is for general directions(NORTH, SOUTH, EAST, WEST)
BoxLayout puts components in a single row or column. It respects the components' requested maximum sizes and also lets you align components.
CardLayout lets you implement an area that contains different components at different times, often controlled by a combo box, with the state of the combo box determining which panel (group of components) the CardLayout displays.
FlowLayout is the default layout manager for every JPanel. It simply lays out components in a single row.
GridBagLayout is a sophisticated, flexible layout manager. It aligns components by placing them within a grid of cells, allowing components to span more than one cell. The rows in the grid can have different heights, and grid columns can have different widths.
GridLayout* simply makes a bunch of components equal in size and displays them in the requested number of rows and columns.
GroupLayout is a layout manager that was developed for use by GUI builder tools, but it can also be used manually. GroupLayout works with the horizontal and vertical layouts separately. The layout is defined for each dimension independently. Consequently, however, each component needs to be defined twice in the layout.
SpringLayout is a flexible layout manager designed for use by GUI builders. It lets you specify precise relationships between the edges of components under its control. For example, you might define that the left edge of one component is a certain distance (which can be dynamically calculated) from the right edge of a second component. SpringLayout lays out the children of its associated container according to a set of constraints.
Disclaimer: Most of these are direct quotes for the Oracle JavaDoc regarding Layout Managers.
Related
I need suggestion for layout type for following task. I have a panel on which user will be able to add or remove some components (label or another panel), which are all same size. There will be specific number of components at same column (like 4 components per column) but the number of components in rows will depend on user. The distance between components will be fixed, right, left up top. I will link you the image of what i need... Thanks.
link
I think you want a GridLayout, however I suggest using a third-party layout manager like MigLayout. For the case where you have empty cells, you can nest JComponents within each other with different layout managers ( see this SO question). MigLayout would be easier because it can simulate a GridLayout while respecting the preferred size you set on your JComponents, which allows you to have empty cells without the components stretching.
I have a JFrame with layout BorderLayout, I add JPanels using BorderLayout.CENTER
I have a JPanel(added using CENTER), that uses GridLayout to place 4 buttons.
These buttons are MASSIVE, taking up the whole screen. Or, I do it my way as follows:
I have a JFrame with layout null, I set JPanel bounds and add them.
I have a JPanel:
It sets it's own bounds so it takes up center 1/2 of screen, and is only 1/8 of the screen
tall.
It adds buttons using grid layout, and results in a nice line of buttons.
Obviously the second option looks better, but as I wish to conform to the norm, and always use layouts... How to I mix customization and layouts?(Or just solve my problem at all)
When you add a componeent to BorderLayout.CENTER, it will expand to fill the remaining space of the container not used by the other portions of the BorderLayout. Also, you can only add one component at a time to BorderLayout.CENTER. Subsequent adds will replace the previous component.
As for solving your exact problem, I suggest you start by taking a look at A Visual Guide to Layout Managers. This will give you a primer on what layouts are available in the Swing API. Oracle also has more in-depth tutorials for each layout. By nesting layouts, you can give your UI any look that you wish while leveraging their power, especially auto-calculations when a window is resized or otherwise changed.
I need to implement ui for list of contacts like in skype. An contact represented by custom class(JContact) which derived from JPanel. I tried to use different layouts but not received expected result. Main frame has next structure.
JFrame -> JPanel(contactsPanel)-> JScrollPane(scrollContacts)->JPanel(contactPanel)
scrollContacts.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrollContacts.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
If use FlowLayout for contactPanel (see FlowLayout in image)
Strange behavior i think, because expected what each contacts will one under one because indicate HORIZONTAL_SCROLLBAR_NEVER for scrollContacts.
If use Grid or Box layout. Layout automatically re-size my panels, it's look very ugly. see Grid&Box layouts image.
Expected result see "expected" image
----SEE IMAGE----
I'm not native speaker, so please sorry for my bad English.Thank you for attention!
Quoting the Swing tutorial:
The FlowLayout class puts components in a row, sized at their preferred size. If the horizontal space in the container is too small to put all the components in one row, the FlowLayout class uses multiple rows
So the result you get is expected.
A GridLayout object places components in a grid of cells. Each component takes all the available space within its cell, and each cell is exactly the same size. If the GridLayoutDemo window is resized, the GridLayout object changes the cell size so that the cells are as large as possible, given the space available to the container.
So the result you get is also expected.
When a BoxLayout lays out components from top to bottom, it tries to size each component at the component's preferred height. If the vertical space of the layout does not match the sum of the preferred heights, then BoxLayout tries to resize the components to fill the space.
So the result you get is also expected.
But, a box layout can contain glue components to avoid that.
I would thus use a vertical box layout, and add a vertical glue as the last component. Read the tutorial.
How would I go about making the length of the tabs automatically resize based on how much room is left in that row of tabs.
Picture:
As you can see the tab's width is based off the text in the tab.
If you need me to explain what I want better then just ask me because I don't know if I made it clear enough.
You can use a custom component and set it's preferred size. For example, in ButtonTabComponent of TabComponentsDemo:
label.setPreferredSize(new Dimension(...));
You have to choose an appropriate dimension based on other aspects of your layout, so it won't be automatic.
I want to define a size for the actual tabbed pan.
The size of the JTabbedPane is a function of the dimensions and LayoutManager of the Container to which it has been added. In the example cited, the default layout of the frame's content pane is BorderLayout, and add(pane) adds it to the center by default.
To accomplish your goal, I see two approaches:
Divide the current width of the enclosing Container among the existing tabs and repaint the tabbed pane, as shown in this example.
Develop your own implementation of TabbedPaneUI and interpret SCROLL_TAB_LAYOUT accordingly.
In which Swing layout manager it is possible to change layout areas programmatically? And how to do this with lowest cost?
I have to create component with functionality similar to JSplitPane but with three panels from scratch. One of the moments is to Expand/Collapse one of the panels after clicking oneTouchExpandable button on the divider. But the problem is that I don't know how to implement this collapse action. I tried just setting panels width to 0, but the layout area which contains this panel doesn't shrink after the component. I tried to do this in all Layout Managers, but effect is the same.
Thanks.
When making a change that affects the layout of a panel after the GUI is visible you need to revalidate() the panel which essentially invoke the layout on the panel. In your case it might be easier to simply make the component invisible:
component.setVisible(false);
panel.revalidate();
panel.repaint(); // this is only required sometimes
All layout managers resize dynamically. However, the width and height properties are the result of the layouting, and will be overwritten.
The properties you should look at are preferredSize, minimumSize, and maximumSize - the layout managers base their calculations on those properties, though the exact effect depends on the layout manager (e.g. BorderLayout will give the NORTH, SOUTH, WEST and EAST components their preferred size if possibe and assign the remainder to the CENTER component).
Once you've changed the size properties, you have to call revalidate() on the container, then you should see the changes.
I'm with the revalidate()/preferredSize answers but just wanted to suggest this: don't re-invent the wheel! Use the JideSplitPane (part of JIDE's free "Common Layer") - it supports more than two splits.
Thanks to all for the answers. Finally I ended up with combining solutions from several answers.
My final solution is following:
I use BorderLayout, set West, Center and East panels and then manipulate their sizes by setting PreferredSize to West and East panels. The scheme of rendering is following: while laying out the components BorderLayout gives East and West panels their PreferredSize and rest of the space to Center panel. So with a bit of easy calculations I can manipulate size of each of three panels painlessly.
I also added dividers(originally just JPanel components with fixed size) to West and East panels(their size is also considered while calculating). For dynamic resize I handle dragging events on this dividers and recalculate panel sizes.
Refreshing is done with following snippet:
container.setVisible(false);
container.revalidate();
container.repaint();
container.setVisible(true);
I'd like to put this code somewhere to be available for others, but don't know where exactly to do this. So if you know such place, please point me to it in the comments.