I was wondering how would I add a JPanel to a JFrame and make sure it is centered and had a gap of set size on either sides? I can get it in the center but it sticks to the edge.
use a Border layout. Put the JPanel in the center, then use Box.createHorizontalStrut(size) and Box.createVerticalStrut(size) in the east/west and north/south locations, respectively.
something like: (from memory, might not be exactly right)
JPanel panel = new JPanel();
int gap = 20; //or whatever
frame.getContentFrame().setLayout(new BorderLayout());
frame.add(panel, BorderLayout.CENTER);
panel.add(Box.createHorizontalStrut(gap), BorderLayout.EAST);
panel.add(Box.createHorizontalStrut(gap), BorderLayout.WEST);
panel.add(Box.createVerticalStrut(gap), BorderLayout.NORTH);
panel.add(Box.createVerticalStrut(gap), BorderLayout.SOUTH);
Related
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.
I am new to Java GUI stuff and currently working on creating a microwave GUI. I am trying to place a panel within a panel but it's not really working as I think it should. I am using BorderLayout and basically putting a panel into BorderLayout.WEST and putting 2 panels within it (NORTH and CENTER). But somehow I can't add a panel to its CENTER, it seems like its going to NORTH all the time. Please see my code below.
private void buildFrame()
{
ovenLabel=new JLabel("Oven Area - Food goes here");
add(ovenLabel, BorderLayout.CENTER);
ovenLabel.setBackground(Color.white);
ovenLabel.setOpaque(true);
ovenLabel.setHorizontalAlignment(JLabel.LEFT);
ovenLabel.setVerticalAlignment(JLabel.TOP);
ovenLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 5));
//sidePanel
side=new JPanel();
add(side, BorderLayout.EAST);
side.setBorder(BorderFactory.createLineBorder(Color.BLACK, 5)); //remove this later
//top panel
sideTop=new JPanel();
sideTop.setBackground(Color.white);
sideTop.setOpaque(true);
sideTop.setLayout(new GridLayout(3, 1));
side.add(sideTop, BorderLayout.NORTH);
displayLabel=new JLabel[3];
for(int i=0;i<displayLabel.length; i++)
{
displayLabel[i]=new JLabel("");
sideTop.add(displayLabel[i]);
}
displayLabel[0].setText("Cooking time is displayed here. . .");
//bottom panel
sideBot=new JPanel();
sideBot.setLayout(new GridLayout(5, 3));
side.add(sideBot, BorderLayout.CENTER);
buttons=new JButton[15];
for(int i=0;i<buttons.length;i++)
{
buttons[i]=new JButton("test");
sideBot.add(buttons[i]);
}
//button
}
If you want to use a BorderLayout , make sure that the container has BorderLayout as its layout manager, or the constraints (NORTH, etc..) will not be understood as they should :
side.setLayout(new BorderLayout());
(do this before adding anything to it)
If you call :
System.out.println(side.getLayout());
before doing the modification, you will see that your panel had a FlowLayout by default.
I am making a copy of the apple calendar application, and I am having trouble aligning the month name and year name with the center of my screen, while aligning the left and right buttons with the left and right sides of the screen. Here is my code:
final JPanel months = new JPanel();
months.setLayout(new BoxLayout(months,BoxLayout.X_AXIS));
months.add(back, BorderLayout.WEST); //back is a JButton
JLabel monthName = new JLabel(this.monthNames[this.month]+" ", SwingConstants.CENTER);
JLabel year = new JLabel("" + this.year, SwingConstants.CENTER);
monthName.setFont(new Font("Helvetica", 0, 24));
year.setFont(new Font("Helvetica", 0, 24));
monthName.setHorizontalAlignment(JLabel.CENTER);
months.add(monthName, BorderLayout.CENTER);
months.add(year, BorderLayout.CENTER);
months.add(front, BorderLayout.EAST);
add(months);
Yet it shows up like this:
months.setLayout(new BoxLayout(months,BoxLayout.X_AXIS));
You are using a BoxLayout. A BoxLayout just adds the components horizontally to the panel. The WEST, CENTER, EAST constrains are only used by a BorderLayout so they are ignored by the BoxLayout.
months.add(monthName, BorderLayout.CENTER);
months.add(year, BorderLayout.CENTER);
When using a BorderLayout you can only add a single component to a region of the layout. So if you want to add two components to the CENTER you need to first create a panel and add the components to the panel.
So your basic code might be something like:
JPanel centerPanel = new JPanel();
centerPanel.add(month);
centerPanel.add(year);
JPanel mainPanel = new JPanel( new BorderLayout() );
mainPanel.add(westButton, BorderLayout.WEST);
mainPanel.add(centerPanel, BorderLayout.CENTER);
mainPanel.add(eastButton, BorderLayout.EAST);
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));
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?