Results 1 to 3 of 3
  1. #1
    frenk_castle is offline Member
    Join Date
    Mar 2010
    Location
    Belgrade, Serbia
    Posts
    27
    Rep Power
    0

    Default GridBagLayout issues

    I have a class in my application that is suppose to represent a single panel. So far the class is defined by the following code.

    Java Code:
    /**
     * 
     */
    package com.quine.workorder.creator.ui;
    
    import java.awt.*;
    import javax.swing.*;
    
    /**
     * @author Ivan Dejanovic
     *
     */
    public class RegularJobPanel extends JPanel
    {
    	private int index;
    	
    	private JLabel jobNameLabel;
    	private JTextField jobNameTextField;
    	
    	private JLabel jobCountLabel;
    	private JSpinner jobCountSpinner;
    	
    	private JLabel duplexLabel;
    	private JComboBox duplexComboBox;
    	
    	private JLabel resolutionLabel;
    	private JComboBox resolutionComboBox;
    	
    	private JLabel deliveryLabel;
    	private JComboBox deliveryComboBox;
    	
    	private JLabel shipmentLabel;
    	private JComboBox shipmentComboBox;
    	
    	private JButton printButton;
    	private JButton packButton;
    	private JButton processButton;
    	
    	private JLabel commentLabel;
    	private JTextArea commentTextArea;
    	
    	public RegularJobPanel(int index)
    	{
    		this.index = index;
    		
    		setBorder(BorderFactory.createTitledBorder("Posao " + (this.index + 1)));
    		
    		setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
    		
    		jobNameLabel = new JLabel("Naziv posla");
    		c.gridx = 0;
    		c.gridy = 0;
    		c.insets = TypeDefinitions.insets;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		add(jobNameLabel, c);
    		
    		jobNameTextField = new JTextField(15);
    		c.gridx = 0;
    		c.gridy = 1;
    		c.insets = TypeDefinitions.insets;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		add(jobNameTextField, c);
    		
    		jobCountLabel = new JLabel("Tiraz");
    		c.gridx = 0;
    		c.gridy = 2;
    		c.insets = TypeDefinitions.insets;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		add(jobCountLabel, c);
    		
    		jobCountSpinner = new JSpinner();
    		c.gridx = 0;
    		c.gridy = 3;
    		c.insets = TypeDefinitions.insets;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		add(jobCountSpinner, c);
    		
    		duplexLabel = new JLabel("Tip stampe");
    		c.gridx = 1;
    		c.gridy = 0;
    		c.insets = TypeDefinitions.insets;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		add(duplexLabel, c);
    		
    		duplexComboBox = new JComboBox();
    		duplexComboBox.addItem("Simplex");
    		duplexComboBox.addItem("Duplex");
    		c.gridx = 1;
    		c.gridy = 1;
    		c.insets = TypeDefinitions.insets;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		add(duplexComboBox, c);
    		
    		resolutionLabel = new JLabel("Rezolucija");
    		c.gridx = 1;
    		c.gridy = 2;
    		c.insets = TypeDefinitions.insets;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		add(resolutionLabel, c);
    		
    		resolutionComboBox = new JComboBox();
    		resolutionComboBox.addItem("300 dpi");
    		resolutionComboBox.addItem("600 dpi");
    		c.gridx = 1;
    		c.gridy = 3;
    		c.insets = TypeDefinitions.insets;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		add(resolutionComboBox, c);
    		
    		deliveryLabel = new JLabel("Tip isporuke");
    		c.gridx = 2;
    		c.gridy = 0;
    		c.insets = TypeDefinitions.insets;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		add(deliveryLabel, c);
    		
    		deliveryComboBox = new JComboBox();
    		deliveryComboBox.addItem("Postom");
    		deliveryComboBox.addItem("Preuzimaju");
    		c.gridx = 2;
    		c.gridy = 1;
    		c.insets = TypeDefinitions.insets;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		add(deliveryComboBox, c);
    		
    		shipmentLabel = new JLabel("Tip posiljke");
    		c.gridx = 2;
    		c.gridy = 2;
    		c.insets = TypeDefinitions.insets;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		add(shipmentLabel, c);
    		
    		shipmentComboBox = new JComboBox();
    		shipmentComboBox.addItem("Obicna");
    		shipmentComboBox.addItem("R");
    		shipmentComboBox.addItem("AR");
    		shipmentComboBox.addItem("Direktna");
    		c.gridx = 2;
    		c.gridy = 3;
    		c.insets = TypeDefinitions.insets;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		add(shipmentComboBox, c);
    		
    		printButton = new JButton("Stampa");
    		c.gridx = 3;
    		c.gridy = 1;
    		c.insets = TypeDefinitions.insets;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		add(printButton, c);
    		
    		packButton = new JButton("Kovertiranje");
    		c.gridx = 3;
    		c.gridy = 2;
    		c.insets = TypeDefinitions.insets;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		add(packButton, c);
    		
    		processButton = new JButton("Dorada");
    		c.gridx = 3;
    		c.gridy = 3;
    		c.insets = TypeDefinitions.insets;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		add(processButton, c);
    		
    		commentLabel = new JLabel("Napomena");
    		c.gridx = 4;
    		c.gridy = 0;
    		c.insets = TypeDefinitions.insets;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		add(commentLabel, c);
    		
    		commentTextArea = new JTextArea();
    		commentTextArea.setLineWrap(true);
    		commentTextArea.setWrapStyleWord(true);
    		c.gridx = 4;
    		c.gridy = 1;
    		c.gridheight = 3;
    		c.gridwidth = 3;
    		c.insets = TypeDefinitions.insets;
    		c.fill = GridBagConstraints.BOTH;
    		add(new JScrollPane(commentTextArea, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), c);
    	}
    }

    TypeDefinition class in which I am putting constants that I will need in this package is so far defined by the following code.

    Java Code:
    /**
     * 
     */
    package com.quine.workorder.creator.ui;
    
    import java.awt.*;
    
    /**
     * @author Ivan Dejanovic
     *
     */
    public class TypeDefinitions
    {
    	public static final int maxJobs = 4;
    	public static final Insets insets= new Insets(5, 5, 5, 5);
    }
    RegularJobPanel is at the moment pretty simple strait forward class. I am having problem with displaying commentTextArea. It is three cells high but not wide enough. API says that GridBagConstraints.gridwidth field controls the width of the component.

    I tried to fix the problem by adding the following code to the end of the RegularJobPanel constructor

    Java Code:
    JLabel label1 = new JLabel("");
    		c.gridx = 5;
    		c.gridy = 0;
    		c.gridheight = 1;
    		c.gridwidth = 1;
    		c.insets = TypeDefinitions.insets;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		add(label1, c);
    		
    		JLabel label2 = new JLabel("");
    		c.gridx = 6;
    		c.gridy = 0;
    		c.insets = TypeDefinitions.insets;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		add(label2, c);
    Width of commentLabel stayed the same. When I changed my "added" code to

    Java Code:
    JLabel label1 = new JLabel("111111111111");
    		c.gridx = 5;
    		c.gridy = 0;
    		c.gridheight = 1;
    		c.gridwidth = 1;
    		c.insets = TypeDefinitions.insets;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		add(label1, c);
    		
    		JLabel label2 = new JLabel("2222222222222");
    		c.gridx = 6;
    		c.gridy = 0;
    		c.insets = TypeDefinitions.insets;
    		c.fill = GridBagConstraints.HORIZONTAL;
    		add(label2, c);
    I got wider commentTextArea. Is there some smarter way to make commentTextArea wider because "solution" I came up with is unacceptable?

    Thanks in advance

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Not sure if it's "cheating" or not, but you could always give your JTextArea row and column sizes:

    Java Code:
        commentTextArea = new JTextArea(3, 30);
    You also might want to give all components weightx and weighty values, though this won't solve your problem.

  3. #3
    frenk_castle is offline Member
    Join Date
    Mar 2010
    Location
    Belgrade, Serbia
    Posts
    27
    Rep Power
    0

    Default

    Giving commentTextArea row and column sizes also doesn't solve the problem. But surprisingly to me adding weight parameters to GridBagConstraints object does. commentTextArea and all other components are stretched to fit the JFrame. commentTextArea is stretched the most and it is wide as I would like.

    Problem now is that UI can get a little ugly when user expands the size of the JFrame but that is little concern at the moment.

    Thank you Fubarable.

Similar Threads

  1. GridBagLayout
    By Moncleared in forum New To Java
    Replies: 1
    Last Post: 10-18-2009, 11:12 PM
  2. [SOLVED] GridBagLayout issues
    By maxim in forum AWT / Swing
    Replies: 6
    Last Post: 02-12-2009, 10:11 PM
  3. GridBagLayout
    By newtojava7 in forum New To Java
    Replies: 2
    Last Post: 03-07-2008, 01:16 AM
  4. GridBagLayout...please help
    By newtojava7 in forum Advanced Java
    Replies: 1
    Last Post: 02-17-2008, 02:16 AM
  5. gridbaglayout
    By newtojava7 in forum New To Java
    Replies: 4
    Last Post: 01-27-2008, 09:03 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
  •