Add JScrollpane to JPanel containing multiple JPanels - java

I have one JFrame (black) that contains one main JPanel (grey), this JPanel contains three other JPanels : North and South (red) and the Center one (blue). The blue JPanel in the center will have lots of JPanels (green) added dynamically to it during the course of the program. When the Center JPanel is full I would like a JScrollbar to appear automatically to scroll down the Center Panel and see all the child (green) panels it contains. Can somebody help me? The problem is that the scrollbar isn't appearing at all, if i add 15 green JPanels to my blue JPanel container, i only see 10 and i can't scroll down.
This is the type of code i have tried so far...
JPanel panelNorth = new JPanel(new GridBagLayout());
panelNorth.setPreferredSize(new Dimension(1000,100);
//add some labels and buttons
JPanel panelCenter = new JPanel();
panelCenter.setLayout(new BoxLayout(panelCenter, BoxLayout.Y_AXIS));
panelCenter.setPreferredSize(new Dimension(1000,500)
JPanel panel1 = new JPanel(new GridLayout(1, 10));
panel1.setPreferredSize(new Dimension(1000,50));
panelCenter.add(panel1);
//...etc dynamically
JPanel panelSouth = new JPanel(new GridBagLayout());
panelSouth.setPreferredSize(new Dimension(1000,100);
//add some labels and buttons
JScrollPane scrollPaneCenter = new JScrollPane(panelCenter,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
//panelCenter must be scrollable when too many panels are added to panelCenter
//add everything to the main panel container
JPanel panelContainer = new JPanel(new BorderLayout(3,3));
panelContainer.setBorder(new EmptyBorder(5,5,5,5));
panelContainer.add(panelNorth,BorderLayout.NORTH);
panelContainer.add(scrollPaneCenter,BorderLayout.CENTER);
panelContainer.add(panelSouth,BorderLayout.SOUTH);
//add everything to frame
JFrame frame = new JFrame();
frame.add(panelContainer);
Thank you.
EDIT:
I changed
JScrollPane scrollPaneCenter = new JScrollPane(panelCenter,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
to this :
JScrollPane scrollPane = new JScrollPane(panelCentre,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
And this is what i get:
I can see it now, it's in the right place, i just can't scroll down to see my other components.

Fixed it by switching
panelCenter.setPreferredSize(new Dimension(1000,500);
with
scrollPane.setPreferredSize(new Dimension(1000,500));

Related

JPanel on top of another JPanel

I have been using JPanels for a while and now want to place a JPanel on top of another JPanel.
I have looked at using JLayer but I was wondering If there is a solution to just set the layer of the bottom and top, I don't want to set each components layer if possible.
Example
JPanel bottomPanel = new JPanel(); # Set as bottom panel
JPanel topPanel = new JPanel(); # Set as top panel
JPanel sidePanel = new JPanel(); # Don't have to set
JPanel anotherSidePanel = new JPanel(); # Don't have to set
If this isn't possible what is the best solution for this, Thanks.
You can have the main panel use a BorderLayout.
Then you can do something like:
mainPanel.add(leftSide, BorderLayout.LINE_START);
mainPanel.add(rightSide, BorderLayout.LINE_END);
JLayeredPane lp = new JLayeredPane();
mainPanel.add(lp, BorderLayout.CENTER);
It sounds like what you want is a layout manager. There are a few different ones that suit different needs. There's a link at the bottom of this post.
My personal favorite is GridLayout. So for what you want to do, you can do this:
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 1));
//the first number is the number of rows, the second is the number of columns
JPanel topPanel = new JPanel();
JPanel bottomPanel = new JPanel();
panel.add(topPanel);
panel.add(bottomPanel);
That will do what you want.
If you wanted to read more about them, here's a link:
Oracle Docs on Layout Managers
I know this is quite late, but if anyone now has this issue, I suggest using a BoxLayout. BorderLayout can only have one cell in each of its five locations, and GridLayout's cells are all the same dimension. If you want to stack different sized JPanels, here's how BoxLayout can be implemented:
JFrame frame = new JFrame("Intro to BoxLayout");
JPanel container = new JPanel();
JPanel panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(X1, Y1));
JPanel panel2 = new JPanel();
panel2.setPreferredSize(new Dimension(X2, Y2));
container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));
container.add(panel1);
container.add(panel2);
frame.add(container);
frame.pack();
frame.setVisible(true);
where X1, Y1, X2, Y2 are arbitrary panel dimensions.

Resizing the panels on gridlayout

I'm trying to make a simple ui game with Java swing. Here is my target layout design: (all panels have same width, excuse my drawing)
I need 3 small height panel and 1 large panel
I used GridLayout with 4x1. I added some buttons to first panel.
mainFrame = new JFrame("Basket Game");
mainFrame.setLayout(new GridLayout(4, 1));
options = new JPanel();
options.setLayout(new FlowLayout());
options.setBorder( new TitledBorder("Options Menu") );
options.add(settings);
options.add(start);
options.add(pause);
options.add(reset);
options.add(exit);
mainFrame.add(options);
But it makes the first panel too big.
How can I set a size for these panels or should I use different layout pattern.
With a GridLayout, all cells in the grid have the same size, that's why your panel has 1/4 of the total height.
You may consider using a vertical BoxLayout:
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
// add the panels to mainPanel, then
mainFrame.setContentPane(mainPanel);
Here is an example, with three panels containing one button each, and one panel having a bigger size :
JFrame frame = new JFrame();
JPanel p1 = new JPanel();
p1.add(new JButton("11111"));
JPanel p2 = new JPanel();
p2.add(new JButton("222222"));
JPanel p3 = new JPanel();
p3.add(new JButton("3333"));
JPanel p4 = new JPanel();
p4.setPreferredSize(new Dimension(50, 400));
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
mainPanel.add(p1);
mainPanel.add(p2);
mainPanel.add(p3);
mainPanel.add(p4);
frame.setContentPane(mainPanel);
frame.pack();
frame.setVisible(true);

Position of buttons in rows, in a JPanel based 'menu bar'

I'm trying Swing programming but I can't do what I want.
I would like to place a top bar button with 2 lines of button but I just have 1 line in my case.
Here is my code:
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
setMinimumSize(new Dimension(1000,500));
setMaximumSize(new Dimension(1000,500));
JPanel panelButton = new JPanel();
JPanel panelTopButton = new JPanel();
JPanel panelBottomButton = new JPanel();
panelTopButton.add(dashboard);
panelTopButton.add(journal);
panelTopButton.add(myPlans);
panelTopButton.add(myFavorites);
panelTopButton.add(shoppingCart);
panelBottomButton.add(profile);
panelBottomButton.add(notifications);
panelButton.add(panelTopButton, BorderLayout.NORTH);
panelButton.add(panelBottomButton, BorderLayout.SOUTH);
contentPane.add(panelButton,BorderLayout.NORTH);
//Display
setSize(400,120);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
I have this
And I want this
Can somebody help me?
You need one panel for each line.
Try to do this:
JPanel panelButtonsL1 = new JPanel();
JPanel panelButtonsL2 = new JPanel();
panelButtonsL1.add(dashboard);
panelButtonsL1.add(journal);
panelButtonsL1.add(myPlans);
panelButtonsL1.add(myFavorites);
panelButtonsL1.add(shoppingCart);
panelButtonsL2.add(profile);
panelButtonsL2.add(notifications);
The default layour of JPanel is FlowLayout. Bear in mind that layout is very important to work with swing component disposition.
Define the bottom panel as GridLayout.
JPanel panelButton = new JPanel(new GridLayout(2, 1)); // 2 rows x 1 column
panelButton.add(panelButtonsL1);
panelButton.add(panelButtonsL2);
Details of GridLayout you can find on API.
You can achieve that using a GridLayout: assign a GridLayout to panelButton with two rows and one column, and then add the two panels to it.
According to what you want there is a simpler alternative by continue using the default FlowLayout from the panel. It is more appropriate than using GridLayout since you wanted the last 2 buttons to move to the next row and center itself.
If you use GridLayout, the buttons at the next row are likely going to be directly below one of the buttons above. Here are 2 ways to get what you want.
Method 1. Reduce the width of the main panel holding your buttons:
Dosing so, you will have to add the main panel using BorderLayout.CENTER.
Method 2. Add the buttons to a sub-panel of smaller width and add it to the main panel. All your buttons will be added to the smaller sub-panel:

JPanel create empty space between subPanels

I'm developing a Java application for homework. This is my code
JLabel queryHandlerL = new JLabel("Create php to handle query results", JLabel.CENTER);
final JCheckBox queryHandlerCB = new JCheckBox();
JPanel checkBoxPanel = new JPanel(new FlowLayout());
checkBoxPanel.add(queryHandlerL);
checkBoxPanel.add(queryHandlerCB);
// Query Panel
// set image
picLabelQuery = new JLabel("",JLabel.LEFT);
picLabelQuery.setIcon(currentPicForm);
JPanel queryPanel = new JPanel();
final JButton queryButton = new JButton("Insert a query");
queryPanel.add(queryButton);
queryPanel.add(picLabelQuery);
// Panel create
final JButton createButton = new JButton("Create");
JPanel createPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
createPanel.add(createButton);
JPanel finalPanel = new JPanel(new GridLayout(0, 1,5,2));
finalPanel.add(queryPanel);
finalPanel.add(checkBoxPanel);
finalPanel.add(createPanel);
finalPanel.setBorder(BorderFactory.createTitledBorder("SQL connection"));
setLayout(new GridBagLayout());
add(finalPanel);
I have a CardLayout and this is a Window inside this CardLayout. The last add(finalPanel) refers to the panel of the CardLayout.
This piece of code works but this is the result
How do I remove the space that is automatically created between the panels?
How do I remove the space that is automatically created between the panels?
Use a different layout manager for the panel. The GridLayout will always resize components to take up all the space in the panel.
Maybe you can use a BoxLayout or a GridBagLayout. You can also nest panels with different layout managers to get your desired effect.
Read the section from the Swing tutorial on Layout Managers for more information and examples.
You should pack() your surrounding panel or set the height to a desired value.

wrong alignment with miglayout

In jframe, I use miglayout for main jpanel position.
in the left panel, I have 2 jpanel, I use boxlayout.
ComponentPanel is the top left position and PropertyPanel is at the bottom left position.
leftPanel = new JPanel();
leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
leftPanel.setMinimumSize(new Dimension(600, 600));
add(leftPanel, BorderLayout.WEST);
componentPanel = new ComponentPanel();
propertyPanel = new PropertyPanel();
in the propertyPanel constructor, i do
setLayout(new MigLayout("debug"));
i get this
why panel is setted to right?
if i add dynamically some space is added.
I tried to use fill to the miglayout constructor without success.
in green is the leftPanel
in red is the componentPanel
http://imagepaste.nullnetwork.net/img/1354548433miglayout3.jpg
Using BorderLayout instead of BoxLayout seem better...
leftPanel.setLayout(new BorderLayout());
leftPanel.add(componentPanel, BorderLayout.NORTH);
leftPanel.add(propertyPanel,BorderLayout.SOUTH);
BoxLayout problem?

Categories

Resources