Results 1 to 2 of 2

Thread: GridBagLayout

  1. #1
    Join Date
    Oct 2016
    Location
    Sumner, WA
    Posts
    4
    Rep Power
    0

    Default GridBagLayout

    Well, it finally came that time where Absolute positioning came back to bite me in the arse. So, looking through the java docs GridBagLayout was my only option that would work for this specific program. I am a bit unfamiliar with - well oblivious to - GridBagLayout but, that didn't stop me from trying. I have it sort of working and I've been reading what each one does but, I am having a difficult time comprehending how I need to use these variables to position my components. At the moment I am trying to position my label in the first column centered. Then, the second row position a label and text field but, I can't seem to get the text field to position right after the search label. Lastly, my Menu button should be at the very bottom stretching across the bottom of the screen.

    GridBagLayout-app.png

    Here is my code:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.ComponentOrientation;
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SpringLayout.Constraints;
    
    public class Menu extends JFrame implements ActionListener{
    	
    	private static final long serialVersionUID = 1L;
    	
    	// Makes 4 button components
    	private JButton buttonMenu = createButton("Menu");
    	private JButton buttonRelics = createButton("Relics");
    	private JButton buttonNews = createButton("News");
    	private JButton buttonExit = createButton("Exit");
    	
    	// Makes 2 panel components
    	private JPanel panelRelics = createPanel();
    	private JPanel panelNews = createPanel();
    	
    	// Makes a JLabel component and references the image to use as a background
    	private JLabel labelBackground = new JLabel(new ImageIcon("C:/Users/jo/Documents/eclipse/workspace/HeroSiegeRelics/src/Images/HeroSiegeBackground.png")); 
    	private JLabel labelHeroSiegeTextIcon = new JLabel(new ImageIcon("C:/Users/jo/Documents/eclipse/workspace/HeroSiegeRelics/bin/Images/HeroSiegeText.png"));
    	private JLabel labelSearch = new JLabel("Search: ");
    	
    	// Makes a Text Field to enter text
    	JTextField textField = new JTextField();
    	
    	private static GridBagConstraints constraints = new GridBagConstraints();
    	
    	// used to set window dimensions
    	private int width, height;
    	
    	private final static boolean shouldFill = true;
    	private final static boolean shouldWeightX = true;
    	private final static boolean RIGHT_TO_LEFT = false;
    	
    	Relics getRelics = new Relics();
    
    	
    	public Menu(int width, int height){
    		
    		// class variables are equal to the constructors variables in the parameter
    		this.width = width;
    		this.height = height;
    		
    		// Sets the layout for the JLabel background
    		labelBackground.setLayout(null);
    		
    		// adds background to JFrame
    		add(labelBackground); 
    		
    		// Label bounds
    		labelBackground.setBounds(325, 447, 325, 447);
    		
    		// Sets button size
    		buttonMenu.setPreferredSize(new Dimension(50, 20));
    		
    		// Sets button Absolute positioning  (PosX, PosY, SizeX, SizeY)
    		buttonRelics.setBounds(125, 175, 80, 30);
    		buttonNews.setBounds(125, 225, 80, 30);
    		buttonExit.setBounds(125, 275, 80, 30);
    		
    		buttonMenu.setOpaque(false);
    		buttonMenu.setContentAreaFilled(false);
    		buttonMenu.setBorderPainted(true);
    		
    		// Sets text color
    		buttonMenu.setForeground(Color.white);
    		
    		// Adds buttons to label
    		labelBackground.add(buttonRelics);
    		labelBackground.add(buttonNews);
    		labelBackground.add(buttonExit);
    		
    		// Adds ActionListeners and MouseListeners
    		// Menu Actions
    		buttonMenu.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    					labelBackground.setVisible(true);
    					remove(panelRelics);
    					remove(panelNews);
    			}
    		});
    		
    		buttonMenu.addMouseListener(new MouseAdapter(){
    			public void mouseEntered(MouseEvent e){
    				buttonMenu.setToolTipText("Returns to main Menu");
    			}
    			public void mouseExited(MouseEvent e){
    				
    			}
    		});
    		
    		// Relic Actions
    		buttonRelics.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				labelBackground.setVisible(false);
    				add(panelRelics);
    				Relics();
    				
    			}
    		});
    		
    		buttonRelics.addMouseListener(new MouseAdapter(){
    			public void mouseEntered(MouseEvent e){
    				buttonRelics.setToolTipText("View all Relics");
    			}
    			public void mouseExited(MouseEvent e){
    				
    			}
    		});
    
    		// News Actions
    		buttonNews.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				labelBackground.setVisible(false);
    				add(panelNews);
    				News();
    			}
    		});
    		
    		buttonNews.addMouseListener(new MouseAdapter(){
    			public void mouseEntered(MouseEvent e){
    				buttonNews.setToolTipText("View patch notes");
    			}
    			public void mouseExited(MouseEvent e){
    				
    			}
    		});
    		
    		// Exit 
    		buttonExit.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				System.exit(0);
    			}
    		});
    		
    		buttonExit.addMouseListener(new MouseAdapter(){
    			public void mouseEntered(MouseEvent e){
    				buttonExit.setToolTipText("Exit application");
    			}
    			public void mouseExited(MouseEvent e){
    				
    			}
    		});
    		
    	}
    
    	// returns width
    	public int getWidth(){
    		return width;
    	}
    
    	// returns height
    	public int getHeight(){
    		return height;
    	}
    
    	
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		
    	}
    	
    	// Method to pass JPanel component (used similar to a getter)
    	private static JPanel createPanel(){
    		JPanel wall = new JPanel(new GridBagLayout());
    		
    		// if true
    		if(shouldFill){
    			//natural height, maximum width
    			constraints.fill = GridBagConstraints.HORIZONTAL;
    		}
    		
    		wall.setBackground(Color.BLACK);
    		wall.setBounds(325, 447, 325, 447);
    		return wall;
    	}
    	
    	// Code to create a button
    	private static JButton createButton(String buttonName){
    		JButton button = new JButton();
    		button.setPreferredSize(new Dimension(80, 30));
    		button.setText(buttonName);
    		return button;
    	}
    	
    	// Contains code about what to do inside Relics
    	private void Relics(){		
    		// Label Color
    		labelSearch.setForeground(Color.WHITE);
    		
    		//Positioning
    		if(shouldWeightX){
    			constraints.weightx = 0.5;
    		}
    		
    		// HeroSiege Icon
    		constraints.fill = GridBagConstraints.HORIZONTAL;
    		constraints.weightx = 0.7;
    		constraints.weighty = 0;
    		constraints.gridwidth = 1;
    		constraints.gridx = 0;
    		constraints.gridy = 0;
    		panelRelics.add(labelHeroSiegeTextIcon, constraints);
    		
    		// Search label
    		constraints.fill = GridBagConstraints.HORIZONTAL;
    		constraints.weightx = 0;
    		constraints.gridx = 0;
    		constraints.gridy = 1; 
    		panelRelics.add(labelSearch, constraints);
    		
    		// search text field
    		constraints.fill = GridBagConstraints.HORIZONTAL;
    		constraints.weightx = .5;
    		constraints.gridx = 1;
    		constraints.gridy = 1;
    		panelRelics.add(textField, constraints);
    		
    		/** Going to have all my relics located here in between search and menu*/
    		
    		// Menu button
    		constraints.fill = GridBagConstraints.HORIZONTAL;
    		constraints.weightx = 0;
    		constraints.weighty = 1;
    		constraints.gridx = 0;
    		constraints.gridy = 3;
    		panelRelics.add(buttonMenu, constraints);
    		
    		for(int i = 0; i<getRelics.labelRelics.length; i++){
    			//panelRelics.add(getRelics.labelRelics[i]);
    		}
    	}
    	
    	// Contains code about what to do inside News
    	private void News(){
    		//Layout Manager
    		panelNews.setLayout(new BorderLayout());
    		
    		// Add
    		panelNews.add(buttonMenu, BorderLayout.SOUTH);
    		panelNews.add(labelHeroSiegeTextIcon, BorderLayout.NORTH);
    	}
    	
    }

  2. #2
    Vinvar is offline Member
    Join Date
    Nov 2012
    Posts
    18
    Rep Power
    0

    Default Re: GridBagLayout

    To me GridBagLayout is essentially a table. Like in MS Excel, all rows in a column have the same width. As you specified the HeroSiege icon (which is positioned in the first column) to have a gridWidth of 1, the textfield on the second row (which is positioned in the second column) will there for be next to yet one row lower then the icon.

Similar Threads

  1. GridBagLayout
    By ginger0001 in forum AWT / Swing
    Replies: 3
    Last Post: 03-27-2012, 02:16 AM
  2. GridBagLayout vs me: 1 - 0
    By JosAH in forum AWT / Swing
    Replies: 9
    Last Post: 08-19-2011, 09:18 PM
  3. help with gridbaglayout
    By robertbob in forum AWT / Swing
    Replies: 5
    Last Post: 05-18-2010, 04:14 AM
  4. GridBagLayout
    By Moncleared in forum New To Java
    Replies: 1
    Last Post: 10-18-2009, 10:12 PM
  5. abt gridbaglayout
    By pinky in forum AWT / Swing
    Replies: 1
    Last Post: 12-15-2008, 08:35 AM

Posting Permissions

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