Reputation: 708
I am having a JPanel that is added to a JFrame. The panel have many buttons added vertically. when i set border to the panel, the size of buttons are getting smaller and layout is going off.
Can anyone advise why the layout is impacted because of the border setting?
Following is the code for the above design
frame = new JFrame();
//frame.setBounds(100, 100, 450, 300);
frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout(0, 0));
JPanel actionPanel = new JPanel();
actionPanel.setPreferredSize(new Dimension(200, 450));
actionPanel.setBackground(Color.BLUE);
frame.getContentPane().add(actionPanel, BorderLayout.WEST);
actionPanel.setLayout(new GridLayout(2, 1, 0, 0));
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
//panel.setBackground(Color.GRAY);
actionPanel.add(panel);
panel.setLayout(new GridBagLayout());
//GridbagLayout constraints
GridBagConstraints gbc = new GridBagConstraints();
gbc.weighty = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
JButton btnPos = new JButton("Point of Sale");
Dimension btnPosDimension = new Dimension(actionPanel.getPreferredSize().width, 40);
btnPos.setPreferredSize(btnPosDimension);
panel.add(btnPos, gbc);
gbc.gridy++;
JButton btnCategory = new JButton("Category");
btnCategory.setPreferredSize(btnPosDimension);
panel.add(btnCategory, gbc);
gbc.gridy++;
JButton btnProduct = new JButton("Product");
btnProduct.setPreferredSize(btnPosDimension);
panel.add(btnProduct, gbc);
gbc.gridy++;
JButton btnVendor = new JButton("Vendor");
btnVendor.setPreferredSize(btnPosDimension);
panel.add(btnVendor, gbc);
gbc.weighty = 1;
gbc.gridy++;
panel.add(Box.createGlue(), gbc); //Adding a component to feel the area.
JPanel panel_1 = new JPanel();
actionPanel.add(panel_1);
JPanel contentPanel = new JPanel();
frame.getContentPane().add(contentPanel, BorderLayout.CENTER);
Upvotes: 0
Views: 686
Reputation: 3956
I just reduced the buttons' width 2. because that 2 using by line border.
This is the working code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Example {
public static void main(String[] args) {
JFrame frame = new JFrame();
//frame.setBounds(100, 100, 450, 300);
frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new BorderLayout(0, 0));
JPanel actionPanel = new JPanel();
actionPanel.setPreferredSize(new Dimension(200, 450));
actionPanel.setBackground(Color.BLUE);
frame.getContentPane().add(actionPanel, BorderLayout.WEST);
actionPanel.setLayout(new GridLayout(2, 1, 0, 0));
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
//panel.setBackground(Color.GRAY);
actionPanel.add(panel);
panel.setLayout(new GridBagLayout());
//GridbagLayout constraints
GridBagConstraints gbc = new GridBagConstraints();
gbc.weighty = 0;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
JButton btnPos = new JButton("Point of Sale");
Dimension btnPosDimension = new Dimension(actionPanel.getPreferredSize().width - 2, 40);
btnPos.setPreferredSize(btnPosDimension);
panel.add(btnPos, gbc);
gbc.gridy++;
JButton btnCategory = new JButton("Category");
btnCategory.setPreferredSize(btnPosDimension);
panel.add(btnCategory, gbc);
gbc.gridy++;
JButton btnProduct = new JButton("Product");
btnProduct.setPreferredSize(btnPosDimension);
panel.add(btnProduct, gbc);
gbc.gridy++;
JButton btnVendor = new JButton("Vendor");
btnVendor.setPreferredSize(btnPosDimension);
panel.add(btnVendor, gbc);
gbc.weighty = 1;
gbc.gridy++;
panel.add(Box.createGlue(), gbc); //Adding a component to feel the area.
JPanel panel_1 = new JPanel();
actionPanel.add(panel_1);
JPanel contentPanel = new JPanel();
frame.getContentPane().add(contentPanel, BorderLayout.CENTER);
frame.setVisible(true);
}
}
Upvotes: 1
Reputation: 77
Borders are added to the inside of a JPanel so there is less space available inside. You have specified the size of the container that holds your panel, so when the border is added there isn't enough space for the contents.
Upvotes: 2