0

Okay, so I need your help guys. I don't know what I missed but the insets and anchor is not taking effect even though I've set the layout to GridBag.

I need to put the logout button just above the tabbedpane and position the logout button on the upper right hand corner. In other words, tabbed pane on position gridx = 0, gridy = 1; and logout Button on position gridx = 0, gridy = 0;

Also, the myaccount button, leftpanel and rightpanel which are inside the home panel, won't get the insets i applied.

What am I missing. Please help because I'm new to this layout.

TopPanel.java

package MainComponents;

import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.border.Border;
import MainTab_TabbedPane.TopTabbedPane;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;

public class TopPanel extends JPanel {
//DECLARATION
JButton logOutButton = new JButton("Logout");
TopTabbedPane topTabbedPane = new TopTabbedPane();
private final Border myLineBorder = BorderFactory.createLineBorder(Color.BLACK, 2);

//CONSTRUCTOR    
public TopPanel(){
    setPanelInitialProperties();
    addComponents();
}

//METHODS
private void setPanelInitialProperties(){
    setLayout(new GridBagLayout());
    setBorder(myLineBorder); //sets a Line Border for this panel
}

private void addComponents(){
    GridBagConstraints gbc = new GridBagConstraints();

    gbc.gridx = 0;
    gbc.gridy = 1;
    this.add(topTabbedPane); //adds TabbedPane holding Home, Administration... to this Top Panel

    gbc.gridx = 0;
    gbc.gridy = 0;
    this.add(logOutButton);
}

}

HomeTopPanel.java

package HomeTab;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.border.Border;

public class HomeTopPanel extends JPanel {

private final JButton MyAccountButton = new JButton("My Account");
private final JPanel leftPanel = new JPanel(new GridBagLayout());
private final JPanel rightPanel = new JPanel(new GridBagLayout());
private final Border leftPanelLineBorder =   BorderFactory.createLineBorder(Color.BLACK, 2);
private final Border rightPanelLineBorder = BorderFactory.createLineBorder(Color.BLACK, 2);

//CONSTRUCTOR
public HomeTopPanel(){
    constructMyAccountButton();
    constructPanels();
    setLeftRightPanelBorders();
    this.setLayout(new GridBagLayout());
}

private void constructMyAccountButton(){
    GridBagConstraints MyAccountButton_gbc = new GridBagConstraints();
    MyAccountButton_gbc.gridx = 0; MyAccountButton_gbc.gridy = 0; 
    MyAccountButton_gbc.anchor = GridBagConstraints.NORTHWEST;
    this.add(MyAccountButton);
}

private void constructPanels(){
    GridBagConstraints leftPanelgbc = new GridBagConstraints();
    GridBagConstraints rightPanelgbc = new GridBagConstraints();

    leftPanelgbc.insets = new Insets(3,3,3,3);
    leftPanelgbc.gridx = 1; leftPanelgbc.gridy = 0;
    leftPanel.setPreferredSize(new Dimension(300, 500));
    this.add(leftPanel);

    rightPanelgbc.insets = new Insets(3,3,3,3);
    rightPanelgbc.gridx = 2; leftPanelgbc.gridy = 0;
    rightPanel.setPreferredSize(new Dimension(300, 500));
    this.add(rightPanel);

}

private void setLeftRightPanelBorders(){
    leftPanel.setBorder(leftPanelLineBorder);
    rightPanel.setBorder(rightPanelLineBorder);
    this.setBorder(leftPanelLineBorder);
}
}

Thanks in advanced. I'm sure there's something I missed but I don't know.

enter image description here

INSETS won't apply. =( ??

UPDATE:

I added the insets with the following code:

private void constructPanels(){
    GridBagConstraints gbc2 = new GridBagConstraints();

    gbc2.gridx = 1; gbc2.gridy = 0; 
    gbc2.insets = new Insets(5, 5, 5, 5);
    leftPanel.setPreferredSize(new Dimension(250, 300));
        this.add(leftPanel,gbc2);

    gbc2.gridx = 2; gbc2.gridy = 0; 
    gbc2.insets = new Insets(5, 5, 5, 5);    
    rightPanel.setPreferredSize(new Dimension(300, 500));
        this.add(rightPanel,gbc2);

}

but still not getting any inset of 5.

enter image description here

heisenberg
  • 1,784
  • 4
  • 33
  • 62

2 Answers2

2

Apply the constraints when adding components

add(topTabbedPane, gbc);
Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Hi, @Reimeus. It fixed the problem with the positioning of Logout. However, the insets I applied within the home panel won't take effect. – heisenberg Jan 10 '16 at 17:59
  • the same applys everywhere you add a component where `GridBagLayout` is used – Reimeus Jan 10 '16 at 18:07
2
GridBagConstraints MyAccountButton_gbc = new GridBagConstraints();

Variable names should NOT start with an upper case character. Most of your other names are correct. Then is no reason to be sloppy. Follow the Java conventions.

constructMyAccountButton();
constructPanels();
setLeftRightPanelBorders();
this.setLayout(new GridBagLayout());

The layout must be set BEFORE you add components to the panel.

GridBagConstraints MyAccountButton_gbc = new GridBagConstraints();
MyAccountButton_gbc.gridx = 0; MyAccountButton_gbc.gridy = 0; 
MyAccountButton_gbc.anchor = GridBagConstraints.NORTHWEST;
//this.add(MyAccountButton); // where is the constraint?
this.add(MyAccountButton, myAccountButton_gbc);

You actually have to use the constraint.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • thanks for your advice. However, the insets i added for HomeTopPanel.java won't apply. private void constructPanels(){ GridBagConstraints gbc2 = new GridBagConstraints(); gbc2.insets = new Insets(5, 5, 5, 5); leftPanel.setPreferredSize(new Dimension(250, 300)); this.add(leftPanel,gbc2); rightPanel.setPreferredSize(new Dimension(300, 500)); this.add(rightPanel,gbc2); } – heisenberg Jan 10 '16 at 18:34
  • @p3ace, You have been given plenty of help. Start with something simple. Create a JFrame with two panels and get your insets working. Then add more components. Don't attempt to create the entire GUI at once when you are learning a new concept. Break it down into small steps. If you still have a problem then post a proper [SSCCE](http://sscce.org/) that demonstrates the problem. The code you posted now is completely irrelevant given all the changes you have made based on our suggestions. – camickr Jan 10 '16 at 18:48
  • I understand and thanks. I'll take your advice. I'm just getting really confused because I tried adding insets on another simple frame earlier, but now can't get it to apply. But I appreciate the help. I'll continue working on this. – heisenberg Jan 10 '16 at 18:54