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);
Related
I'm using GridLayout and all my panels have the same size, how can I change their size?
I tried all functions getPreferred/Minimum/MaximumSize which makes that, and nothing.
I still want to stay with GridLayout
Any suggestions?
By its very nature, all components held by a GridLayout will have the same size. If you want them to be different sizes,
You can use a different layout, such as a GridBagLayout or MigLayout or
Use the GridLayout to hold same-sized JPanels that act as containers for other components. The inner components of course can be different sizes. For example: a chess board that holds its chess cells in a GridLayout, but that allows each cell to hold a chess piece that has varying sizes.
If this doesn't answer your question, then please clarify your question.
So I managed to split all the part above to my first JPanel which located to NORTH.
Then, the JPanel with the button I did the same to be located SOUTH.
So the scrolled JLabel in now located in the CENTER which allows him to be flexible.
JPanel mainPanel1 = new JPanel(new GridLayout(6,1));
mainPanel1.add(titleLabel);
mainPanel1.add(participantPanel);
mainPanel1.add(swimPanel);
mainPanel1.add(ridePanel);
mainPanel1.add(runPanel);
mainPanel1.add(categoriesPanel);
JPanel mainPanel2 = new JPanel(new GridLayout(1,1));
mainPanel2.add(listPanel);
JPanel mainPanel3 = new JPanel(new GridLayout(1,1));
mainPanel3.add(buttonsPanel);
this.getContentPane().add(mainPanel1, BorderLayout.NORTH);
this.getContentPane().add(mainPanel2, BorderLayout.CENTER);
this.getContentPane().add(mainPanel3, BorderLayout.SOUTH);
I have a problem because I want to put a small JPanel inside a different JPanel, but I can't get the small JPanel to show.
What am I missing?
this.setLayout(new BorderLayout(5,5));
this.cardsPanel= new JPanel();
this.cardsPanel.setBackground(Color.DARK_GRAY);
this.cardsPanel.setLayout(new FlowLayout (FlowLayout.CENTER,3,10));
this.cardsPanel2= new JPanel();
this.cardsPanel2.setBackground(Color.DARK_GRAY);
this.cardsPanel2.setLayout(new FlowLayout (FlowLayout.CENTER,3,10));
this.tablePanel=new JPanel();
this.tablePanel.setBackground(Color.PINK);
this.tablePanel.setLayout(new FlowLayout (FlowLayout.CENTER,5,5));
this.tablePanel1=new JPanel();
this.tablePanel1.setBackground(Color.ORANGE);
this.tablePanel1.setPreferredSize(new Dimension(100,100));
// this.tablePanel1.setLayout(null);
this.tablePanel1.add(tablePanel);
this.add(cardsPanel, BorderLayout.SOUTH);
this.add(cardsPanel2, BorderLayout.NORTH);
this.add(tablePanel,BorderLayout.CENTER);
Using that code:
Neither cardsPanel1 nor cardsPanel2 (dark gray) have any components (or therefore) any size. At 0x0 pixels size, they are invisible even when added to another container (unless the layout stretches them).
tablePanel (pink) should be visible so long as this is assigned enough space in whatever layout it is added to.
tablePanel1 (orange) should not be visible as it is never added to anything.
But as mentioned, for better help sooner, post an MCVE (Minimal Complete and Verifiable Example).
this.validate(); your root panel after adding new content.
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
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
In a project of mine I have been trying to add a JConsonle to a JPanel witch is contained by another JPanel.
The problem is that the JConsole keeps being set 5px from the top of the JPanel.At first I tought it was the container that wasent beeing set up right but after giving it a red background I realised that the console is being set 5px from the top.
I've also tried to use BorderLayout to set it in the NORTH or CENTER of the JPanel but that
dosent work either.
This is my code:
public class MonopolyPanel extends JPanel {
JPanel consoleP = new JPanel();
JConsole console = new JConsole();
MonopolyPanel(){
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
console.setPreferredSize(new Dimension(530, 300));
consoleP.add(console);
this.add(consoleP);
}
}
The console is added to consoleP which has FlowLayout by default, which by default has a vertical and horizontal gap of 5px. Instantiating that with a FlowLayout with zero gaps should do the trick
consoleP == new JPanel(new FlowLayout(align, 0, 0));
From my experience, the best and most flexible layout is GridBagLayout.
99% of panels in Swing I make are GridBagLayout, otherwise its impossible to get it all right.
Because with that layout, you can set weights, exact padding and spacing parameters.
The other layoutmanagers are to limited and not very configurable.
Have you looked at Borders?
It's possible you need to set the JPanel's or the JConsole's border to an EmptyBorder, like so:
component.setBorder(BorderFactory.createEmptyBorder());
You might also look for something about insets in the Javadocs.