Results 1 to 7 of 7
  1. #1
    maxim is offline Member
    Join Date
    Apr 2008
    Posts
    15
    Rep Power
    0

    Default [SOLVED] GridBagLayout issues

    having some strange issue with gridbaglayout.

    from a code perspective its all correct and it logicly seems like it should work without to much of a problem which at first glance it did. but then i noticed the layout dropped the last row (row 7) off its self. i think it actually painted it behind row 6.

    basically its the fact that a component spans several rows thats causing the problem. so ive commented out that line. just uncomment it and youll see what i mean.

    just copy paste this code to see what i mean

    PHP Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class GuiBug extends JFrame {
    
        public GuiBug() {
    
            setLayout(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();
    
            //span all the elements out verticly
            c.weighty = 1.0;
    
            //make all elements the same width
            c.fill = GridBagConstraints.HORIZONTAL;
    
            //left align all elements
            c.anchor = GridBagConstraints.LINE_START;
            c.gridx = 0;
            c.gridy = 0;
            add(new JLabel("Score"), c);
    
            c.gridx = 0;
            c.gridy = 1;
            add(new JLabel("Carrier"), c);
    
            c.gridx = 0;
            c.gridy = 2;
            add(new JLabel("Battleship"), c);
    
            c.gridx = 0;
            c.gridy = 3;
            add(new JLabel("Destroyer"), c);
    
            c.gridx = 0;
            c.gridy = 4;
            add(new JLabel("Submarine"), c);
    
            c.gridx = 0;
            c.gridy = 5;
            add(new JLabel("PT Boat"), c);
    
            c.gridx = 1;
            c.gridy = 0;
            add(new JTextField("0000"), c);
    
            c.gridx = 1;
            c.gridy = 1;
    
            //this is the line i need enabled but when i do it
            //cuts off the last two buttons !!!
            
    //        c.gridheight = 4; // <----
    
            c.fill = GridBagConstraints.VERTICAL;
            c.anchor = GridBagConstraints.CENTER;
            JProgressBar pBar = new JProgressBar();
            pBar.setOrientation(JProgressBar.VERTICAL);
            pBar.setValue(50);
            add(pBar, c);
    
            c.gridx = 1;
            c.gridy = 5;
            add(new JLabel("Time"), c);
    
            c.gridx = 0;
            c.gridy = 6;
            c.weighty = 0.0;
            c.fill = GridBagConstraints.HORIZONTAL;
            add(new JButton("Pause"), c);
    
            c.gridx = 1;
            c.gridy = 6;
            add(new JButton("Sound"), c);
    
            c.gridx = 0;
            c.gridy = 7;
            add(new JButton("New"), c);
    
            c.gridx = 1;
            c.gridy = 7;
            add(new JButton("Quit"), c);
        }
    
        public static void main(String args[]) {
         GuiBug thisProgram = new GuiBug();
         thisProgram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         thisProgram.setSize(150,300);
         thisProgram.setVisible(true);
    
        }
    }

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    //this is the line i need enabled but when i do it
    //cuts off the last two buttons !!!

    // c.gridheight = 4; // <----
    You set your constraint so that it "covers" four rows, then you put several components one row under the previous. I would expect something to get covered.

    If you don't have an IDE with a form designer, try using a Spreadsheet and just typing values in the cells. To simulate height, color 4 rows of cells. I think that will help you visualize your problem.

    Netbeans Matisse is a nice tool, and it is available in the Eclipse MyEclipse plugin for $60/year (that's what I use.)

    Don't give up on GridBagLayout. Once you really understand it, it is *very* powerful.

  3. #3
    maxim is offline Member
    Join Date
    Apr 2008
    Posts
    15
    Rep Power
    0

    Default

    i hand code all my stuff, i don't learn with a gui doing it for me.

    if you actually read the code you'll see it makes perfect sense.

    yes i cover 4 rows but the elements i have under it start at under those 4 rows is covers. the element in question starts at gridy = 1 and spans 4 rows so it ends at y position 5. the next element under starts at y position 6 so there should be no problem.

    i actually already found a solution my self, it was by using setting the y position to = GridBagConstraints.RELATIVE

    i was just wondering if anyone else knew.

    heres my revised working hand written code after you uncomment the line back in
    PHP Code:
     
    c.gridx = 0;
    c.gridy = GridBagConstraints.RELATIVE;
    add(new JButton("New"), c);
    
    c.gridx = 1;
    c.gridy = GridBagConstraints.RELATIVE;
    add(new JButton("Quit"), c);
    just those 2 extra lines to fix it. you actually only need it once but i put it in twice just to make sure
    Last edited by maxim; 02-12-2009 at 08:50 PM.

  4. #4
    maxim is offline Member
    Join Date
    Apr 2008
    Posts
    15
    Rep Power
    0

    Default

    well if you run it with the modified code and turn back on the commented out line, just like i said to do in my last post you will see it actually works fine. but i guess reading is to hard for you.

    but stick to using gui design tools, they seem to help your layout management skills alot ! /sarcasm

  5. #5
    maxim is offline Member
    Join Date
    Apr 2008
    Posts
    15
    Rep Power
    0

    Default

    haha if you think your trying to help trying to start a flame war is not the way to go about it you just do your self harm.

    and no its not a project for any class just im just making a port of an old Atari-ST game in java because i like programming and like this old game im porting i used to play as a kid. i dont want to add flame to this fire but i feel like i have make my point clear.

    ww*.atarimagazines.com/startv3n2/navalbattle.html

    notice the the layout on the screenshot of the Atari-ST version is the same as the code i posted above.

    yikes a bit of egg on your face there ! :D

    professional programmer ? nothing professional about your posts
    Last edited by maxim; 02-12-2009 at 10:04 PM.

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    You are right. I deleted the last two, because, after I calmed down, they embarrassed me. Good luck with your project.

  7. #7
    maxim is offline Member
    Join Date
    Apr 2008
    Posts
    15
    Rep Power
    0

Similar Threads

  1. GridBagLayout
    By carderne in forum New To Java
    Replies: 8
    Last Post: 01-25-2009, 03:06 PM
  2. GridBagLayout
    By MuslimCoder in forum New To Java
    Replies: 1
    Last Post: 01-15-2009, 09:54 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
  •