We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
I am searching on the web for a long time to get a JTextArea in my Processing sketch... Because it isn't possible to get Java Swing or Java AWT into a PApplet, I tried to create a code which creates a JFrame with a JTextArea in it.
Here's my code:
import javax.swing.*;
import java.awt.*;
JFrame frame = new JFrame();
JTextArea textArea = new JTextArea();
JScrollPane scrollPane = new JScrollPane();
void setup() {
//Hide the PApplet
surface.setVisible(false);
//Create new JFrame
frame = new JFrame("Swing Demo");
frame.setBounds(50, 50, 50, 50);
textArea = new JTextArea();
textArea.setLineWrap(true);
scrollPane = new JScrollPane(textArea);
scrollPane.setViewportView(textArea);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
frame.add(scrollPane);
frame.pack();
frame.setVisible(true);
}
void draw() {
}
When I run this code, Processing hides his own PApplet and creates a JFrame with a JTextArea in it. My question is: How can I set the size of the JTextArea to a fixed value? If I'm running this code and maximize the JFrame, the JTextArea will become bigger (it fills the whole JFrame). What I want is to say that the JTextArea has to be 400px by 50px always, unregarded the size of the JFrame...
Thanks in advance! Kind regards, Daantje
Answers
Dunno much about AWT & SWING but maybe you can call setPreferredSize() on your Component:
http://docs.Oracle.com/javase/8/docs/api/java/awt/Component.html#setPreferredSize-java.awt.Dimension-
Or even consider prohibiting your Frame to be resizable at all w/ setResizable():
http://docs.Oracle.com/javase/8/docs/api/java/awt/Frame.html#setResizable-boolean-
The JTextArea will be resized to fill the JFrame even if you set the preferred and maximum size because you can't have 'empty space' in the JFrame when it bigger than the text area. The trick is to use a JPanel as in intermediary between the textarea and the frame like this.
The JPanel will now resize to fit the frame rather than the textarea.
BTW you were creating 2 frames, 2 textareas and 2 scrollpanes which I have also corrected.
@quark, thanks for your code!
I have one more question for you... In the code you've posted, you are using a 'FlowLayout'. If I add a second textArea to the JPanel, they are put next to each other.
Is there a way (another Layout or something) to put all textAreas above each other? So:
Regards, Daantje
There are different layout managers that you can use. The one you could use to get a vertical arrangement would be the GridLayout.
Thank you...
Actually I want to use something like a GridBagLayout so I can set the position of the components on the screen manually. But I don't know how to use that Layout in the code. And how can I set the row and column the textArea has to be 'spawned'?
I know how I can use a GridLayout, but with that layout I can't set the X-position and Y-position and if I maximize the JFrame, the JTextArea fills the JFrame's width (see screenshot)
Regards, Daantje!
I couldn't upload the screenshot in the previous post, so here it is :D
panel.setLayout(new GridLayout(0,1));
that is 1 column with as many rows as needed.
Yes, I found something on the internet (http://stackoverflow.com/questions/6906331/how-do-you-set-the-position-of-button-in-grid-layout) where they said I have to use a GridBagLayout instead of a GridLayout (so I can set the X and Y Position). I have changed my previous 2 posts to the right question ;-)
Regards, Daantje
Have you tried the GridBagLayout and if you have can you post your code so I can see where you are up to.
I have this code:
Problems I have:
If I put text into one of the JTextAreas, the scrollbar doesn't work
If I maximize the screen, there are very much blank spaces. Is there a way to fill them more?
Regards, Daantje
I hadn't noticed that before. The solution is to use a different JTextArea constructor, so change line 30 to
The two arguments to the JTextArea constructor are hints as to the number of rows and columns, respectively, that the text area should display. The scroll pane that contains the text area pays attention to these hints when determining how big the scroll pane should be.
Then do the same for the other 2 textareas.
Either the components expand to fill the frame or the frame will have a lot of 'blank' space, the choice is yours.
In a real application you would design the GUI using paper and pencil and then decide on how the components should be displayed when the frame size is changed. From that decide on the layout manager to be used and then create the code. Seeing what components 'fit' a frame does not make sense to me.
Quark, thank you so much for your help and your time! It works, so I'm going to look how to get everything working for me. I think I have to learn more about Java :D
Regards, Daantje