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

Thread: Aligning labels

  1. #1
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Aligning labels

    Hi,
    I am new to using GUI, and am trying to make a simple display box. However i ran into a problem. I don't know how to align the labels or buttons in the box. So now all the labels and buttons are centred and on the same line. How do align them to the left an on separate lines?

    /**
     * A class that displays the weather
     * 
     * @author Joel Potts
     * @version 1
     * @last modified 22 september 2011
     */
     
    import java.awt.*;  //Imports files needed for Dialog Windows
    import javax.swing.*;   // Imports predefinded windows
     
    public class WeatherDisplay 
    {
        public static void main (String args[])
        {
        JFrame frame = new JFrame( "Heres Whats Happening In Your Neck of the Woods"); // Sets title for display window
        JLabel daysofWeek = new JLabel("Sunday  Monday  Tuesday  Wednesday  Thursday  Friday  Saturday ", JLabel.CENTER ); // Label for days of the week
        JLabel temperature = new JLabel("78   79      79        77         78       73      72", JLabel.LEFT);   // Makes labelfor the temperatures
        frame.getContentPane().add(daysofWeek);
        frame.getContentPane().add(temperature);
        frame.setSize (400, 70);
        Container content = frame.getContentPane();
        content.setBackground (Color.red);
        content.setLayout(new FlowLayout());
        content.add(new JButton("Nothing Happens If You Click Me"));
        frame.setVisible (true);
     
        }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Aligning labels

    Use a LayoutManager. See the following link for the most appropriate Layouts. Also note that layouts can be nested - JPanel within JPanel with different layouts as needed.
    A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

  3. #3
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Aligning labels

    Hi,
    This wasnt all that helpful... Do you have anything for real noobs (like me ). Most of that went over my head... sorry...

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Aligning labels

    Sorry it wasn't that helpful, but that link was for novices, and its one of the best resources for newcomers to the language to dive into. Amongst the list of layouts in that page, which looks similar to what you wish to have? You mentioned you want labels on new lines....does the BoxLayout look like something you might want?

  5. #5
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Aligning labels

    Yes i was looking at the BoxLayout, but i didnt understand how to use it...

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Aligning labels

    See the following link for details:
    How to Use BoxLayout (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

    The main concept is to create a JPanel, set its layout, then add your components.
    JLabel label1 = new JLabel("Label 1");
    JLabel label2 = new JLabel("Label 2");
     
    JPanel myPanel = new JPanel();//create the panel
    myPanel.setLayout(new BoxLayout(myPanel, BoxLayout.PAGE_AXIS));//set the layout
    myPanel.add(label1);//add the components
    myPanel.add(label2);

    You can combine layouts like this....for instance if you want your labels to be left aligned, create another JPanel, set its layout to BoxLayout (line axis this time), add the label and then add a horizontal glue (see the API and above link), then add this panel to myPanel.

    Hope this helps. Give it a try and post back if you have problems.

  7. #7
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Aligning labels

    Hey, I tried it, but it didnt work. Is this all i need to do, or do i need to make a JFrame aswell?

  8. #8
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Aligning labels

    Quote Originally Posted by pottsiex5 View Post
    Hey, I tried it, but it didnt work. Is this all i need to do, or do i need to make a JFrame aswell?
    Define 'it didn't work'....and yes you need a JFrame - that code snippet was just a demonstration for the Layout to incorporate into your code.

  9. #9
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Aligning labels

    Hey,
    I found a decent tutorial (Complete Java GUI Tutorial - Beginner to Expert! by Donny Nadolny). I went through the whole thing to get a firmer understanding about all the components in GUI, but i ran into a problem... again it was with the layouts.
    My code is:

    import javax.swing.*;
    import java.awt.*;
     
    public class LessonOne {
     
    	public static void main(String[] args) {
    		GridLayout myLayout = new GridLayout(2, 3);
    		setLayout(myLayout);
    		JFrame frame = new JFrame( "Hello World");
    		frame.setSize(100,100);
    		TextField txtName = new TextField(10);
    		Panel myPanel = new Panel();
    		JLabel label = new JLabel ("Hello World");
    		myPanel.add(label);
    		myPanel.add(txtName);
    		frame.getContentPane().add(myPanel);
    		frame.setVisible(true);
     
    	}
     
    }

    however it says that setLayout is undefined. Is there something else i need to import for this to work?
    Thankyou, Joel

  10. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Aligning labels

    The method setLayout is defined by the class Container...it must be called on an object that is or extends the class Container. For example, the JPanel you defined:
    myPanel.setLayout(myLayout);

    Edit: Note you should not mix AWT with Swing. Use the J* classes, for example JPanel rather than Panel, and JTextField rather than TextField.
    Last edited by copeg; September 25th, 2011 at 01:34 PM.

  11. #11
    Member
    Join Date
    Sep 2011
    Posts
    46
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Aligning labels

    Does it make much of a difference on which one i use? I can still mix them to get the extra layouts and all that right?
    Thankyou, by the way for being patient enough to help me

  12. #12
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Aligning labels

    Quote Originally Posted by pottsiex5 View Post
    Does it make much of a difference on which one i use? I can still mix them to get the extra layouts and all that right?
    Thankyou, by the way for being patient enough to help me
    My comment was independent of layouts - mixing the Components should be avoided for reasons you can find by googling. Suffice it to say, Layouts in the links I provided above are find for both AWT and Swing, but I recommend for every class you use that extends Component, it should also extend JComponent.

Similar Threads

  1. labels,textfields,buttons
    By balu424525 in forum AWT / Java Swing
    Replies: 1
    Last Post: June 26th, 2011, 07:44 AM
  2. labels and fields
    By danty in forum AWT / Java Swing
    Replies: 2
    Last Post: June 12th, 2011, 11:48 AM
  3. aligning help
    By Macgrubber in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 16th, 2010, 01:52 AM
  4. Aligning Issues in the Output
    By KiwiFlan in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 15th, 2010, 05:29 AM
  5. Need help understanding GUI screen layouts and labels
    By Bill_H in forum AWT / Java Swing
    Replies: 3
    Last Post: December 2nd, 2009, 11:50 PM