Gridbag layout positions

Hello, I am creating a gui with various swing objects. I put a grouping of various objects within some panels as to allow for simpler layout. The problem however is spaning a jpanel over 2 rows.
basically i want one panel on the left that spans 2 rows, and two panels that span one row each but are placed horizontally to the first panel. followed by 3 panels places horizontally on a 3rd row. when i try to set gridheigh=2 i get weirdness
package javaiq3; 
/** Iq3GUI.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Iq3GUI extends JFrame implements ActionListener{
/* main window essential objects */
  protected JFrame frame;  // main window
  private JPanel panel;  // content panel for main window
  private GridBagLayout gridbag = new GridBagLayout(); // layout object for main window
   * constructor
   * @param title Main window title
  public Iq3GUI(String title){
    //initializes the main window, its layout and panels
    frame = new JFrame(title); // initializes main window
    frame.addWindowListener(new WindowAdapter() { // window listener
      // exits program when window is closed
      public void windowClosing(WindowEvent e) {System.exit(0);}
    GridBagConstraints c = new GridBagConstraints(); // constraints
    GridBagConstraints subc = new GridBagConstraints(); // constraints
    panel = new JPanel();
    panel.setLayout(gridbag);
    setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
/**** menu bar ****/
    JMenuBar menuBar = new JMenuBar();
    frame.setJMenuBar(menuBar);
    JMenu menu = new JMenu("File");
    menu.setMnemonic(KeyEvent.VK_F);
    menu.getAccessibleContext().setAccessibleDescription("File menu");
    menuBar.add(menu);
/**** end: menu bar ****/
/**** type radio buttons ****/
    JRadioButton[] typeRButton = new JRadioButton[2];
    typeRButton[0]=new JRadioButton("Life");
    typeRButton[1]=new JRadioButton("Crit");
    JPanel typeRButtonPanel = new JPanel(); // panel for radio buttons
    typeRButtonPanel.setLayout(new BorderLayout());
    typeRButtonPanel.setBorder(BorderFactory.createTitledBorder("Type:")); // sets border
    ButtonGroup typeGroup = new ButtonGroup(); // radio button group
    for(int x=0;x<typeRButton.length;x++){ // groups radio buttons together
        typeGroup.add(typeRButton[x]);
    typeRButtonPanel.add(typeRButton[0],BorderLayout.CENTER); // adds them to panel
    typeRButtonPanel.add(typeRButton[1],BorderLayout.SOUTH);
    c.insets = new Insets(15,15,0,0);
    c.gridx=0; c.gridy=0; c.ipady=0; c.gridwidth=1; gridbag.setConstraints(typeRButtonPanel, c);
    panel.add(typeRButtonPanel); // adds radio button panel to main panel
/**** end: type radio buttons ****/
/**** company combo box ****/
    JComboBox companyComboBox = new JComboBox();
    JPanel companyComboBoxPanel = new JPanel();
    companyComboBox.setPreferredSize(new Dimension(300,18));
    companyComboBoxPanel.setLayout(gridbag);
    subc.insets = new Insets(-2,5,2,5); gridbag.setConstraints(companyComboBox, subc);
    companyComboBoxPanel.setBorder(BorderFactory.createTitledBorder("Company:"));
    companyComboBoxPanel.add(companyComboBox);
    c.insets = new Insets(0,0,0,0);
    c.gridx=1; c.gridy=0; c.ipadx=0; gridbag.setConstraints(companyComboBoxPanel, c);
    panel.add(companyComboBoxPanel);
/****  end: company combo box ****/
/**** product combo box ****/
    JComboBox productComboBox = new JComboBox();
    JPanel productComboBoxPanel = new JPanel();
    productComboBox.setPreferredSize(new Dimension(300,18));
    productComboBoxPanel.setLayout(gridbag);
    subc.insets = new Insets(-2,5,2,5); gridbag.setConstraints(productComboBox, subc);
    productComboBoxPanel.setBorder(BorderFactory.createTitledBorder("Product:"));
    productComboBoxPanel.add(productComboBox);
    c.insets = new Insets(0,0,0,0);
    c.gridx=1; c.gridy=2; c.ipady=0; gridbag.setConstraints(productComboBoxPanel, c);
    panel.add(productComboBoxPanel);
/****  end: product combo box ****/
/**** type sex radio buttons ****/
    JRadioButton[] sexRButton = new JRadioButton[2];
    sexRButton[0]=new JRadioButton("Male");
    sexRButton[1]=new JRadioButton("Female");
    JPanel sexRButtonPanel = new JPanel(); // panel for radio buttons
    sexRButtonPanel.setLayout(new BorderLayout());
    sexRButtonPanel.setBorder(BorderFactory.createTitledBorder("Sex:")); // sets border
    ButtonGroup sexGroup = new ButtonGroup(); // radio button group
    for(int x=0;x<sexRButton.length;x++){ // groups radio buttons together
        sexGroup.add(sexRButton[x]);
     sexRButtonPanel.add(sexRButton[0],BorderLayout.CENTER); // adds them to panel
     sexRButtonPanel.add(sexRButton[1],BorderLayout.SOUTH);
     c.gridx=0; c.gridy=3; c.ipady=0; gridbag.setConstraints(sexRButtonPanel, c);
     panel.add(sexRButtonPanel); // adds radio button panel to main panel
/**** end: sex radio buttons ****/
/**** smoker check box/combo box ****/
    JCheckBox smokerRButton = new JCheckBox("No");
    JComboBox smokerComboBox = new JComboBox();
    smokerComboBox.setPreferredSize(new Dimension(100,18));
    JPanel smokerPanel = new JPanel();
    smokerPanel.setLayout(new BorderLayout());
    smokerPanel.setBorder(BorderFactory.createTitledBorder("Smoker:"));
    smokerPanel.add(smokerRButton,BorderLayout.CENTER);
    smokerPanel.add(smokerComboBox,BorderLayout.SOUTH);
    c.gridx=1; c.gridy=3; c.ipady=0; gridbag.setConstraints(smokerPanel, c);
    panel.add(smokerPanel);
/**** end: smoker check box/combo box ****/  
/**** waiver radio buttons ****/
    JRadioButton[] waiverRButton = new JRadioButton[2];
    waiverRButton[0]=new JRadioButton("Yes");
    waiverRButton[1]=new JRadioButton("No");
    JPanel waiverRButtonPanel = new JPanel(); // panel for radio buttons
    waiverRButtonPanel.setLayout(new BorderLayout());
    waiverRButtonPanel.setBorder(BorderFactory.createTitledBorder("Waiver:")); // sets border
    ButtonGroup waiverGroup = new ButtonGroup(); // radio button group
    for(int x=0;x<waiverRButton.length;x++){ // groups radio buttons together
        waiverGroup.add(waiverRButton[x]);
     waiverRButtonPanel.add(waiverRButton[0],BorderLayout.CENTER); // adds them to panel
     waiverRButtonPanel.add(waiverRButton[1],BorderLayout.SOUTH);
     c.gridx=2; c.gridy=3; c.ipady=0; gridbag.setConstraints(waiverRButtonPanel, c);
     panel.add(waiverRButtonPanel); // adds radio button panel to main panel
/**** end: type radio buttons ****/
/**** main gui properties ****/
  // Dimension screenDim = Toolkit.getDefaultToolkit().getScreenSize(); //screen dimensions
  //  Rectangle frameDim = frame.getBounds(); // frame boundries
  //  frame.setLocation((screenDim.width - frameDim.width) / 4,(screenDim.height - frameDim.height) / 4);//sets frame's location to center screen
  //  frame.setSize(600,100);
    frame.setContentPane(panel); frame.pack();
    frame.setVisible(true);
/**** end: main gui properties ****/
   * setLookAndFeel
   * Sets the look and feel of the application, if it cannot be set to the
   * user defined one, sets the default java
   * @param feel Stores the look and feel string
  public void setLookAndFeel(String feel){
     try { // sets the GUI style to the parameter type
          UIManager.setLookAndFeel(feel);
      } catch (Exception e) {
         try{
           UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
         catch (Exception e2){ // if default java one cannot be set, exits program
            System.err.println(e2.getMessage());
            System.exit(0);
   * errorBox
   * Displays a JDialog box with an ok button, programmer defined message
   * and a warning icon
   * @param message Programmer defined message
  public void errorBox(String message){
    JOptionPane.showMessageDialog(frame,message,"Error",JOptionPane.ERROR_MESSAGE);
    System.exit(0);
* actionPerformed
* Performs action sent to by action listener
* @param e Action object
  public void actionPerformed(ActionEvent e){
    System.out.println("Click");
* Main
* Test method
* @param args Does nothing
public static void main(String[] args){
    Iq3GUI heh = new Iq3GUI("IQ3");
}

Wow. That's some ugly code. You might want to work on coding conventions a bit.
The reason books provide information in paragraphs is to give the eye a rest and the brain time to process the information (a brain is only 8 MHz -- no kidding). Your code should do the same. If 80% of effort goes into maintaining code, you'll help yourself (and potential coworkers) by coding neatly so that things are easy to understand at a glance without dealing with a lot of visual clutter.
1) Try to put comments on their own lines instead of whacking them onto the end of a line of code.
2) Include spaces between code segments that are related tasks -- think of them as "virtual blocks" (since they're not enclosed in braces). Think about the paragraph analogy again. A paragraph groups related sentences. Let your code do the same.
3) Never, ever jam multiple statements into a single line of code. It's a huge pain in the ass to read. While the compiler doesn't care about whitespace, human beings do.
4) When working with GridBagLayout, add all your components in the same part of your class file. By doing so, when you need to alter the layout, you don't have to dig through all of your code to find bits and pieces of the layout. Your project is small, but if you do this stuff for a living, you're eventually going to work on gigantic projects. My current project, for example, consists of 20,000 lines of code just for the GUI (and my project isn't even all at big).
5) Exiting a program without telling the user is laaaaame. I know you're just playing around, but now's the time to get good habits.
OK, so now that you've sat through the editorial, the quick and dirty answer is that you have to explicitly specify widths and heights in your constrains for each component, otherwise GridBagLayout's behavior becomes hard to predict.
In your code, you specify gridwidth once and never again. You know that a Constraint object is reusable, right? What you've just told GridBagLayout is "try to make everything the same width." Meanwhile, the layout is trying to respect your preferred sizes.
Please read the Java Tutorial on GridBagLayout, particularly the suggestion to sketch your layout on a piece of paper ahead of time so that you remember which constraint attributes to change. For safe measure, you might want to specify an entire set of constraints each time you lay out a component so that you won't leave out something important.
As for the background color, I just fixed it by setting a background for your main panel. Windows look and feel is lame, I guess. I always use the Java look and feel, so I haven't run into your problem before.
See my modified code below.
//package javaiq3; 
/** Iq3GUI.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Iq3GUI extends JFrame implements ActionListener
    protected JFrame frame;
    private JPanel panel;
    private GridBagLayout gridbag = new GridBagLayout();
    public Iq3GUI(String title)
        frame = new JFrame(title);
        frame.addWindowListener(new WindowAdapter()
            public void windowClosing(WindowEvent e)
                System.exit(0);
        GridBagConstraints c = new GridBagConstraints();
        GridBagConstraints subc = new GridBagConstraints();
        panel = new JPanel();
        panel.setLayout(gridbag);
        panel.setBackground(Color.lightGray);
        setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        JMenuBar menuBar = new JMenuBar();
        frame.setJMenuBar(menuBar);
        JMenu menu = new JMenu("File");
        menu.setMnemonic(KeyEvent.VK_F);
        menu.getAccessibleContext().setAccessibleDescription("File menu");
        menuBar.add(menu);
        JRadioButton[] typeRButton = new JRadioButton[2];
        typeRButton[0]=new JRadioButton("Life");
        typeRButton[1]=new JRadioButton("Crit");
        JPanel typeRButtonPanel = new JPanel();
        typeRButtonPanel.setLayout(new BorderLayout());
        typeRButtonPanel.setBorder(BorderFactory.createTitledBorder("Type:"));
        ButtonGroup typeGroup = new ButtonGroup();
        for(int x=0;x<typeRButton.length;x++)
            typeGroup.add(typeRButton[x]);
        typeRButtonPanel.add(typeRButton[0],BorderLayout.CENTER);
        typeRButtonPanel.add(typeRButton[1],BorderLayout.SOUTH);
        c.insets = new Insets(15,15,0,0);
        c.gridx=0;
        c.gridy=0;
        c.ipady=0;
        c.gridwidth=1;
        gridbag.setConstraints(typeRButtonPanel,c);
        panel.add(typeRButtonPanel);
        JComboBox companyComboBox = new JComboBox();
        JPanel companyComboBoxPanel = new JPanel();
        companyComboBox.setPreferredSize(new Dimension(300,18));
        companyComboBoxPanel.setLayout(gridbag);
        subc.insets = new Insets(-2,5,2,5);
        gridbag.setConstraints(companyComboBox, subc);
        companyComboBoxPanel.setBorder(BorderFactory.createTitledBorder("Company:"));
        companyComboBoxPanel.add(companyComboBox);
        c.insets = new Insets(0,0,0,0);
        c.gridx=1;
        c.gridy=0;
        c.ipadx=0;
        gridbag.setConstraints(companyComboBoxPanel, c);
        panel.add(companyComboBoxPanel);
        JComboBox productComboBox = new JComboBox();
        JPanel productComboBoxPanel = new JPanel();
        productComboBox.setPreferredSize(new Dimension(300,18));
        productComboBoxPanel.setLayout(gridbag);
        subc.insets = new Insets(-2,5,2,5);
        gridbag.setConstraints(productComboBox, subc);
        productComboBoxPanel.setBorder(BorderFactory.createTitledBorder("Product:"));
        productComboBoxPanel.add(productComboBox);
        c.insets = new Insets(0,0,0,0);
        c.gridx=1; c.gridy=2; c.ipady=0;
        gridbag.setConstraints(productComboBoxPanel, c);
        panel.add(productComboBoxPanel);
        JRadioButton[] sexRButton = new JRadioButton[2];
        sexRButton[0]=new JRadioButton("Male");
        sexRButton[1]=new JRadioButton("Female");
        JPanel sexRButtonPanel = new JPanel();
        sexRButtonPanel.setLayout(new BorderLayout());
        sexRButtonPanel.setBorder(BorderFactory.createTitledBorder("Sex:"));
        ButtonGroup sexGroup = new ButtonGroup();
        for(int x=0;x<sexRButton.length;x++)
            sexGroup.add(sexRButton[x]);
        sexRButtonPanel.add(sexRButton[0],BorderLayout.CENTER);
        sexRButtonPanel.add(sexRButton[1],BorderLayout.SOUTH);
        c.gridx=0;
        c.gridy=3;
        c.ipady=0;
        gridbag.setConstraints(sexRButtonPanel,c);
        panel.add(sexRButtonPanel);
        JCheckBox smokerRButton = new JCheckBox("No");
        JComboBox smokerComboBox = new JComboBox();
        smokerComboBox.setPreferredSize(new Dimension(100,18));
        JPanel smokerPanel = new JPanel();
        smokerPanel.setLayout(new BorderLayout());
        smokerPanel.setBorder(BorderFactory.createTitledBorder("Smoker:"));
        smokerPanel.add(smokerRButton,BorderLayout.CENTER);
        smokerPanel.add(smokerComboBox,BorderLayout.SOUTH);
        c.gridx=1;
        c.gridy=3;
        c.ipady=0;
        gridbag.setConstraints(smokerPanel,c);
        panel.add(smokerPanel);
        JRadioButton[] waiverRButton = new JRadioButton[2];
        waiverRButton[0]=new JRadioButton("Yes");
        waiverRButton[1]=new JRadioButton("No");
        JPanel waiverRButtonPanel = new JPanel();
        waiverRButtonPanel.setLayout(new BorderLayout());
        waiverRButtonPanel.setBorder(BorderFactory.createTitledBorder("Waiver:"));
        ButtonGroup waiverGroup = new ButtonGroup();
        for(int x=0;x<waiverRButton.length;x++)
            waiverGroup.add(waiverRButton[x]);
        waiverRButtonPanel.add(waiverRButton[0],BorderLayout.CENTER);
        waiverRButtonPanel.add(waiverRButton[1],BorderLayout.SOUTH);
        c.gridx=2;
        c.gridy=3;
        c.ipady=0;
        gridbag.setConstraints(waiverRButtonPanel,c);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        c.gridheight = 2;
        //c.weightx = 100;
        //c.weighty = 100;
        c.insets = new Insets(0,0,0,0);
        panel.add(typeRButtonPanel, c);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 0;
        c.gridwidth = 2;
        c.gridheight = 1;
        //c.weightx = 100;
        //c.weighty = 100;
        c.insets = new Insets(0,0,0,0);
        panel.add(companyComboBoxPanel, c);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 1;
        c.gridwidth = 2;
        c.gridheight = 1;
        //c.weightx = 100;
        //c.weighty = 100;
        c.insets = new Insets(0,0,0,0);
        panel.add(productComboBoxPanel, c);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 3;
        c.gridwidth = 1;
        c.gridheight = 2;
        //c.weightx = 100;
        //c.weighty = 100;
        c.insets = new Insets(0,0,0,0);
        panel.add(sexRButtonPanel, c);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 1;
        c.gridy = 3;
        c.gridwidth = 1;
        c.gridheight = 2;
        //c.weightx = 100;
        //c.weighty = 100;
        c.insets = new Insets(0,0,0,0);
        panel.add(smokerPanel, c);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 2;
        c.gridy = 3;
        c.gridwidth = 1;
        c.gridheight = 2;
        //c.weightx = 100;
        //c.weighty = 100;
        c.insets = new Insets(0,0,0,0);
        panel.add(waiverRButtonPanel, c);
        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
    public void setLookAndFeel(String feel)
        try
            UIManager.setLookAndFeel(feel);
        catch(Exception e)
            try
                UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
            catch(Exception e2)
                System.err.println(e2.getMessage());
                System.exit(0);
    public void errorBox(String message)
        JOptionPane.showMessageDialog(frame,message,"Error",JOptionPane.ERROR_MESSAGE);
        System.exit(0);
    public void actionPerformed(ActionEvent e)
        System.out.println("Click");
    public static void main(String[] args)
        Iq3GUI heh = new Iq3GUI("IQ3");
}

Similar Messages

  • JPanel Positiong with Gridbag Layout Problem

    Hi,
    I'm desingning an UI in which I made a JFrame and set it Layout as gridBag layout. Now I have added Three JPanel in this JFrame.
    The problem is that All the three JPanels are shown in center While I want to palce them in top left corner.
    I have done this through NetBeans 4.0 in the layout coustomize option I've set them in first in the left top most, second in below of the first and so on but when I run the application The JPanel are shown in the center in this Particular position i.e first at the top in center second below to the fist and so on.
    Kindly solve my this problem.
    Regards,
    Danish Kamran.

    To get better help sooner, post a SSCCE that clearly demonstrates your problem.
    Use code tags to post codes -- [code]CODE[/code] will display asCODEOr click the CODE button and paste your code between the {code} tags that appear.
    db

  • How to make JLabel Left Justified in Gridbag Layout?

    Hi Govind here I am using Gridbag layout and having Labels and TextBox . i want my All my Labels Left Justified (Currently they are RightJustified),I tried JLabel.Left but not working so any help most welcome............
    JLabel startDateLabel      = new JLabel("STARTDATE ",JLabel.LEFT);
              startDateLabel.setBorder( border );                                   
              //set the position of the label               
              gbc.fill = GridBagConstraints.NONE;
              usingGBC(startDateLabel,gbc,3,0,1,1.0);
                   //text field for start dat
    txtStartDate                = new JTextField();
              txtStartDate.setMinimumSize(minSize);
              txtStartDate.setPreferredSize( longField );
              txtStartDate.setEnabled( true);                    
              txtStartDate.setEditable(false);
              txtStartDate.setBackground(Color.WHITE);          
              gbc.fill = GridBagConstraints.HORIZONTAL;
              usingGBC(txtStartDate,gbc,4,0,2,1.0);
         here usingGBC is method as follows
    protected void usingGBC(Component com,GridBagConstraints gbc,int gx,int gy,int gwidth,double wx)
                   gbc.gridx = gx;
                   gbc.gridy = gy;
                   gbc.gridwidth = gwidth;
                   gbc.weightx = wx;
                   add(com,gbc);

    It seems you set the fill of the GridBagConstraints after you add the label. Either specify HORIZONTAL fill before adding the label or specify alignment for the constraints.
    Mike

  • JTextArea & Gridbag layout - max size of control

    Hello everyone:
    I am currnetly using a JTextArea with the Gridbag layout. I want to limit the size of the JTextArea. Found an article when I did a "search" that says the size is actually controlled by the layout manager. Have tried setMaximunSize(), no luck. It appears the layout manager ignores this. The problem I am having is that I can set the size I want, however if the user types a lot of text in to the JTextArea, the control expands and over and "pushes" the controls below it down. How do I keep the layout manager from allowing this to happen?
    Thanks in advance, Bart

    Hello everyone:
    I am currnetly using a JTextArea with the Gridbag
    layout. I want to limit the size of the JTextArea.
    Found an article when I did a "search" that says the
    size is actually controlled by the layout manager.
    Have tried setMaximunSize(), no luck. It appears the
    layout manager ignores this. The problem I am having
    is that I can set the size I want, however if the user
    types a lot of text in to the JTextArea, the control
    expands and over and "pushes" the controls below it
    down. How do I keep the layout manager from allowing
    this to happen?
    Thanks in advance, Bart Do you wish to allow the user to enter as much text as he or she likes? If so, wrap the JTextArea in a JScrollPane and set the preferred size of the JScrollPange to the area you'd like the pane to consume. It'll listen, no doubt. :)

  • Gridbag Layout In Flex - Looking for canvasgrid

    Hello,
    I found a posting about a layout mechanism call canvasgrid,
    based on the gridbag layout principle. It is documented at
    Mannu's Blog.
    It is a very effective layout, but the download is broken, and so
    If anyone happens to have his open source reference The source
    filename is canvasgrid_v0.3.1.zip, and it was originally located
    at:
    http://tasmania.globat.com/~mannu.info/flex/canvasgrid_v0.3.1.zip
    If anyone happens to be a code squirel, like myself, and has
    a copy of this, that would be great. I will make it a part of the
    flexlib, just because it's so usefull.
    Thaks

    Hi, You can try this:
    package {
    import flash.display.MovieClip;
    import mx.core.SpriteAsset;
    import mx.core.MovieClipAsset;
    public class GameSymbols extends MovieClip {
    [Embed(source='gameSymbols.swf', symbol='Coffee')]
    private var CoffeeClass:Class;
    //CoffeeClass represents the embedded symbol.
    public function GameSymbols() {
    //Flex defines CoffeClass as a reference to a subclass of
    //SpriteAsset class - for single-frame SWF files or
    //MovieClipAsset class - for multiframe SWF files.
    var myCoffee:SpriteAsset = new CoffeeClass() as SpriteAsset;
    //var myCoffee:MovieClipAsset = new CoffeeClass() as
    MovieClipAsset;
    //You can manipulate the embedded Symbol by using the
    methods
    //and properties of the SpriteAsset or MovieClipAsset class.
    myCoffee.setActualSize(50, 50);
    myCoffee.x = 10;
    myCoffee.y = 10;
    myCoffee.alpha = 100;
    myCoffee.visible = true;
    addChild(myCoffee);
    Luk.

  • Possible Problem/Bug In GridBag Layout ????

    I've done hundreds of Gridbag layouts in the past 2 years and just noticed this weird little behavior today.....
    Basically what I'm trying to do is arrange panels like this.....
    +--------+--------+--------+
    |        |        |        |
    |   A    |    B   |   C    |
    |        |        |        |
    +--------+----+---+--------+
    |             |            |
    |     D       |      E     |
    |             |            |
    +-------------+------------+The obvious thing to do is to
    o make the gridwidths of
    B, D, E = 2
    A, C = 1
    D and E should each share one unit of
    B's 2 width.
    Tried it in my code with my real UI and it didn't work, so then I tested it
    out using a Gridbag tool where you can set parameters on the fly quickly and
    surely enough it did not work there either.
    The GridBagLayout is refusing to split B into 2 portions for the 2nd row
    to use in an unalligned fashion. I either get
    +--------+--------+--------+
    |        |        |        |
    |   A    |    B   |   C    |
    |        |        |        |
    +--------+----+---+--------+
    |        |                 |
    |     D  |           E     |
    |        |                 |
    +--------+-----------------+  or
    +--------+--------+--------+
    |        |        |        |
    |   A    |    B   |   C    |
    |        |        |        |
    +--------+----+---+--------+
    |                 |        |
    |     D           |  E     |
    |                 |        |
    +--------+--------+--------+ depending on the order in which I add my panels to the layout.
    even though my gridx, gridy and widths and heights are properly set for
    obtaining the result I want.
    Can someone confirm that this is a bug in the GridBagLayout or share the trick for
    getting around this?

    Thanks for the reply - I can set the weights however
    I wish and I never see box D extending even one pixel
    into the A-B boundary.This does what you say...
    import java.awt.*;
    import javax.swing.*;
    public class TestFrame extends JFrame {
      public TestFrame() {
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weighty = 0.5; 
        panel.add(new JButton("A"), gbc);
        gbc.gridx = 1;   
        gbc.gridwidth = 2; 
        panel.add(new JButton("B"), gbc);
        gbc.gridx = 3;   
        gbc.gridwidth = 1;
        panel.add(new JButton("C"), gbc);
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 2;
        gbc.weightx = 0.5;
        panel.add(new JButton("D"), gbc);
        gbc.gridx = 2;
        panel.add(new JButton("E"), gbc);
        getContentPane().add(panel);
        setSize(400, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
      public static void main(String[] args) {
        TestFrame frame = new TestFrame();
        frame.setVisible(true);
    }But i'm sure it's not a satisfactory layout because button A does not want to resize horizontally at all. But of course, a mix of grid layouts does the trick:
    import java.awt.*;
    import javax.swing.*;
    public class TestFrame extends JFrame {
      public TestFrame() {
        JPanel panel = new JPanel(new GridLayout(2, 1));
        JPanel top = new JPanel(new GridLayout(1, 3));
        top.add(new JButton("A"));
        top.add(new JButton("B"));
        top.add(new JButton("C"));
        panel.add(top);
        JPanel buttom = new JPanel(new GridLayout(1, 2));
        buttom.add(new JButton("D"));
        buttom.add(new JButton("E"));
        panel.add(buttom);
        getContentPane().add(panel);
        setSize(400, 300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
      public static void main(String[] args) {
        TestFrame frame = new TestFrame();
        frame.setVisible(true);
    }

  • Gridbag layout problem on a tabbedpane

    I am not sure whether we can do this or not but, I am trying to have a gridbag layout on a TabbedPane object.
    I have a JFrame on which I am adding a TabbedPane object called "t" and on this TabbedPane I am adding a tab called "Insert" which is an object of class "Insert Data". Then, I add the TabbedPane t on the Container cp, which is inside the JFrame.
    In the InsertData Class (a JPanel), I need to have the gridbag layout. With this gridbag layout object, I am trying to place different objects like buttons, at various places, on this JPanel. But nothing moves on this panel.
    In short, please let me know how can I have a gridbag layout on a Tabbedpane.
    The Main Class is as follows:
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import javax.swing.JFrame;
    import javax.swing.JTabbedPane;
    import javax.swing.event.ChangeEvent;
    import javax.swing.event.ChangeListener;
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    public class Main extends JFrame implements ActionListener, ChangeListener{
         Main(){
                   setPreferredSize(new Dimension(1200,600));
                   Container cp = getContentPane();
                   JTabbedPane t = new JTabbedPane();
                   // insert
                   InsertData insertOptions = new InsertData();
                   t.addTab("Insert",insertOptions);
                   cp.add(t);
                   this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                   pack();
                   setVisible(true);
              @Override
              public void actionPerformed(ActionEvent arg0) {
                   // TODO Auto-generated method stub
              @Override
              public void stateChanged(ChangeEvent arg0) {
                   // TODO Auto-generated method stub
              public static void main(String args[]){
                   new Main();
         }The InsertDataClass is:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    public class InsertData extends JPanel{
         InsertData(){
              setPreferredSize(new Dimension(1200,600));
              setBackground(Color.blue);
              //setLayout(new GridBagLayout());
              GridBagLayout gb = new GridBagLayout();
              setLayout(gb);
              GridBagConstraints c = new GridBagConstraints();
              //c.insets = new Insets(2, 2, 2, 2);
              //JPanel p1= new JPanel();
              //p1.setPreferredSize(new Dimension(200,200));
              JButton b1 = new JButton("here i am!!!");
              //p1.add(b1);
              //c.fill = GridBagConstraints.HORIZONTAL;
              //c.anchor = GridBagConstraints.WEST;
              c.gridx=0;
              c.gridy=1;
              add(b1,c);
    }

    how can I have a gridbag layout on a Tabbedpane.Huh? You post an example with just one JButton and no weightx / weighty set and you expect others here to be able to see a problem with your layout?
    Also, there's needless, unused code that is just clutter -- like the unimplemented ActionListener and ChaneListener. Recommended reading: [SSCCE (Short, Self Contained, Compilable and Executable, Example Program)|http://mindprod.com/jgloss/sscce.html].
    Finally, I don't see any need whatsoever to extend JFrame and JPanel as you are not introducing any new behavior of either. Always favor composition over inheritance.
    I plugged in some code I had used for someone else's GridBagLayout problems and this will show you that there's no difference in using GridBagLayout in a tabbed pane component or anywhere else.import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import javax.swing.*;
    public class GridBagInTabbedPane {
      public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
          @Override
          public void run() {
            new GridBagInTabbedPane().makeUI();
      public void makeUI() {
        Font fontButton = new Font("Arial", Font.BOLD, 10);
        Font fontLabel = new Font("Arial", Font.BOLD, 15);
        JLabel labelEnter = new JLabel("Enter sentences in the text area ");
        labelEnter.setFont(fontLabel);
        JTextArea textArea = new JTextArea();
        textArea.setPreferredSize(new Dimension(630, 280));
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setMinimumSize(new Dimension(630, 280));
        JLabel labelDuplicates = new JLabel("Duplicates will appear here");
        labelDuplicates.setMinimumSize(new Dimension(650, 30));
        labelDuplicates.setFont(fontLabel);
        JButton buttonDisplay = new JButton("Display Map");
        buttonDisplay.setFont(fontButton);
        JButton buttonClear = new JButton("Clear");
        buttonClear.setFont(fontButton);
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        gbc.fill = GridBagConstraints.NONE;
        gbc.anchor = GridBagConstraints.CENTER;
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.weightx = 0.5;
        panel.add(labelEnter, gbc);
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.weighty = 0.5;
        panel.add(scrollPane, gbc);
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.NONE;
        gbc.weighty = 0.0;
        panel.add(labelDuplicates, gbc);
        gbc.gridy = 3;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(buttonDisplay, gbc);
        gbc.gridx = 1;
        gbc.anchor = GridBagConstraints.WEST;
        panel.add(buttonClear, gbc);
        JTabbedPane tabbedPane = new JTabbedPane();
        tabbedPane.add(panel);
        JFrame frame = new JFrame();
        frame.add(tabbedPane);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }db

  • Gridbag layout continues to confound me...

    I am having problems with the gridx and gridy constraints when trying to use gridbag layout. They seem to have no effect when I use them. The button always appears in the top left of the panel. Other constraints seem to work as expected.
    For example: c2.fill = GridBagConstraints.BOTH; will fill up the entire panel with my button.
    Any advice on what I am doing wrong this time?
    Thanks
    import javax.swing.JPanel;
    import javax.swing.JTextArea;
    import javax.swing.JScrollPane;
    import javax.swing.JFrame;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.*;
    import java.awt.Color;
    import javax.swing.*;
    import javax.swing.border.*;
    import javax.swing.text.*;
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
    public class Question
         public JPanel test()
              JPanel testPanel = new JPanel(new GridBagLayout());
              GridBagConstraints c2 = new GridBagConstraints();
              c2.insets = new Insets(5,5,5,5);
              c2.weightx = 1.0;
              c2.weighty = 1.0;
              c2.anchor = c2.NORTHWEST;
              JButton redButton = new JButton("Button");
              c2.gridx = 2;
              c2.gridy = 2;
              //c2.fill = GridBagConstraints.BOTH;//this works as expected
              testPanel.add(redButton,c2);
              return testPanel;
         public Container createContentPane()
              //Create the content-pane-to-be.
              JPanel contentPane = new JPanel(new BorderLayout());
              contentPane.setOpaque(true);
              return contentPane;
         private static void createAndShowGUI()
              //Create and set up the window.
              JFrame frame = new JFrame("question");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              //Create and set up the content pane.
              Question demo = new Question();
              frame.setContentPane(demo.createContentPane());
              frame.add(demo.test());
              //Display the window.
              frame.setSize(400, 400);
              frame.setVisible(true);
        public static void main(String[] args)
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            javax.swing.SwingUtilities.invokeLater(new Runnable()
                public void run()
                    createAndShowGUI();
        }//end main
    }//end Question

    GridBagLayout keeps zero width/height grid for non-existant component for the grid.
    You could override this behavior by using GBL's four arrays as shown below.
    However, in order to get the desired layout effect, using other layout manager, e.g. BoxLayout and/or Box, is much easier and flexible as camickr, a GBL hater, suggests.
    Anyway, however, before using a complex API class as GBL, you shoud read the documentation closely.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class Question{
      // you may adjust these values for your taste
      static final int rowHeight    = 100;
      static final int colWidth     = 100;
      static final double rowWeight = 1.0;
      static final double colWeight = 1.0;
      public JPanel test(){
        GridBagLayout gb = new GridBagLayout();
        gb = keepAllRowsAndColumns(gb, 3, 3);
        JPanel testPanel = new JPanel(gb);
        GridBagConstraints c2 = new GridBagConstraints();
        c2.insets = new Insets(5,5,5,5);
        c2.weightx = 1.0;
        c2.weighty = 1.0;
        c2.anchor = c2.NORTHWEST;
        JButton redButton = new JButton("Button");
        c2.gridx = 2;
        c2.gridy = 2;
        // c2.fill = GridBagConstraints.BOTH;//this works as expected
        testPanel.add(redButton,c2);
        return testPanel;
      GridBagLayout keepAllRowsAndColumns(GridBagLayout g, int rn, int cn){
        g.rowHeights = new int[rn];
        g.columnWidths = new int[cn];
        g.rowWeights = new double[rn];
        g.columnWeights = new double[cn];
        for (int i = 0; i < rn; ++i){
          g.rowHeights[i] = rowHeight;
          g.rowWeights[i] = rowWeight;
        for (int i = 0; i < cn; ++i){
          g.columnWidths[i] = colWidth;
          g.columnWeights[i] = colWeight;
        return g;
      private static void createAndShowGUI(){
        JFrame frame = new JFrame("question");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Question demo = new Question();
        frame.getContentPane().add(demo.test(), BorderLayout.CENTER);
        frame.setSize(400, 400);
        frame.setVisible(true);
      public static void main(String[] args){
        javax.swing.SwingUtilities.invokeLater(new Runnable(){
          public void run(){
            createAndShowGUI();
    }

  • Gridbag Layout and Tabbed Panes

    First off, I'm pretty new to Java, I started it around February as a school course, and it's been a pretty smooth ride. Recently, we started to code GUI, trying out different layouts and whatnot. I'm having a little trouble in this portion as our teacher did not really delve into it very much. My question concerns the GridBag layout within a panel of a tab (sorry if this is unclear, I'm talking about something that looks like this: http://www.codeproject.com/useritems/tabcontrol.asp). I don't know how to use GridBag constraints within a panel. What I used to do with GridBag was simply use some code that looked like (forgive me, this might not be very accurate): AddComponent button1(#, #, #, #) and define what each of those numbers meant later (first would be row, second would be column, third would be width, fourth would be height, for example). But now, with the tabs, each button, label, or any other control would be declared, then I would place a bunch of direct constraints after, like this:
    JButton button1 = new JButton("1");
    constraints.gridx = 0;
    constraints.gridy = 0;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;then add them into the panel like this:
    panel1.add(button1, constraints);However, I have tried making the buttons on top of each other, and other patterns, but it would just place the buttons next to each other within the panel. How do I make it so that the constraints work? Or rather, if they do work, what kinda things might mess it up? I think the problem is that I dunno how to make the constraints specific to that one button.
    Also, how do I make each panel a certain size instead of each panel being as small as possible to contain anything within it.
    I'm sorry if this all sounds very vague, I'd be more than happy to give you any needed information. Also, I'm not asking any of you to do my homework, I just need a little clarification or an example. Just in case, here's the code I'm working on: http://hashasp.mine.nu/paster/?1376. Be aware that there are no comments or anything, it's very bare. Also, the form may be a bit messy because I'm pretty much reusing code that was given to me, just manipulating it in a different way. Thanks a lot in advanced for your time in reading this.

    * GridBag_Demo.java
    import java.awt.*;
    import javax.swing.*;
    public class GridBag_Demo extends JFrame {
        public GridBag_Demo() {
            setTitle("GridBag Demo");
            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            setSize(400,300);
            setLocationRelativeTo(null);
            GridBagConstraints gridBagConstraints;
            jTabbedPane1 = new JTabbedPane();
            jPanel1 = new JPanel();
            jButton1 = new JButton();
            jButton2 = new JButton();
            jButton3 = new JButton();
            jPanel1.setLayout(new GridBagLayout());
            jButton1.setText("jButton1");
            gridBagConstraints = new GridBagConstraints();
            gridBagConstraints.insets = new Insets(2,2,2,2);
            jPanel1.add(jButton1, gridBagConstraints);
            jButton2.setText("jButton2");
            gridBagConstraints.gridy = 1;
            jPanel1.add(jButton2, gridBagConstraints);
            jButton3.setText("jButton3");
            gridBagConstraints.gridy = 2;
            jPanel1.add(jButton3, gridBagConstraints);
            jTabbedPane1.addTab("tab1", jPanel1);
            getContentPane().add(jTabbedPane1, BorderLayout.CENTER);
        public static void main(String args[]) {
            new GridBag_Demo().setVisible(true);
        private JButton jButton1;
        private JButton jButton2;
        private JButton jButton3;
        private JPanel jPanel1;
        private JTabbedPane jTabbedPane1;
    }

  • JTextArea & Gridbag layout

    Hello everyone:
    I am currnetly using a JTextArea with the Gridbag layout. I want to limit the size of the JTextArea. Found an article when I did a "search" that says the size is actually controlled by the layout manager. Have tried setMaximunSize(), no luck. It appears the layout manager ignores this. The problem I am having is that I can set the size I want, however if the user types a lot of text in to the JTextArea, the control expands and over and "pushes" the controls below it down. How do I keep the layout manager from allowing this to happen?
    Thanks in advance, Bart

    Have set the rows and columns as you suggested in the original code,and get the size I want initalially. The problem is that the JTextArea will increase past the size set so it "bleeds" downward. I looked in the Java book I have, and it says that the scrollbars for a JTextArea are provided automatically, however they do not appear when the user supplies more lines of text than there are rows. Instead the height of the control is enlarged and this in turn pushes the other controls on the pane down. Is this a mi-print in the book, ie the scroll bars are not automatically provide in a JTextArea?
    Thanks,
    Bart

  • Gridbag layout in jpanel?

    Hi, all
    I just start to use gridbag layout in my application. It looks very like table in html to me. While writing html table, I can use 'border=1' to determine width and height for each cell. Is it possible to reveal border of each grid in jpanel?
    Thanks,

    You can't reveal the border of the cells being used by GridBagLayout.
    You could add a LineBorder to each component but this won't show you the cell border, it will show you the component border. There could be padding between the component and the cell boundary.
    However, you should try to avoid thinking of GridBagLayout as being like an HTML table; this usually causes confusion. There's probably a number of topics in this forum about wanting to get HTML table behaviour with a GridBagLayout.
    Here's one for a start:
    http://forum.java.sun.com/thread.jspa?forumID=57&threadID=251815

  • GridBag Layout help

    Hey all,
    I'm currently revamping an application I wrote for my company that I wrote with a GridLayout. It doesn't look bad, but it doesn't look as good as a standard windows application. I'd like to write it using a gridbag layout in order to make it look a little better but no where can I find a good idea of how to even use it... Javadocs on it seem kinda cryptic to me and I'd like a better visual example than just a bunch of buttons (which is what the tutorial gives). Anyone have any ideas where I can look?

    Here is a simple example of some of my code that uses the gridbag layout. (slightly modified so it will run by itself...). I added some comments on what some of the different settings do...
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    import java.text.*;
    import javax.swing.event.*;
    import java.util.Properties;
    * trackWindow.java
    * Created October 24, 2002, 6:26 PM
    * This class is used to display a tabbed internal window that is used to
    * display and control the settings required for localizing particles and
    * tracking their movement as part of the Trackguy program.
    public class trackWindow2 extends JInternalFrame implements ActionListener {
         private JTabbedPane trackTabPanel;
         private JPanel maskPanel;
         private JPanel trackPanel;
         private JLabel multLabel;
         private JTextField multiplier;
         private JLabel threshLabel;
         private JTextField threshold;
         private JToggleButton dilate2;
         private JToggleButton partBK;
         private JLabel partSizeLabel;
         private JTextField minPartSize;
         private JLabel scaleLabel;
         private JLabel tIntervalLabel;
         private JLabel xScaleLabel;
         private JTextField xScale;
         private JLabel yScaleLabel;
         private JTextField yScale;
         private JTextField tInterval;
         private JLabel timeUnitLabel;
         private JLabel maxDifLabel;
         private JTextField maxDif;
         private JLabel minTrackLabel;
         private JTextField minTrack;
         private JLabel difCoefLabel;
         private JTextField difCoef;
         public trackWindow2 () {
                initFields();
                collectPanels();
                buildWindow();
                setIcon();
                show();
         private void initFields() {
                multLabel = new JLabel("Multiplier: ");
                multiplier = new JTextField("10");
                multiplier.setActionCommand("multiplier");
                multiplier.setPreferredSize(new Dimension(40, 18));
                multiplier.addActionListener(this);
                threshLabel = new JLabel("Threshold: ");
                threshold = new JTextField("32770");
                threshold.setActionCommand("threshold");
                threshold.setPreferredSize(new Dimension(40, 18));
                threshold.addActionListener(this);
                dilate2 = new JToggleButton("Dilate 2 X");
                dilate2.addActionListener(this);
                partBK = new JToggleButton("Particle BK");
                partBK.addActionListener(this);
                partSizeLabel = new JLabel("Min Particle Size: ");
                minPartSize = new JTextField("9");
                minPartSize.setActionCommand("part. size");
                minPartSize.setPreferredSize(new Dimension(40, 18));
                minPartSize.addActionListener(this);
                scaleLabel = new JLabel("Scale");
                tIntervalLabel = new JLabel("Time Interval");
                xScaleLabel = new JLabel("x = ");
                xScale = new JTextField("1.08e-007");
                xScale.setActionCommand("xScale");
                xScale.setPreferredSize(new Dimension(60, 18));
                xScale.addActionListener(this);
                yScaleLabel = new JLabel("y = ");
                yScale = new JTextField("1.08e-007");
                yScale.setActionCommand("yScale");
                yScale.setPreferredSize(new Dimension(60, 18));
                yScale.addActionListener(this);
                tInterval = new JTextField("1.5");
                tInterval.setHorizontalAlignment(JTextField.CENTER);
                tInterval.setActionCommand("tInterval");
                tInterval.setPreferredSize(new Dimension(40, 18));
                tInterval.addActionListener(this);
                timeUnitLabel = new JLabel("Sec.");
                timeUnitLabel.setHorizontalAlignment(SwingConstants.LEFT);
                maxDifLabel = new JLabel("Max. Difference: ");
                maxDif = new JTextField("50");
                maxDif.setActionCommand("difference");
                maxDif.setPreferredSize(new Dimension(40, 18));
                maxDif.addActionListener(this);
                minTrackLabel = new JLabel("Min. Track: ");
                minTrack = new JTextField("20");
                minTrack.setActionCommand("minTrack");
                minTrack.setPreferredSize(new Dimension(40, 18));
                minTrack.addActionListener(this);
                difCoefLabel = new JLabel("Diff. Coefficient:");
                difCoef = new JTextField("6.00e-14");
                difCoef.setActionCommand("coefficient");
                difCoef.setPreferredSize(new Dimension(60, 18));
                difCoef.addActionListener(this);
         private void collectPanels() {
                GridBagConstraints gridBagConstraints;
                maskPanel = new JPanel();
              maskPanel.setLayout(new GridBagLayout());
                gridBagConstraints = new GridBagConstraints();
                //1st Column
                   gridBagConstraints.gridx = 0;
                //1st Row
                   gridBagConstraints.gridy = 0;
                //Stuck to the Left of the Column
                   gridBagConstraints.anchor = GridBagConstraints.WEST;
                maskPanel.add(multLabel, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                 gridBagConstraints.gridx = 1;
                     gridBagConstraints.gridy = 0;
                   gridBagConstraints.anchor = GridBagConstraints.WEST;
                maskPanel.add(multiplier, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 0;
                   gridBagConstraints.gridy = 1;
                   gridBagConstraints.anchor = GridBagConstraints.WEST;
                maskPanel.add(threshLabel, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 1;
                   gridBagConstraints.gridy = 1;
                   gridBagConstraints.anchor = GridBagConstraints.WEST;
                maskPanel.add(threshold, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 0;
                   gridBagConstraints.gridy = 2;
                maskPanel.add(dilate2, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 1;
                   gridBagConstraints.gridy = 2;
                maskPanel.add(partBK, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 0;
                   gridBagConstraints.gridy = 3;
                   gridBagConstraints.anchor = GridBagConstraints.WEST;
                maskPanel.add(partSizeLabel, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 1;
                   gridBagConstraints.gridy = 3;
                 gridBagConstraints.anchor = GridBagConstraints.WEST;
                maskPanel.add(minPartSize, gridBagConstraints);
                trackPanel = new JPanel();
                trackPanel.setLayout(new GridBagLayout());
                gridBagConstraints = new GridBagConstraints();
                //1st Column
                   gridBagConstraints.gridx = 0;
                //1st Row
                   gridBagConstraints.gridy = 0;
                //2 Columns Wide
                   gridBagConstraints.gridwidth = 2;
                //40 empty pixels to Left of the Component
                   gridBagConstraints.insets = new Insets(0, 40, 0, 0);
                trackPanel.add(scaleLabel, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                //3rd Column
                   gridBagConstraints.gridx = 2;
                //1st Row
                   gridBagConstraints.gridy = 0;
                //2 Columns wide
                   gridBagConstraints.gridwidth = 2;
                //5 empty pixels to the left of the component
                   gridBagConstraints.insets = new Insets(0, 5, 0, 0);
                trackPanel.add(tIntervalLabel, gridBagConstraints);
                  gridBagConstraints = new GridBagConstraints();
                //1st Column
                   gridBagConstraints.gridx = 0;
                //2nd Row
                   gridBagConstraints.gridy = 1;
                //Stuck to the Right of the Column
                   gridBagConstraints.anchor = GridBagConstraints.EAST;
                trackPanel.add(xScaleLabel, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                //2nd Column
                   gridBagConstraints.gridx = 1;
                //2nd Row
                   gridBagConstraints.gridy = 1;
                //Stuck to the Left of the Column
                   gridBagConstraints.anchor = GridBagConstraints.WEST;
                trackPanel.add(xScale, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 0;
                   gridBagConstraints.gridy = 2;
                   gridBagConstraints.anchor = GridBagConstraints.EAST;
                trackPanel.add(yScaleLabel, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 1;
                   gridBagConstraints.gridy = 2;
                trackPanel.add(yScale, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 2;
                   gridBagConstraints.insets = new Insets(0, 10, 0, 0);
                trackPanel.add(tInterval, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 2;
                   gridBagConstraints.insets = new Insets(0, 10, 0, 0);
                trackPanel.add(tInterval, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 3;
                   gridBagConstraints.gridy = 1;
                   gridBagConstraints.anchor = GridBagConstraints.WEST;
                trackPanel.add(timeUnitLabel, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                //1st Column
                   gridBagConstraints.gridx = 0;
                //4th Row
                   gridBagConstraints.gridy = 3;
                //10 empty pixels on top of the Component
                   gridBagConstraints.insets = new Insets(10, 0, 0, 0);
                trackPanel.add(maxDifLabel, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 1;
                   gridBagConstraints.gridy = 3;
                   gridBagConstraints.insets = new Insets(10, 0, 0, 0);
                trackPanel.add(maxDif, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 0;
                   gridBagConstraints.gridy = 4;
                   gridBagConstraints.anchor = GridBagConstraints.EAST;
                trackPanel.add(minTrackLabel, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 1;
                   gridBagConstraints.gridy = 4;
                trackPanel.add(minTrack, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridx = 2;
                   gridBagConstraints.gridy = 3;
                   gridBagConstraints.gridwidth = 2;
                   gridBagConstraints.insets = new Insets(0, 5, 0, 0);
                trackPanel.add(difCoefLabel, gridBagConstraints);
                gridBagConstraints = new GridBagConstraints();
                   gridBagConstraints.gridy = 4;
                   gridBagConstraints.gridwidth = 2;
                   gridBagConstraints.insets = new Insets(0, 0, 5, 0);
                trackPanel.add(difCoef, gridBagConstraints);
                trackTabPanel = new JTabbedPane();
                trackTabPanel.addTab("Masking", maskPanel);
                trackTabPanel.addTab("Tracking", trackPanel);
         private void buildWindow() {
                Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
                setResizable(true);
                setTitle("Tracking");
                setVisible(true);
                getContentPane().add(trackTabPanel, BorderLayout.CENTER);
                setBounds(10, 10, 280, 180);
                addComponentListener(new ComponentAdapter () {
                                              public void componentMoved(ComponentEvent evt) {
                                                   Component c = (Component)evt.getSource();
                                                   c.repaint();
         private void setIcon() {
         public void actionPerformed(ActionEvent evt) {
         public void savePreferences(Properties prefs) {
                prefs.put("multiplier", multiplier.getText());
                prefs.put("threshold", threshold.getText());
                prefs.put("dilate2x", (dilate2.isSelected())?"true":"false");
                prefs.put("particleBK", (partBK.isSelected())?"true":"false");
                prefs.put("minPartSize", minPartSize.getText());
                prefs.put("xScale", xScale.getText());
                prefs.put("yScale", yScale.getText());
                prefs.put("tInterval", tInterval.getText());
                prefs.put("maxDif", maxDif.getText());
                prefs.put("minTrack", minTrack.getText());
                prefs.put("difCoef", difCoef.getText());
         public void setScale (double value,String unit) {
         public static void main(String[] args) {
              JFrame mFrame = new JFrame();
              JDesktopPane jdp = new JDesktopPane();
              trackWindow2 tw = new trackWindow2();
              jdp.add(tw);
              mFrame.setContentPane(jdp);
              mFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
              mFrame.setSize(new Dimension(300,250));
              mFrame.setVisible(true);
    }

  • Multiple components in a gridbag layout cell

    Is it possible to have two components in one cell of a grid bag layout? Say, a textfield box and some text to its immediate right? If so, can someone post an example? Thanksmuch.

    Put the textfield and a label with the text in a panel. Then add that panel to your gridbag cell.

  • Get layout-position of a component

    Hi!
    i have a frame applcation with two toolbars.
    they can both be dragged to a new position in the borderlayout with the mouse. it was'nt easy to have no problems with the layout-manager when they are set to a new position.
    now i want so save the alignment of the toolbars for the next start of the application.
    how do i get the current alignment of a toolbar?
    the toolbar is an attribute of the class, i know the panel, and that is no other component in the north, south, east or west.
    for example the best thing would be to retrieve
    String layoutPosition = toolbarPanel.whereTheHellIsTheToolbar(toolbar);
    (e.g. "BorderLayout.SOUTH")
    to serialize this string and to initialize at the next start
    add(toolbar, layoutPosition);
    thanks for any help in advance.
    Rainer

    Not sure, but this thread might help:
    http://forum.java.sun.com/thread.jsp?forum=57&thread=340326

  • Gridbag layout - set size

    can I set a pixel width size for the cell of a gridbag?

    Set the minimum width and height as well as preferred width and height and let the layout manager take care of the size.
    You can not set the absolute width and height because components under GridbagLayout do not have adjustable size. Their size is determinded by layout manager, and the only thing you can set is the layout constraints and minimum/maximum/preferred size.

Maybe you are looking for

  • Virtual Cube with DTP for direct access

    Hello experts, I would like to hear about some tips to improve performance on reporting over this kind of infoprovider. If there were no performance impact in reporting then this cubes would be perfect to face the requirement we are facing. So I woul

  • How can I find and download Media Encoder in Creative Cloud?

    I have some MOV files that won't open in Premeire Pro. i thought I'd try Media Encoder, but when i get to my Creative Cloud download page, Media Encoder is not there.

  • How to Modify Search URL's in Forums?

    OK, this has been plaguing me for a while now, but had time on my hands, so will ask for some assistance from the forum gurus here. Problem: I use the Forum Search (yeah, I know that the name could be better... ), to locate many articles to link to.

  • Rotated thumbnails don't stay rotated in full size

    I recently upgraded to iphoto '11 and have been trying to go through photos and organize. If I look at thumbnails for an event, I can rotate the ones that need it using cmd+r and they appear as I wish, but if I view them full size or in a slideshow t

  • IMovie '14 - importing from iPhoto

    First use of iMovie'14 since upgrade to Retina MacBook so had a quick look at a couple of YouTube tutorials which seem fairly straight forward but when I select the Import function I get a new screen with the list of my everything on my hard drive ap