Results 1 to 8 of 8
- 11-29-2012, 06:41 PM #1
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
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
- 11-29-2012, 06:44 PM #2
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!
- 11-29-2012, 07:14 PM #3
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
Re: Have a JTextArea and JTextField in one JFrame
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:
(textOutput is the JTextArea and textInput is the JTextField).Java Code:PAGE_START.add(textOutput); PAGE_END.add(textInput);
As I am sure you know, that doesn't work.
-
Re: Have a JTextArea and JTextField in one JFrame
Good
Why no center area? Why setting height of anything?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.
Let's see.Am I correct with this?
I am very new to swing, I have only dealt with command-line outputs up until now.
The link provided should show you to the BorderLayout tutorial, and this is well explained there.if this is correct, how to I add each of my components to the top and bottom areas?
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.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:
(textOutput is the JTextArea and textInput is the JTextField).Java Code:PAGE_START.add(textOutput); PAGE_END.add(textInput);
As I am sure you know, that doesn't work.
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.
- 12-01-2012, 03:01 PM #5
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
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.
-
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.
- 12-01-2012, 07:38 PM #7
Senior Member
- Join Date
- Jul 2012
- Posts
- 165
- Rep Power
- 1
- 12-03-2012, 02:30 PM #8
Re: Have a JTextArea and JTextField in one JFrame
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
-
To maximize JTextArea on maximizing the Jframe ?
By adiotrox in forum New To JavaReplies: 1Last Post: 06-16-2012, 02:12 PM -
Problem Loading JTextField in JFrame / Problema al cargar JTextField en JFrame
By thor_inc in forum AWT / SwingReplies: 0Last Post: 08-30-2011, 09:18 AM -
JFrame+JPanel+JButton+JtextArea+ImageIcon
By zusss in forum AWT / SwingReplies: 11Last Post: 02-18-2011, 10:40 AM -
JTextField & JTextArea won't receive focus in a modal JDialog window
By javaexplorer in forum AWT / SwingReplies: 9Last Post: 06-16-2010, 03:42 AM -
Displaying text in a JTextArea when a JtextField is Selected
By Frys82 in forum AWT / SwingReplies: 8Last Post: 08-13-2009, 04:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks