This discussion is archived
5 Replies Latest reply: Jun 9, 2010 9:04 PM by 843807 RSS

FlowLayout and Jlabels and Jbuttons,  Help!!!!!

843807 Newbie
Currently Being Moderated
Ok, I have an Assignment using the FlowLayout only!!. I have been readin all the tutorials and documentation regarding the FlowLayout. Yet it seems I have a problem. Within my prrogram I am to position the buttons on the right and postion the text on the left.

My question is: Is there a way to postion each component separtely using FlowLayout. Can yoo position the Jlabels to the left and the JButtons to the right?
I know other layouts would be easier but our proffessor wants us to use the flowlayout for this assignment.

Here is my code
import javax.swing.*; 
import java.awt.*;
import java.awt.FlowLayout;
import java.awt.Font;
               
               public class VideoStore extends JFrame {
                    JButton b1, b2, b3;
                    JLabel movie1,movie2,movie3,header;
               public VideoStore (String title) {
                    
                    super (title);
                    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    FlowLayout video = new FlowLayout(FlowLayout.LEFT);
                   setLayout(video);
                    
                    header = new JLabel("CCAC VideoStore");
                    header.setFont(new Font("Times New Roman", Font.BOLD, 24));
                   add(header);
                   
                   
                   {movie1= new JLabel("Johnson Family Vacation");
                   movie1.setFont(new Font("Courier New", Font.BOLD, 14));
                    add(movie1);
                    b1= new JButton("Buy");
                    video.setAlignment(FlowLayout.RIGHT);
                    add(b1);}
                    
                    movie2= new JLabel("PitchBlack");
                        movie2.setFont(new Font("Courier New", Font.BOLD, 14));
                   add(movie2); 
                    b2 = new JButton ("Buy");
                    add(b2);
                    
                    movie3= new JLabel ("Meet The Fockers");
                   movie3.setFont(new Font("Courier New", Font.BOLD, 14));
                   add(movie3);
                    b3 = new JButton ("Buy");
                    add(b3);
               
               }
               }
Edited by: ConfusedNewb on Apr 20, 2010 8:10 AM
  • 1. Re: FlowLayout and Jlabels and Jbuttons,  Help!!!!!
    843807 Newbie
    Currently Being Moderated
    Flow Layout is the default layout for your SWING components. Add a JPanel to your JFrame and work with he JPanel.

    Since you don't say anything about any requriements except using Flow Layout, just add your 3 components to the JPanel and they will be put in a default arrangement according to the order you put them in.
  • 2. Re: FlowLayout and Jlabels and Jbuttons,  Help!!!!!
    843807 Newbie
    Currently Being Moderated
    Sorry.Requirements are: All of the "Buy" JButtons need aligned to the right and all of the JLabels need aligned to the left. There are 3 movie Jlabels and 3 jbuttons. I just cant get them to align without using the spacing trick. Everything has to fit in one window.

    Can I enclose each of the components within the JPanel. In other words Can I have The movie title and the buyJbutton enclosed with one panel or 3 seperate panels.

    Sorry about the grammar. I am new to java and havent got the lingo down yet.

    Thanks
  • 3. Re: FlowLayout and Jlabels and Jbuttons,  Help!!!!!
    843807 Newbie
    Currently Being Moderated
    Yes, you can, but what I was originally saying was not to try to layout your GUI in the display container (your JFrame). It's good practice to add your components to a JPanel which is added to the JFrame.
  • 4. Re: FlowLayout and Jlabels and Jbuttons,  Help!!!!!
    843807 Newbie
    Currently Being Moderated
    ConfusedNewb wrote:
    Sorry.Requirements are: All of the "Buy" JButtons need aligned to the right and all of the JLabels need aligned to the left. There are 3 movie Jlabels and 3 jbuttons. I just cant get them to align without using the spacing trick. Everything has to fit in one window.
    This should be layed out with 2 JPanels, one for the Labels and one for the Buttons. Since FlowLayout will lay the 2 JPanels side by side, you will get the separation that you need. You just need to justify the Label and Button Panels appropriately and make the outer container and Panels appropriate widths to allow the layout to function as you need.
  • 5. Re: FlowLayout and Jlabels and Jbuttons,  Help!!!!!
    843807 Newbie
    Currently Being Moderated
    use panels in the frames it helps out so much

    u would have

    class whatever
    {

    create all ur frames,buttons,etc.

    JFrame frame;
    JPanel panel;
    JButton button;

    public void menu()
    {

    create whatever
    frame= new JFrame("");

    panel= new JPanel();
    then set a layout for ur panel like for ex. flowlayout

    button= new JButton();

    then add whatever to the panel like if u created a button

    panel.add(button);

    then panel added to the frame

    frame.add(panel);
    }

    }

    panels work so much easier

    Edited by: babojee on Jun 10, 2010 4:03 AM