Results 1 to 8 of 8

Thread: Java Component Alignment Trouble

  1. #1
    Custom User
    Join Date
    Oct 2001
    Posts
    503

    Java Component Alignment Trouble

    I was wondering if anyone can shed any light on the following problem I've been having. I finally stumbled upon the GridBagLayout manager and now do not need to suffer stretched components being displayed on my screen, but the problem that I'm having is that the GridBagLayout is centering the components on the screen when I want them placed in the top left hand corner like this ascii diagram shows:

    Code:
    I currently have the following showing:
     ------------------
    |                  |
    |	           |
    |      Label1	   |
    |      Label2      |
    |                  |
    |	           |
     ------------------
    
    rather than:
    
     ------------------
    |Label1            |
    |Label2	           |
    |       	   |
    |                  |
    |                  |
    |	           |
     ------------------
    I'm not sure how to change this as I cannot find any methods or public vars that I can use/change to alter where the components are placed. And yes, I have searched through the java api extensively and searched google and the sun java site.

    Thanks,

    ac

  2. #2
    Banned
    Join Date
    Sep 2004
    Posts
    305
    Make a panel and then use a BorderLayout and then for the location use PAGE_START... look below:

    Code:
    mainPanel.setLayout(new BorderLayout());        
    mainPanel.add(topLeftButton, BorderLayout.PAGE_START);

  3. #3
    Senior Member
    Join Date
    May 2003
    Posts
    226
    My suggestion: Use a BoxLayout

    Swing tutorial

  4. #4
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    Death_Knight, using a BoxLayout solved the problem I posted above, the only problem I have now is that before the labels stretched horizontally so that if you changed the background colour of the label to be different from the background colour of the panel it was added to, the colour would stretch to the end of the panel, now that's not happening. meh! Solved one problem and made another. :P

    Anyhow, if you've got a solution I'd be glad to hear it - meanwhile I'll keep looking myself.

    ac

  5. #5
    Senior Member
    Join Date
    May 2003
    Posts
    226
    You can try reading this

    Take note of this
    What if none of the components has a maximum width? Well, if all the components have identical X alignment, then all components are made as wide as their container. If the X alignments are different, then any component with an X alignment of 0.0 (left) or 1.0 (right) will be smaller. All components with an intermediate X alignment (such as center) will be as wide as their container. Here are two examples:

  6. #6
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    Amazing...I had actually skimmed that and tried changing the Maximum size of my labels but because I didn't set the alignment of them, it didn't change the size correctly. For some reason I just assumed it couldn't have been an alignment issue.

    BTW -- if anyone is interested, I'm attaching the program. It's not nearly finished yet, but just so that you can see what I was trying to do. To be honest, the gui is not meant to be taken seriously, it was meant to be a bad joke (one that I won't explain); in fact, I find it ironic that in creating this gui, I'm basically emulating an old dos application :P

    The most interesting part of the program is the Menu class which creates a menu by parsing the code written in the menu file (which is changed at the moment by changing the value of a constant in the Menu class.

    I want to stress that the program isn't finished...I've still got to add stuff and clean up the mess that MenuGame.java got into from me trying out different layout managers and weird things.

    The two menu files included are MENU.txt and JOYCE.tst. And if you're wondering where I got the sad idea for the gui, it's the till system we use at my work (or my copy of it)

    ac

  7. #7
    Senior Member
    Join Date
    May 2003
    Posts
    226
    Did you try using setBound ? Perharps you can try using JBuilder from Borland. This will help you in GUI development

  8. #8
    Custom User
    Join Date
    Oct 2001
    Posts
    503
    I didn't, but it doesn't matter because I got the program to do what I want now. I've tried using JBuilder before (just messing about really), but I don't like the way it works (from what I've seen). I quite like the fact that in java it's relatively easy to code fairly complicated GUIs by hand.

    ac

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •