Resizing LayeredPane Swing - java

I have a JPanel that uses BorderLayout and contains a jTable and a layeredPane. I need to use the layeredPane because it contains a jComboBox on top of a jTextPane. The problem is that when the user resizes the screen, the layeredPane doesn't resize. How can i fix this? I have something like this:
JPanel panel = new JPanel(new Borderlayout());
MyTable table = new MyTable();
panel.add(table, BorderLayout.CENTER);
JLayeredPane layeredPane = new JLayeredPane();
//addind things in the layered pane
panel.add(layeredPane, BorderLayout.PAGE_END);
Using a layout manager for the layered pane won't work, because i need components to be overlapped.

In the example you have posted JLayeredPane takes the SOUTH (BorderLayout.PAGE_END) place in the panel with BorderLayout. That means that the component in CENTER will take all additional space offered to the panel. And you have your table in CENTER.
Just place JLayeredPane in the CENTER and table in the NORTH to let the pane take all additional space.
More on BorderLayout:
http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html
You also might want to read more on Swing layouts:
http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html

common issue about JLayeredPane is that its childs would be placed by setBounds, because doesn't supports Standards LayoutManagers excluding Null Layout
have to add ComponentListener on event componentResized to change JPanels Bounds
for JComponents placed to the JPanel to use Standard LayoutManager
more than an alternative is to use the proper JLayer for Java7 based on custom JXLayer for Java6

Related

How to set size of JScrollPane according to JFrame size?

I have been working on a JFrame, which contains a panel of multiple components. However, the frame cannot accommodate all components due to its size. I wish to use a scrollable frame, and came across a link where JScrollPane use is suggested. However, when the frame is resized, the scroll pane container does not take up the size of the resized frame.
Is there a way to achieve this?
By default a JFrame uses a BorderLayout. When you add a component to the CENTER of the BorderLayout the component will take all the available space of the frame.
So the basic logic is:
JPanel panel = new JPanel(...);
JScrollPane scrollPane = new JScrollPane( panel );
frame.add(scrollPane, BorderLayout.CENTER);
Set the frame to use grid layout. Gridlayout makes all of the components in the frame use up the entire size.
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(1,1));

set layout on top left of the JPanel instead of center

Say I have a GridLayout in a JPanel, if my layout isn't big enought to fill the whole JPanel, it will auto align it at the center of ther JPanel like:
But I want my layout to align it on top left of the JPanel like:
so my component will start on top left of the panel, is there any method to do it?
Add your gridLayoutPanel to another panel i.e. subPanel. Set this subPanel layout to the flow layout like subPanel.setLayout(new FlowLayout(FlowLayout.LEFT)). Than set your main panel's layout to BorderLayout. And add your subPanel panel to the NORTH position of the main panel.
Example code:
JPanel subPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
subPanel.add(gridLayoutPanel);
mainPanel.setLayout(new BorderLayout());
mainPanel.add(subPanel, BorderLayout.NORTH);
it will auto align it at the center of ther JPanel like:
Sounds like you are using a GridBagLayout on the main panel and this is the default behaviour unless you use the GridBagConstraints to control this behaviour.
Read the section from the Swing tutorial on How to Use GridBagLayout, especially the section on using the weightx/y constraints. Setting these constraints correctly will prevent the panel from centering itself.

Resizable scrollpane to an awt frame

I need to add a scrollable JPanel to a AWT frame that can scale when the frame is re-sized. The scrollpane appears when I set it a fixed size. But I need the panel to cover the whole frame and re-size automatically when I re-adjust the frame size.
Composite composite = new Composite(parent, SWT.EMBEDDED | SWT.NO_BACKGROUND);
Frame frame = SWT_AWT.new_Frame(composite);
SimulationPanel simPanel= new SimulationPanel(WIDTH,simPanelTotalHeight);
JScrollPane scrollPane = new JScrollPane(simPanel);
scrollPane.setSize(new Dimension(500,400));
JPanel contentPane = new JPanel(null);
contentPane.add(scrollPane);
frame.add(contentPane);
Don't set the JScrollPane's size or its preferredSize. Either can mess with its ability to re-size if needed.
Instead add it to a container using a layout that allows it to re-size with the container. BorderLayout comes to mind. Make sure that the container hierarchy also attaches to the top-level window in some decent way using a smart combination of layout managers. This is one of the key reasons to understand well and use the layout managers.
Don't use null layout, and in fact that is one thing that is messing up your code.
As a side issue: Why use AWT's Frame and not Swing's JFrame? This doesn't make much sense.

Why am I not getting a functioning JScrollPane?

I have a JFrame window, and I'd like to add a scrollable JTable towards the middle of it. I have a method, called collectionTableScrollPane() that generates the JScrollPane (and I know this is guaranteed to work).
I then proceed to add it to my mainPanel panel. However, I'd like there to be some forced 30px padding on the left and right of the JScrollPane. Logically, I would create a holding JPanel with a centred FlowLayout, and add Box.createHorizontalStrut(30) either side of the JScrollPane.
JPanel tableHolderPanel = new JPanel(new FlowLayout());
mainPanel.add(tableHolderPanel);
tableHolderPanel.add(Box.createHorizontalStrut(30));
tableHolderPanel.add(collectionTableScrollPane());
tableHolderPanel.add(Box.createHorizontalStrut(30));
However, I'm getting a strange result, where the JScrollPane in the middle of the window (denoted by the arrows) sort of becomes ineffectual.
Does anyone know what the problem is?
Note that the JTable contains four rows, of which only two are visible.
I had some issues in the past when i used a JScrollPane inside a panel with a FlowLayout. The behaviour could be tricky, when the content grow, the horizontal scrollbar may appear or the FlowLayout should add a new line.
In your case, i will replace the FlowLayout by a BorderLayout :
JPanel tableHolderPanel = new JPanel(new BorderLayout());
mainPanel.add(tableHolderPanel);
tableHolderPanel.add(Box.createHorizontalStrut(30), BorderLayout.WEST);
tableHolderPanel.add(collectionTableScrollPane(), BorderLayout.CENTER);
tableHolderPanel.add(Box.createHorizontalStrut(30), BorderLayout.EAST);
As far as I'm aware, Box is suppose to be used with the BoxLayout, this may be causing you some issues. Instead, why not use a EmptyBorder on the tableHolderPane
BoxLayout accepting size that came from JComponents, the same issue with default FlowLayout pre_implemented for JPanel
you have to returns PreferredSize by overrode JPanel nested JScrollPane,
use another LayoutManager, e.g. GridBagLayout or todays MigLayout
use NestedLayout, by using BorderLayout where you put two JLabels (e.i. that returns PreferredSize) to the EAST and WEST area
everything depends if you really to want to create the empty area and if shoud be resiziable or not

A simple two column layout with Swing

How to get a sidebar JPanel of a fixed with with Swing. Now I'm trying this:
public SidebarPanel() {
this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
this.setPreferredSize(new Dimension(200, this.getPreferredSize().height));
...
But when I resize the window also the width of the sidebar changes. How to fix this?
There is no guarantee in Swing that the preferred size is respected. This depends on the layout manager of the container.
If the container uses a BorderLayout, you can add the SidebarPanel using:
container.add(sidebarPanel, BorderLayout.EAST)
This will respect the preferred width.
You'll have to rely upon the good old GridBagLayout. A good helper class is the GBC, which will allow you to position elements with ease.
You can use BorderLayout, adding to JPanel
mainContent.setLayout(new BorderLayout());
mainContent.add(pnlHeader, BorderLayout.WEST);
mainContent.add(pnlResultList, BorderLayout.EAST);

Categories