5 Replies - 499 Views - Last Post: 15 July 2011 - 04:01 AM Rate Topic: -----

#1 ThisIsPathetic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-November 10

Java Swing GUI GridBagLayout Problem

Posted 14 July 2011 - 01:37 PM

I can't seem to figure this out for the life of me... I've been at it for a couple hours, and read a ton of tutorials.

I'm trying to use a GridBagLayout to make a design like the picture below... But my issue is the ComboBox and Label sizes don't seem to like me... and they always end up the length of the however long the lists/text is...

I've been messing with the constraints before adding them to the panel, but whenever I get one thing right, like anchored to the left... they go off center. Or when I finally get them aligned... They don't anchor anymore.

Any insight?? If someone would be willing to help me out with just the start of the layout, I would be much appreciated... once I have the right string of code, I can usually learn how it works from there.

any help is very much appreciated :)

Thanks!

Here's the layout I'm going for...


and even though I'm not to the point yet, I need to use some kind of component to draw on... the program allows the user to use the menus on the right to make shapes and such... any suggestions??

http://i.imgur.com/XPGLZ.jpg

Is This A Good Question/Topic? 0
  • +

Replies To: Java Swing GUI GridBagLayout Problem

#2 StuckInJava  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 143
  • Joined: 11-November 10

Re: Java Swing GUI GridBagLayout Problem

Posted 14 July 2011 - 01:41 PM

Use TableLayout.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8024
  • View blog
  • Posts: 31,149
  • Joined: 06-March 08

Re: Java Swing GUI GridBagLayout Problem

Posted 14 July 2011 - 02:33 PM

TableLayout is not part of the API (at least 6) so you will have to download the .jar from Sun/Oracle

Not very kind of you StuckInJava to haven't mentioned that. The OP might have search for hours in the API.

This post has been edited by pbl: 14 July 2011 - 02:34 PM

Was This Post Helpful? 0
  • +
  • -

#4 ThisIsPathetic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-November 10

Re: Java Swing GUI GridBagLayout Problem

Posted 14 July 2011 - 02:42 PM

View Postpbl, on 14 July 2011 - 02:33 PM, said:

TableLayout is not part of the API (at least 6) so you will have to download the .jar from Sun/Oracle

Not very kind of you StuckInJava to haven't mentioned that. The OP might have search for hours in the API.


To be honest, I looked into it... aka 'googled' it, and realized that it wasn't in the standard API, and then just ignored the fact.

I've been doing a lot of research about the GridBagLayout, so switching layouts, especially to one that looks almost as complex, isn't really in my timeline.

Just lookin for help with the GridBagLayout.
Was This Post Helpful? 0
  • +
  • -

#5 immeraufdemhund  Icon User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: Java Swing GUI GridBagLayout Problem

Posted 14 July 2011 - 04:38 PM

I find it very helpful to make a method that adds components to my container. It is using the gridbaglayout.. see if you can make sense of it, i have to get going so I will explain later

    AddComponent(jLabel1,mainFrame,     1, 0, 1, 1,0.1, 0.0, 0);
    AddComponent(jLabel2,mainFrame,     3, 0, 1, 1,0.1, 0.0, 0);
    AddComponent(serialField, mainFrame,2, 0, 1, 1,0.35,0.0, 1);
    AddComponent(repairField, mainFrame,4, 0, 1, 1,0.35,0.0, 1);
    AddComponent(goButt,mainFrame,      5, 0, 1, 1,0.1, 0.0, 0);
    AddComponent(textButt,mainFrame,    0, 1, 1, 1,0.1, 0.0, 0);
    AddComponent(JS,mainFrame,          6, 1, 1, 5,0.1, 1.0, 3);
    AddComponent(picPanel,mainFrame,    1, 1, 5, 5,0.8, 1.0, 3);

  private void AddComponent(Component comp, Container cont,int gridX, int gridY,
          int heightX, int heightY,double weightX, double weightY, int fill)
  {
    GridBagConstraints GBC = new GridBagConstraints();
    int[] filler = {GridBagConstraints.NONE,GridBagConstraints.HORIZONTAL,
      GridBagConstraints.VERTICAL,GridBagConstraints.BOTH};
    GBC.gridx = gridX;
    GBC.gridy = gridY;
    GBC.gridwidth = heightX;
    GBC.gridheight = heightY;
    GBC.weightx = weightX;
    GBC.weighty = weightY;
    GBC.fill = filler[fill];
    GBC.insets = new java.awt.Insets(0, 5, 0, 5);
    cont.add(comp,GBC);
  }



it is a very useful layout to use. I love using it, but it can be very tricky to get the weights and the heights and widths right. good luck.
Was This Post Helpful? 0
  • +
  • -

#6 immeraufdemhund  Icon User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: Java Swing GUI GridBagLayout Problem

Posted 15 July 2011 - 04:01 AM

ok so I said I would be back, I just didn't expect it to take me all night. So the basics of grid bag layout is that you can design your frame to look kinda like an excel sheet. You can put different components (buttons, panels, labels..etc) into different cells. You can specify how wide and how tall each cell can be, but keep in mind you will be changing the width of the entire column, and the height of the entire row. You can make components span multiple columns or multiple rows or both. For the most part the size of the cell is determained by the prefered size of each component. But you can tell the component to force it self to the entire width of the cell, or the entire height of the cell, or both. None is implied as default if memory serves me.

the best bet is to play around with it. The nice thing about the code above that I provided is it will work on any frame. just use the AddComponent(...) method to add things to that frame and you are good to go. Does this help you?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1