Results 1 to 7 of 7
Thread: [SOLVED] GridBagLayout issues
- 02-12-2009, 12:47 PM #1
Member
- Join Date
- Apr 2008
- Posts
- 15
- Rep Power
- 0
[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); } }
- 02-12-2009, 05:36 PM #2
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.//this is the line i need enabled but when i do it
//cuts off the last two buttons !!!
// c.gridheight = 4; // <----
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.
- 02-12-2009, 06:34 PM #3
Member
- Join Date
- Apr 2008
- Posts
- 15
- Rep Power
- 0
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
just those 2 extra lines to fix it. you actually only need it once but i put it in twice just to make surePHP 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);Last edited by maxim; 02-12-2009 at 07:50 PM.
- 02-12-2009, 07:47 PM #4
Member
- Join Date
- Apr 2008
- Posts
- 15
- Rep Power
- 0
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
- 02-12-2009, 09:01 PM #5
Member
- Join Date
- Apr 2008
- Posts
- 15
- Rep Power
- 0
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 postsLast edited by maxim; 02-12-2009 at 09:04 PM.
- 02-12-2009, 09:06 PM #6
You are right. I deleted the last two, because, after I calmed down, they embarrassed me. Good luck with your project.
- 02-12-2009, 09:11 PM #7
Member
- Join Date
- Apr 2008
- Posts
- 15
- Rep Power
- 0
Similar Threads
-
GridBagLayout
By carderne in forum New To JavaReplies: 8Last Post: 01-25-2009, 02:06 PM -
GridBagLayout
By MuslimCoder in forum New To JavaReplies: 1Last Post: 01-15-2009, 08:54 PM -
GridBagLayout
By newtojava7 in forum New To JavaReplies: 2Last Post: 03-07-2008, 12:16 AM -
GridBagLayout...please help
By newtojava7 in forum Advanced JavaReplies: 1Last Post: 02-17-2008, 01:16 AM -
gridbaglayout
By newtojava7 in forum New To JavaReplies: 4Last Post: 01-27-2008, 08:03 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks