How to set JTextArea's size

edited August 2016 in Questions about Code

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

  • Answer ✓

    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-

  • edited August 2016 Answer ✓

    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.

    import javax.swing.*;
    import java.awt.*;
    
    JFrame frame;
    JPanel panel;
    JTextArea textArea;
    JScrollPane scrollPane;
    
    void setup() {
    
      //Hide the PApplet
      surface.setVisible(false);
    
      //Create new JFrame 
      frame = new JFrame("Swing Demo");
      frame.setBounds(50, 50, 250, 100);
    
      panel = new JPanel();
      panel.setLayout(new FlowLayout(FlowLayout.LEADING));
    
      textArea = new JTextArea();
      textArea.setLineWrap(true);
      textArea.setPreferredSize(new Dimension(400, 400));
      textArea.setMinimumSize(new Dimension(400, 400));
      textArea.setBounds(0, 0, 400, 400);
    
      scrollPane = new JScrollPane(textArea);
      scrollPane.setViewportView(textArea);
      scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    
      //  panel.setLayout(null);
      panel.add(scrollPane, BorderLayout.PAGE_START);
      frame.add(panel);
      frame.pack(); 
      frame.setVisible(true);
    }
    
    void draw() {
    } 
    

    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.

  • edited August 2016

    @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:

    TextArea1
        |
    TextArea2
        |
    TextArea3
    

    Regards, Daantje

  • edited August 2016

    There are different layout managers that you can use. The one you could use to get a vertical arrangement would be the GridLayout.

  • edited August 2016

    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!

  • edited August 2016

    Untitled

    I couldn't upload the screenshot in the previous post, so here it is :D

  • edited August 2016

    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.

  • edited August 2016

    I have this code:

    import javax.swing.*;
    import java.awt.*;
    
    JFrame frame;
    JPanel panel;
    JTextArea textArea, textArea2, textArea3;
    JScrollPane scrollPane, scrollPane2, scrollPane3;
    JButton button;
    
    void setup() {
    
      //Hide the PApplet
      surface.setVisible(false);
    
      //Create new JFrame 
      frame = new JFrame("Swing Demo");
      frame.setBounds(50, 50, 250, 100);
    
      panel = new JPanel();
      panel.setLayout(new GridBagLayout());
      GridBagConstraints c = new GridBagConstraints();
    
      //TextArea1
      textArea = new JTextArea();
      textArea.setLineWrap(true);
      textArea.setPreferredSize(new Dimension(100, 100));
      textArea.setMinimumSize(new Dimension(100, 100));
      textArea.setBounds(0, 0, 100, 100);
    
      scrollPane = new JScrollPane(textArea);
      scrollPane.setViewportView(textArea);
      scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    
      c.fill = GridBagConstraints.HORIZONTAL;
      c.gridx = 0;
      c.gridy = 0;
      c.weightx = 0.5;
      panel.add(scrollPane, c);
    
      //TextArea2
      textArea2 = new JTextArea();
      textArea2.setLineWrap(true);
      textArea2.setPreferredSize(new Dimension(100, 100));
      textArea2.setMinimumSize(new Dimension(100, 100));
      textArea2.setBounds(0, 0, 100, 100);
    
      scrollPane2 = new JScrollPane(textArea2);
      scrollPane2.setViewportView(textArea2);
      scrollPane2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    
      c.fill = GridBagConstraints.HORIZONTAL;
      c.weightx = 0.5;
      c.gridx = 1;
      c.gridy = 0;
      panel.add(scrollPane2, c);
    
      //TextArea3
      textArea3 = new JTextArea();
      textArea3.setLineWrap(true);
      textArea3.setPreferredSize(new Dimension(100, 100));
      textArea3.setMinimumSize(new Dimension(100, 100));
      textArea3.setBounds(0, 0, 100, 100);
    
      scrollPane3 = new JScrollPane(textArea3);
      scrollPane3.setViewportView(textArea3);
      scrollPane3.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 
    
      c.fill = GridBagConstraints.HORIZONTAL;
      c.weightx = 0.5;
      c.gridx = 2;
      c.gridy = 0;
      panel.add(scrollPane3, c);
    
      //Button
      button = new JButton("Button");
      c.fill = GridBagConstraints.HORIZONTAL;
      c.ipady = 40;      //make this component tall
      c.weightx = 0.0;
      c.gridwidth = 3;
      c.gridx = 0;
      c.gridy = 1;
      panel.add(button, c);
    
      //  panel.setLayout(null);
      frame.add(panel);
      frame.pack(); 
      frame.setVisible(true);
    
    
    }
    
    void draw() {
    
    } 
    

    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

  • edited August 2016 Answer ✓

    If I put text into one of the JTextAreas, the scrollbar doesn't work

    I hadn't noticed that before. The solution is to use a different JTextArea constructor, so change line 30 to

    textArea = new JTextArea(5, 10);
    

    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.

    If I maximize the screen, there are very much blank spaces. Is there a way to fill them more?

    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

Sign In or Register to comment.