Results 1 to 6 of 6
  1. #1
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Question Vertical alignment

    I want to vertical align the buttons, labels in my Jpanel (Jframe).
    But according to this code I am able just to vertically align, but not centered.
    What should I do to do it, as now it is displayed as left-aligned.
    As well frame.setSize(800, 600) is not working - the size is very small.
    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    import javax.swing.BoxLayout;
    //import javax.swing.Container;
    import java.awt.GridLayout; 
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.Component;
    import java.awt.Container;
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    
    public class JFramePack {
    
      public static void main(String[] args) {
        JFrame frame = new JFrame();
        JFrame.setDefaultLookAndFeelDecorated(true);
        frame.setTitle("Indexator");
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.setLayout(new GridLayout(0,1));
        JPanel contentPane = new  JPanel();
        contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
        frame.add(contentPane, BorderLayout.CENTER);
        //contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS));
        JLabel label = new JLabel("Welcome");
        label.setAlignmentY(Component.CENTER_ALIGNMENT);
        //frame.add(label);
        JButton buttonInd = new JButton("Indexation");
        buttonInd.setAlignmentY(Component.CENTER_ALIGNMENT);
        buttonInd.setHorizontalAlignment(JButton.CENTER);
        //buttonInd.setVerticalAlignment(JButton.CENTER);
        contentPane.add(label);
        contentPane.add(buttonInd);
        
        buttonInd.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {
                Filewalker.walk("c:\\" );
             }
          });
           
        frame.pack();
        frame.setVisible(true);
      }
    }

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    878
    Rep Power
    11

    Default Re: what layout to use for vertical alignment?

    comment frame.pack() .if you use frame.setSize() no need to use frame.pack()
    Ramya:cool:

  3. #3
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: what layout to use for vertical alignment?

    Thanks for note about of comments - setSize() works.
    But why my two components is vertically aligned, but are not in the centre - but in the left upper corner.

  4. #4
    ark
    ark is offline Senior Member
    Join Date
    Apr 2015
    Posts
    288
    Rep Power
    5

    Default Re: what layout to use for vertical alignment?

    I have another simple question -- when I moved all code from main method to the JFramePack (that I extended Jframe) empty arguments constructors except some static fields (as I need to assign some text to them with Listeners) - created the one object of JFramePack in main () --I got empty Jframe after running the file- and just after some moving (resizing) of frame borders, I got visible other components such as JTextArea, JLabels, Jbuttons. What is the reason -- maybe absence of pack() method or so?

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: what layout to use for vertical alignment?

    First, this thread is over 7 years old. I doubt the OP needs help anymore. Second, don't ask a new question on the same thread (or especially someone elses). Please start your own.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: Vertical alignment

    Moved to own thread.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Vertical menu
    By Kligham in forum New To Java
    Replies: 13
    Last Post: 07-09-2014, 02:07 PM
  2. Vertical box and positioning
    By aborgeld in forum New To Java
    Replies: 9
    Last Post: 04-01-2011, 07:33 PM
  3. Vertical menubar
    By Java Unknown in forum New To Java
    Replies: 0
    Last Post: 02-22-2011, 05:50 AM
  4. vertical sync
    By afraidofdark in forum Java 2D
    Replies: 0
    Last Post: 03-25-2010, 08:54 PM
  5. what layout to use for vertical alignment?
    By dim_ath in forum AWT / Swing
    Replies: 6
    Last Post: 01-20-2008, 05:28 PM

Posting Permissions

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