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 10 of 10

Thread: Alignment issues

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    18
    My Mood
    Stressed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Alignment issues

    I'm tryint to get my label to align top left and it aligns top center.

           JLabel label1 = new JLabel("Text here", JLabel.LEFT);
           label1.setVerticalAlignment(JLabel.TOP
           jp1.add(label1);


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Alignment issues

    Code you post should be in SSCCE form, that way we know exactly what you're doing. For example, what layout are you using?

    Until I see an SSCCE, I can only point you towards this tutorial: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2011
    Posts
    18
    My Mood
    Stressed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Alignment issues

    I'm sorry, but I don't know how to post in SSCCE. I'm not getting any errors when I run it, it just doesn't align properly. Would it be better to post all of the code?

    Thanks

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Alignment issues

    No. If you don't know how to post an SSCCE, that's why I provided a link that explains exactly how to do it. Check it out and let me know if you have any questions.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Mar 2011
    Posts
    18
    My Mood
    Stressed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Alignment issues

    I did check it out. I'm not sure how to do it.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Alignment issues

    What do you mean? You eliminate any code that doesn't have to do with your problem, and you put it in a single compilable/runnable class so other people can simply copy and paste it into their own IDE and run it.

    In your case, we'd just need a single JFrame holding a single JLabel in exactly the way you're trying to do it in your program.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Mar 2011
    Posts
    18
    My Mood
    Stressed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Alignment issues

    import javax.swing.*;
    import javax.swing.JLabel;
    import javax.swing.plaf.metal.MetalIconFactory;
     
    public class Alignment {
      protected JTabbedPane tabPane;
      public Alignment() {
        tabPane = new JTabbedPane();
        tabPane.add(new JLabel("One", JLabel.LEFT), "First");
        Icon warnIcon = MetalIconFactory.getTreeComputerIcon();
        JLabel label1 = new JLabel(warnIcon);
        label1.setHorizontalTextPosition(JLabel.LEFT);
        label1.setVerticalTextPosition(JLabel.TOP);
      }
      public static void main(String[] a) {
        JFrame f = new JFrame("Tab Demo");
        f.getContentPane().add(new Alignment().tabPane);
        f.setSize(400, 200);
        f.setVisible(true);
      }
    }

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Alignment issues

    Yeah, the problem is that you aren't really using layouts how you should be. Which JLabel are you trying to align? You don't even add label1 at all.

    This uses a BorderLayout to get your JLabel to the top of a JPanel:
    import java.awt.BorderLayout;
    import javax.swing.*;
     
    public class Alignment {
      protected JTabbedPane tabPane;
      public Alignment() {
        tabPane = new JTabbedPane();
     
        JLabel labelOne = new JLabel("One", JLabel.LEFT);
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(labelOne, BorderLayout.NORTH);
        tabPane.add(panel);
      }
      public static void main(String[] a) {
        JFrame f = new JFrame("Tab Demo");
        f.getContentPane().add(new Alignment().tabPane);
        f.setSize(400, 200);
        f.setVisible(true);
      }
    }
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. The Following User Says Thank You to KevinWorkman For This Useful Post:

    fride360 (March 10th, 2011)

  10. #9
    Junior Member
    Join Date
    Mar 2011
    Posts
    18
    My Mood
    Stressed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Alignment issues

    Thanks, that's what I was trying to do. How do you name the tab?

  11. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Alignment issues

    You had that part right the first time.

    tabPane.add(panel, "Tab One");
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. The Following User Says Thank You to KevinWorkman For This Useful Post:

    fride360 (March 10th, 2011)

Similar Threads

  1. Output Alignment
    By Fahmi in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: August 22nd, 2010, 04:14 PM
  2. Output Alignment
    By xdrechsler in forum Loops & Control Statements
    Replies: 1
    Last Post: August 18th, 2010, 10:26 AM
  3. The positioning and alignment of the text on the paper to be printed
    By java_fledgeling in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: June 8th, 2010, 08:54 PM
  4. Alignment
    By angelaffxmaniac in forum Java Theory & Questions
    Replies: 1
    Last Post: May 13th, 2010, 08:55 AM
  5. Replies: 1
    Last Post: March 31st, 2009, 02:49 PM