Results 1 to 5 of 5
  1. #1
    Wasp is offline Member
    Join Date
    Nov 2016
    Posts
    21
    Rep Power
    0

    Default Swing GridBagLayout Problem

    I did gridbagconstrains anchor NORTHWEST but its still runing CENTER. I try First Line Start too but still no changes. Help me.

    Java Code:
    package studentMVC.view;
    
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTabbedPane;
    
    
    public class AdminInputFrame  {
    	
    	private JFrame frame;
    	JTabbedPane tabbedPane;
    	
    	public AdminInputFrame(){
    		adminInput();
    	}
    	
    	private void adminInput() {
    		
    		frame = new JFrame("Admin Sayfası");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		{
    			tabbedPane = new JTabbedPane();
    			studentTab();
    			teacherTab();
    			adminTab();
    			settingTab();
    			calcelTab();
    			
    			frame.add(tabbedPane);
    		}
    		frame.setSize(800, 600);
    		frame.setResizable(false);
    		frame.setLocationRelativeTo(null);
    	}
    	
    	public void show() {
    		frame.setVisible(true);
    	}
    	
    	public void studentTab(){
    		JPanel panel = new JPanel(new GridBagLayout());
    		GridBagConstraints gridBagConstraints = new GridBagConstraints();
    		gridBagConstraints.insets = new Insets(5, 5, 5, 5);
    		gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
    		{
    			panel = new JPanel(new GridBagLayout());
    			{
    				JLabel label = new JLabel("ğrenci Ekle");
    				gridBagConstraints.gridx = 0;
    				gridBagConstraints.gridy = 0;
    				panel.add(label, gridBagConstraints);
    			}
    			{
    				JLabel label = new JLabel("TC Numarası");
    				gridBagConstraints.gridx = 0;
    				gridBagConstraints.gridy = 1;
    				panel.add(label, gridBagConstraints);
    			}
    			tabbedPane.addTab("ğrenci Ekle-Sil", panel);
    		}
    	}
    	
    	public void teacherTab(){
    		JPanel panel = new JPanel(new GridBagLayout());
    		GridBagConstraints gridBagConstraints = new GridBagConstraints();
    		gridBagConstraints.insets = new Insets(5, 5, 5, 5);
    		gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
    		{
    			panel = new JPanel(new GridBagLayout());
    			{
    				JLabel label = new JLabel("ğrenci Ekle");
    				gridBagConstraints.gridx = 0;
    				gridBagConstraints.gridy = 0;
    				panel.add(label, gridBagConstraints);
    			}
    			{
    				JLabel label = new JLabel("TC Numarası");
    				gridBagConstraints.gridx = 0;
    				gridBagConstraints.gridy = 1;
    				panel.add(label, gridBagConstraints);
    			}
    			tabbedPane.addTab("ğrenci Ekle-Sil", panel);
    		}
    	}
    	
    	public void adminTab(){
    		JPanel panel = new JPanel(new GridBagLayout());
    		GridBagConstraints gridBagConstraints = new GridBagConstraints();
    		gridBagConstraints.insets = new Insets(5, 5, 5, 5);
    		gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
    		{
    			panel = new JPanel(new GridBagLayout());
    			{
    				JLabel label = new JLabel("ğrenci Ekle");
    				gridBagConstraints.gridx = 0;
    				gridBagConstraints.gridy = 0;
    				panel.add(label, gridBagConstraints);
    			}
    			{
    				JLabel label = new JLabel("TC Numarası");
    				gridBagConstraints.gridx = 0;
    				gridBagConstraints.gridy = 1;
    				panel.add(label, gridBagConstraints);
    			}
    			tabbedPane.addTab("ğrenci Ekle-Sil", panel);
    		}
    	}
    	
    	public void settingTab(){
    		JPanel panel = new JPanel(new GridBagLayout());
    		GridBagConstraints gridBagConstraints = new GridBagConstraints();
    		gridBagConstraints.insets = new Insets(5, 5, 5, 5);
    		gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
    		{
    			panel = new JPanel(new GridBagLayout());
    			{
    				JLabel label = new JLabel("ğrenci Ekle");
    				gridBagConstraints.gridx = 0;
    				gridBagConstraints.gridy = 0;
    				panel.add(label, gridBagConstraints);
    			}
    			{
    				JLabel label = new JLabel("TC Numarası");
    				gridBagConstraints.gridx = 0;
    				gridBagConstraints.gridy = 1;
    				panel.add(label, gridBagConstraints);
    			}
    			tabbedPane.addTab("ğrenci Ekle-Sil", panel);
    		}
    	}
    	
    	public void calcelTab(){
    		JPanel panel = new JPanel(new GridBagLayout());
    		GridBagConstraints gridBagConstraints = new GridBagConstraints();
    		gridBagConstraints.insets = new Insets(5, 5, 5, 5);
    		gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
    		{
    			panel = new JPanel(new GridBagLayout());
    			{
    				JLabel label = new JLabel("ğrenci Ekle");
    				gridBagConstraints.gridx = 0;
    				gridBagConstraints.gridy = 0;
    				panel.add(label, gridBagConstraints);
    			}
    			{
    				JLabel label = new JLabel("TC Numarası");
    				gridBagConstraints.gridx = 0;
    				gridBagConstraints.gridy = 1;
    				panel.add(label, gridBagConstraints);
    			}
    			tabbedPane.addTab("ğrenci Ekle-Sil", panel);
    		}
    	}
    
    	
    	public static void main(String[] args) {
    		AdminInputFrame adminInputFrame = new AdminInputFrame();
    		adminInputFrame.show();
    	}
    
    	
    
    }

  2. #2
    Wasp is offline Member
    Join Date
    Nov 2016
    Posts
    21
    Rep Power
    0

    Default Re: Swing GridBagLayout Problem

    Any help?

  3. #3
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,722
    Rep Power
    6

    Default Re: Swing GridBagLayout Problem

    It's not running CENTER, but NORTWEST. Why? Because weightx and weighty are 0 everywhere. This means all the extra space will be distributed around all the components. If you add the code below to the last label in the tab, the remaining space of the tab will be added to the last label only, effectively shrinking the other labels.
    Java Code:
    gridBagConstraints.weightx= 1;
    gridBagConstraints.weighty= 1;
    Shameless plug: please give MigLayout a try. Usage is way way easier than GridBagLayout. Also, JGoodies FormLayout is good too. GridBagLayout is good as an exercise. It helps you find out that the GridBagLayout is shit to code by hand. That's where the modern LayoutManagers like MigLayout and FormLayout come in.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    Wasp is offline Member
    Join Date
    Nov 2016
    Posts
    21
    Rep Power
    0

    Default Re: Swing GridBagLayout Problem

    I am still didnt get gridbaglayout how to work. frame.pack() its okey but frame.size iget confused. I studied MigLayout its look esiasier. Now i will try this user panel with MigLayout. Thank you @SurfMan

  5. #5
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,722
    Rep Power
    6

    Default Re: Swing GridBagLayout Problem

    Remember that pack() resizes everything to its preferred size, including the frame itself. You either set size of use pack(), but not both. If you use setSize(600,400) and then call pack(), the frame will be resized if necessary effectively overruling setSize().
    Last edited by SurfMan; 01-03-2017 at 10:41 AM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Java Swing ( GridBagLayout )
    By Dex in forum AWT / Swing
    Replies: 2
    Last Post: 12-24-2012, 10:39 PM
  2. Problem with GridBagLayout.
    By orushw in forum AWT / Swing
    Replies: 1
    Last Post: 05-26-2012, 03:12 PM
  3. Trouble displaying an image using GridBagLayout
    By treeface99 in forum AWT / Swing
    Replies: 0
    Last Post: 03-15-2012, 10:52 PM
  4. GridBagLayout problem!
    By canersen in forum AWT / Swing
    Replies: 9
    Last Post: 03-07-2011, 12:35 AM
  5. Java Swing GridBagLayout Problem
    By hemanthjava in forum AWT / Swing
    Replies: 1
    Last Post: 06-29-2008, 08:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •