Results 1 to 10 of 10
  1. #1
    kahaj is offline Member
    Join Date
    Sep 2009
    Posts
    12
    Rep Power
    0

    Default Centering inside a JFrame

    I'm trying to make an app center when it comes up. I'm also trying to make the TextArea inside of the app centered. I've tried several things but have been unable to get any of them to work. I've looked up several things online to try and haven't been able to get any of them to successfully work.

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    class SimpleJavaGUI extends JFrame
    {
        public JTextArea theTextArea;
    
        public SimpleJavaGUI()
        {
            super("mainForSimpleGUI");
            setLayout(new FlowLayout() );
            theTextArea = new JTextArea(2, 20);
            theTextArea.setAlignmentX(CENTER_ALIGNMENT);
            theTextArea.setAlignmentY(CENTER_ALIGNMENT);
            theTextArea.setText(" Follow the white rabbit.");
            theTextArea.setToolTipText("This is a text area.");
            theTextArea.setEditable(false);
            theTextArea.setDisabledTextColor(Color.BLACK);
            theTextArea.setLineWrap(true);
            theTextArea.setBorder(BorderFactory.createLineBorder(Color.black) );
            add(theTextArea);
        }
    }
    I left some coding that I had commented out after I couldn't get it to work.
    Java Code:
    import javax.swing.*;
    import java.awt.*;
    
    public class mainForSimpleGUI
    {
        public static void main(String[] args)
        {
            SimpleJavaGUI labelFrame = new SimpleJavaGUI(); //creates SimpleJavaGUI
            labelFrame.setLocationRelativeTo(null);
            labelFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            labelFrame.setSize(300, 300);
            labelFrame.setVisible(true);
        }
    }
    /*
    class SimpleJavaGUI extends JFrame
    {
        public JTextArea theTextArea;
    
        public SimpleJavaGUI()
        {
            super("mainForSimpleGUI");
            setLayout(new FlowLayout() );
            theTextArea = new JTextArea(2, 20);
            theTextArea.setLocation(30, 30);
            //theTextArea.setAlignmentX(CENTER_ALIGNMENT);
            //theTextArea.setAlignmentY(CENTER_ALIGNMENT);
            theTextArea.setText(" Follow the white rabbit.");
            theTextArea.setToolTipText("This is a text area.");
            theTextArea.setEditable(false);
            theTextArea.setDisabledTextColor(Color.BLACK);
            theTextArea.setLineWrap(true);
            theTextArea.setBorder(BorderFactory.createLineBorder(Color.black) );
            add(theTextArea);
        }
    }
    */

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Specify what the problem is.
    What is the code doing vs what it is supposed to be doing?

  3. #3
    kahaj is offline Member
    Join Date
    Sep 2009
    Posts
    12
    Rep Power
    0

    Default

    The app is not centering on the screen when it appears, nor is the TextArea centering inside of the app.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    To center the app, use setPreferredSize, not setSize, pack the app, and only then set the location relative to null:
    Java Code:
        public static void main(String[] args)
        {
            SimpleJavaGUI labelFrame = new SimpleJavaGUI(); //creates SimpleJavaGUI
            labelFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            labelFrame.setPreferredSize(new Dimension(300, 300));
            labelFrame.pack();
            labelFrame.setLocationRelativeTo(null);
            labelFrame.setVisible(true);
        }
    One way to center a single component is to have the Container that holds it use a GridBagLayout.

  5. #5
    kahaj is offline Member
    Join Date
    Sep 2009
    Posts
    12
    Rep Power
    0

    Default

    What is the difference between setSize and setPreferredSize?

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    That is why they wrote the API specs for those methods.
    All you have to do is to read them.

  7. #7
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    Centering the window
    setLocationRelativeTo(null) worked for me, but it centered the upper lefthand corner.
    Here is a method that has worked for me in the past:
    blog.codebeach.com/2008/02/center-dialog-box-frame-or-window-in.html

    Centering text in a label
    java.sun.com/docs/books/tutorial/uiswing/components/label.html

    Centering a component in a window
    GridBagLayout can do the job. Usually it is easier to write your own LayoutManager instead of fighting with AWT or Swing.

    Setting size
    Preferred, minimum and maximum size are hints to the layout manager as to how big the component would like to be. setSize() is what the layout manager calls after it has decided how big it can allow the component to be.

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    That is why they wrote the API specs for those methods.
    I'm the first to recommend reading the API, but its not always very helpfull as it isn't in this case.

    What is the difference between setSize and setPreferredSize?
    You need to understand how layout managers work to understand the difference. The preferred size is a suggestion for the layout manager. Based on the suggestion the layout manager will use its rulse to set the size and location of the component. Read this section from the Swing tutorial for more information:

    java.sun.com/docs/books/tutorial/uiswing/layout/using.html#sizealignment

  9. #9
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    5

    Default

    Writing a layout manager is often straightforward.
    For your example, replace "setLayout(new FlowLayout() );" with:
    Java Code:
    	setLayout(new LayoutManager() {
    		public void addLayoutComponent(String name, Component comp) {}
    		public void removeLayoutComponent(Component comp) {}
    		public Dimension preferredLayoutSize(Container parent) {
    			return theTextArea.getPreferredSize();
    		}
    		public Dimension minimumLayoutSize(Container parent) {
    			return theTextArea.getMinimumSize();
    		}
    		public void layoutContainer(Container parent) {
    			int pW = parent.getWidth(), pH = parent.getHeight();
    			int tW = theTextArea.getPreferredSize().width,
    			    tH = theTextArea.getPreferredSize().height;
    			theTextArea.setBounds((pW-tW)/2, (pH-tH)/2, tW, tH);
    		}
    	});

  10. #10
    kahaj is offline Member
    Join Date
    Sep 2009
    Posts
    12
    Rep Power
    0

    Default

    Alright, changed the layout manager and a few others things. It works great now. Man, Java sure does go down to the minute details of everything.

Similar Threads

  1. Launch native app inside a JFrame???
    By benedums in forum Advanced Java
    Replies: 6
    Last Post: 02-25-2011, 03:23 PM
  2. How to add JFrame inside JPanel
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 8
    Last Post: 12-13-2009, 08:41 PM
  3. Passing data from one JFrame to another JFrame
    By tarami in forum New To Java
    Replies: 3
    Last Post: 08-06-2009, 05:44 PM
  4. Replies: 4
    Last Post: 04-22-2009, 11:02 PM
  5. Centering text of output
    By dch414 in forum New To Java
    Replies: 2
    Last Post: 10-02-2008, 10:08 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
  •