Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Beginner question about setHorisontalAlignment!

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Beginner question about setHorisontalAlignment!

    I´m new at java, and world like to place a label on a panels upper left corner.
    Should i use a different LayoutManager?

    Hers a simple examle i can't get to work.
    Probably a simple fix, but i´m bad at this

    Thanks for your time !

    Best Regards
    Oskar

    import javax.swing.*;
    import java.awt.*;
     
     
    public class test {
     
        public static void main(String[] args) {
     
            JFrame frame = new JFrame("TrippTrapp");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            JPanel panel = new JPanel();
            panel.setPreferredSize(new Dimension(300, 300));
            panel.setBackground(Color.red);
     
            JLabel label = new JLabel("hej");
            label.setHorizontalAlignment(SwingConstants.LEFT);
     
     
            panel.add(label);
     
     
     
            frame.getContentPane().add(panel);
            frame.pack();
            frame.setVisible(true);
     
        }
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Beginner question about setHorisontalAlignment!

    setHorizontalAlignment will place your JLabel's text in the for left portion of the JLabel, but this won't have any visible effect on your GUI since your JLabel is only large enough to hold the text it contains.

    I would suggest that yes, you use a different layout manager for the JPanel since its default FlowLayout will place the JLabel at the top-center position. Consider a GridBagLayout for this. With an appropriate GridBagConstraint, you should be able to place your JLabel where you want it. Another option is to nest JPanels that use simpler layouts such as both using BorderLayout, and placing one JPanel in the BorderLayout.NORTH position of the main JPanel, and then placing the JLabel in the BorderLayout.WEST position of the north JPanel.

  3. The Following User Says Thank You to curmudgeon For This Useful Post:

    oskar.svard (November 25th, 2012)

  4. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    14
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Beginner question about setHorisontalAlignment!

    You can use "null" layout....then use setBounds() method to specify exactly where you want to display your component...
    like....
    setLayout(null);
    component.setBounds(a,b,c,d);

    here--a= starting x coordinate,b= starting y coordinate,c=length of component along x axis,d=height of component along y axis.

    hope its helpful

  5. The Following User Says Thank You to rougeking For This Useful Post:

    oskar.svard (November 27th, 2012)

  6. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Beginner question about setHorisontalAlignment!

    Quote Originally Posted by rougeking View Post
    You can use "null" layout....then use setBounds() method to specify exactly where you want to display your component...
    like....
    setLayout(null);
    component.setBounds(a,b,c,d);

    here--a= starting x coordinate,b= starting y coordinate,c=length of component along x axis,d=height of component along y axis.

    hope its helpful
    I urge you to ignore rougeking's well-meaning but misleading help. null layout should be avoided unless absolutely necessary (such as for some animations and for dragging components). If you use null layout, you will make your GUI *very* hard to update and expand.

  7. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    14
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Beginner question about setHorisontalAlignment!

    ya I too agree with curmudgen.....every layout has got it drawbacks....
    I just gave it so that u'll have an idea about null also.....
    use it only there are less components,frame is set to nonresizable....and more over null layout doesnt guarantee same amount of pixel count on different platforms.

  8. The Following User Says Thank You to rougeking For This Useful Post:

    oskar.svard (November 27th, 2012)

  9. #6
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    My Mood
    Confused
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Beginner question about setHorisontalAlignment!

    Thanks everyone! You guys have been a big help! I solved the problem using GridBagLayout! Thanks for the hint!
    Best Regards
    Oskar

  10. #7
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Beginner question about setHorisontalAlignment!

    Great -- glad you've got it working!

Similar Threads

  1. Beginner question about for loops...
    By ninjaBob in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 29th, 2012, 03:57 PM
  2. Loop Question - Very new beginner
    By Callcollect in forum Loops & Control Statements
    Replies: 12
    Last Post: January 18th, 2012, 04:01 AM
  3. Beginner Question
    By jrt224 in forum Loops & Control Statements
    Replies: 1
    Last Post: March 10th, 2011, 12:56 PM
  4. [SOLVED] Simple question from a beginner
    By jimmylee7706 in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2011, 09:57 PM
  5. beginner's question
    By bardd in forum Java Theory & Questions
    Replies: 5
    Last Post: September 14th, 2010, 04:02 PM