Results 1 to 8 of 8
  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Have a JTextArea and JTextField in one JFrame

    I am essentially recreating a command-line interface.

    I want a JTextArea (output) above a JTextField (input).


    However I cannot get both of them to appear for some reason, I can't even explain the layout, I can never see both of them in the frame, it is one or the other.



    I have coloured them in different colours purely so I can see them. This is my code for it:


    Java Code:
    textOutput = new JTextArea();
            textInput = new JTextField();
            
            add(textOutput);
            add(textInput);
            
            textOutput.setEditable(false);
            textOutput.setBackground(Color.GRAY);
            textOutput.setSize(((width/3)*2), ((height/3)*2));
            
            textInput.setBackground(Color.white);
            textInput.setSize((width/3), (height/3));

    What am I doing wrong?



    Note: height and width are the dimensions of the JFrame, therefore I want the JTextArea to have 2/3 of it and the JTextField to have 1/3 of it

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: Have a JTextArea and JTextField in one JFrame

    Recommended reading: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    What layout are you using? Hint: What actually happens when you add something to a JFrame? What is a content pane? What is its default layout?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: Have a JTextArea and JTextField in one JFrame

    Quote Originally Posted by KevinWorkman View Post
    Recommended reading: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    What layout are you using? Hint: What actually happens when you add something to a JFrame? What is a content pane? What is its default layout?


    I've just been doing some research using the link you provided, I have learnt that I was adding the components to the JFrame itself instead of the JFrames layout (which by default is the border layout).

    Therefore, for my arrangement, I will want to assign the JTextArea to the top area of the layout and I will want to assign the JTextField to the bottom area.
    By having no centre areas (i.e setting their height to 0), the top and bottom will "touch" and give me my desired layout.


    Am I correct with this?
    I am very new to swing, I have only dealt with command-line outputs up until now.


    if this is correct, how to I add each of my components to the top and bottom areas?

    I saw that oracle named the areas PAGE_START and PAGE_END and therefore thought that I may have to do it by doing something like this:
    Java Code:
    PAGE_START.add(textOutput);
    PAGE_END.add(textInput);
    (textOutput is the JTextArea and textInput is the JTextField).


    As I am sure you know, that doesn't work.

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

    Default Re: Have a JTextArea and JTextField in one JFrame

    Quote Originally Posted by kkid View Post
    I've just been doing some research using the link you provided, I have learnt that I was adding the components to the JFrame itself instead of the JFrames layout (which by default is the border layout).
    Good

    Therefore, for my arrangement, I will want to assign the JTextArea to the top area of the layout and I will want to assign the JTextField to the bottom area.
    By having no centre areas (i.e setting their height to 0), the top and bottom will "touch" and give me my desired layout.
    Why no center area? Why setting height of anything?

    Am I correct with this?
    I am very new to swing, I have only dealt with command-line outputs up until now.
    Let's see.


    if this is correct, how to I add each of my components to the top and bottom areas?
    The link provided should show you to the BorderLayout tutorial, and this is well explained there.

    I saw that oracle named the areas PAGE_START and PAGE_END and therefore thought that I may have to do it by doing something like this:
    Java Code:
    PAGE_START.add(textOutput);
    PAGE_END.add(textInput);
    (textOutput is the JTextArea and textInput is the JTextField).

    As I am sure you know, that doesn't work.
    You are trying to add a GUI component to a String with a String method that doesn't exist which makes little sense. Again, please read the tutorial, as it's all well explained there, and you should have read this by now.

    As an aside, I would not ignore the central region and in fact usually would put my JTextArea inside of a JScrollPane and the JScrollPane into the BorderLayout-using container in the BorderLayout.CENTER position. Again please tell me why you're trying to avoid this position. I would also take care to not set the JTextArea's size or preferredSize since I know that this can mess things up, but instead would set its visible columns and rows.

  5. #5
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: Have a JTextArea and JTextField in one JFrame

    Thanks for your help, I came across GridLayout in the Oracle tutorials. This seemed to be perfect for what I needed. Just tested it and it works :)

    I'll definitely use your JScrollPane suggestion too, thanks.

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

    Default Re: Have a JTextArea and JTextField in one JFrame

    Great.

    GridLayout is great for creating equal sized grids of sub components, but don't try to use it to hold your JTextField and your JTextArea as that would look artificial, since the JTextField's size would have to expand to match that of the JTextArea.

  7. #7
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: Have a JTextArea and JTextField in one JFrame

    Quote Originally Posted by Fubarable View Post
    Great.

    GridLayout is great for creating equal sized grids of sub components, but don't try to use it to hold your JTextField and your JTextArea as that would look artificial, since the JTextField's size would have to expand to match that of the JTextArea.
    Yeah, I've just noticed this :/

    Is there no way of altering the ratio of sizes between rows?


    At the moment I have 1 column, 2 rows - can I set row 1 to 70% height and row 2 to 30% height?

  8. #8
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,864
    Rep Power
    8

    Default Re: Have a JTextArea and JTextField in one JFrame

    Quote Originally Posted by kkid View Post
    Yeah, I've just noticed this :/

    Is there no way of altering the ratio of sizes between rows?


    At the moment I have 1 column, 2 rows - can I set row 1 to 70% height and row 2 to 30% height?
    I think you were on the right track with BorderLayout. I'd recommend reading over the advice you already received.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. To maximize JTextArea on maximizing the Jframe ?
    By adiotrox in forum New To Java
    Replies: 1
    Last Post: 06-16-2012, 02:12 PM
  2. Replies: 0
    Last Post: 08-30-2011, 09:18 AM
  3. JFrame+JPanel+JButton+JtextArea+ImageIcon
    By zusss in forum AWT / Swing
    Replies: 11
    Last Post: 02-18-2011, 10:40 AM
  4. Replies: 9
    Last Post: 06-16-2010, 03:42 AM
  5. Replies: 8
    Last Post: 08-13-2009, 04:23 AM

Posting Permissions

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