2

I want a JTextArea in a certain position. I tried several things like using different LayoutManagers, no LayoutManager at all, setLayout(null) etc. Whatever I do, it seems like setBounds(), setLocation() and setSize() aren't working here, but I read about it and it said it should work. So what am I doing wrong?

The JTextArea is always too high and the position doesn't change if I change the Parameters in setBounds().

public class textarea extends JPanel {

    public static void main(String[] args){
        JFrame frame = new JFrame("text area");
        textarea content = new textarea();
        frame.setContentPane(content);
        frame.setLocation(120,70);
        frame.pack();
        frame.setVisible(true);
        frame.setSize(700,500);     
    }

    JPanel PanelForText;

    public textarea(){
        setBackground(Color.LIGHT_GRAY);
        setLayout(new FlowLayout(FlowLayout.CENTER,50,50));

        txtArea txt = new txtArea();

        PanelForText = new JPanel();
        PanelForText.setPreferredSize(new Dimension(500,300));
        PanelForText.setBorder(BorderFactory.createEtchedBorder());
        PanelForText.add(txt);

        add(PanelForText);
    }
}


public class txtArea extends JPanel {

    boolean textAreaCreated = false;

    public txtArea(){
        setBackground(Color.WHITE);
        setPreferredSize(new Dimension(496, 290));
    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(Color.GRAY);
        g.fillRect(50, 25, 400, 245);
        if (!textAreaCreated)
            createTextArea();
    }

    public void createTextArea() {  
        JTextArea Text = new JTextArea();
        Text.setBounds(500,300,300,300);
        Text.setOpaque(false);
        Text.setWrapStyleWord(true);
        Text.setLineWrap(true); 
        Text.setBorder(BorderFactory.createLineBorder(Color.RED));
        add(Text);
        textAreaCreated = true;
    }
}

Here is what I want it to look like: enter image description here

And here is what it currently looks like: enter image description here

I did a few tutorials where they used JTextFields that were added to JPanels, but I was wondering if I could just use a JTextField or JTextArea for more text WITHOUT adding it to a Panel first!

Like I said, I was looking up "how to set JTextArea position" and it said to use setBounds(). Apparently that's not correct.. So again, all I want to know is how to do it better. Also: I did read a lot about the LayoutManagers, but for me trying to use it is more helpful than just reading about it...

I tried that with rows and columns, but it didn't change the fact that the JTextArea was not in the right position.

What I did was (in the CreateTextArea method):

public void createTextArea() {  
    JTextArea Text = new JTextArea(5,1);
    Text.setOpaque(false);
    Text.setWrapStyleWord(true);
    Text.setLineWrap(true); 
    Text.setBorder(BorderFactory.createLineBorder(Color.RED));
    add(Text);
    textAreaCreated = true;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
mcCat
  • 120
  • 2
  • 13
  • 6
    ***Never*** use `setBounds(...)` on a JTextArea ever. Doing this will guarantee that it won't work inside of a JScrollPane which is where it is most commonly placed. Your question is missing key information, including your layout manager attempts, an image of your current GUI results and an image of your desired results. Create your images, upload them to an image sharing site, and then give us the links to the images, and we'll insert them into your question for you. – Hovercraft Full Of Eels Oct 14 '14 at 12:02
  • 1
    Instead you will want to set the visible columns and rows of your JTextArea, and it has a constructor that allows you to do this. You also will want to study the layout manager tutorials in greater depth since this is how you will solve your current coding problem. – Hovercraft Full Of Eels Oct 14 '14 at 12:04
  • 1
    I'm still kind of unsure what you are trying to do. If I run your probram I get a grey rectangle, and above that a textarea with a red border. What exactly do you want to do? How do you want it to look like? – Oliver Watkins Oct 14 '14 at 12:07
  • If you are using Eclipse/Spring for your development, why can't you use WindowBuilder plugin? it makes life a lot easier. – ha9u63a7 Oct 14 '14 at 12:18
  • @hagubear: and also teaches you that you don't have to understand how layout managers work -- not good. Better to avoid using GUI-builders before you understand the GUI library. – Hovercraft Full Of Eels Oct 14 '14 at 12:22
  • so when i added rows and coloums to new JTextArea(); (see code above) it looked like this: http://oi58.tinypic.com/21jx34h.jpg – mcCat Oct 14 '14 at 12:36
  • Oliver Watkins: thankx for ur answer, i edited my post now and added a picture of what i want :) – mcCat Oct 14 '14 at 12:37
  • @hagubear: thx for responding. well i did my tutorials and they were about jFrame, JPanel and all that stuff where they used these TextFields and TextAreas so i was trying to use them on my own, but i will read about WindowBuilder. Still i was just curious why i cant change the position of my JTextArea :/.... EDIT: ok well i dont wanna use WindowBuilder for now, cus just like Hovercraft said, im trying to learn it/understand it and i dont think i can when i use WindowBuilder. – mcCat Oct 14 '14 at 12:38
  • Alright, now i tried to add JTextArea to a JPanel and i used FlowLayout like this. JPanel txtPanel = new JPanel() ... txtPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5); and then added JTextArea to the Panel, but the position still wont change. It doesnt even matter if i use FlowLayout.CENTER or LEFT. it seems like the FlowLayout isnt even there. Theres probably an easy answer for this, but im new to java and really need some help here :) – mcCat Oct 14 '14 at 12:55

1 Answers1

2

You could achieve your center-nested look by doing just that, using GridBagLayout to center nest your components. You could achieve the width of some of the framing JPanels by using an EmptyBorder. You should never add components from within a paintComponent method.

For example:

import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;

@SuppressWarnings("serial")
public class TestTextArea2 extends JPanel {

   private static final Color BG = Color.LIGHT_GRAY;
   private static final int ROWS = 14;
   private static final int COLS = 34;
   private JTextArea textArea = new JTextArea(ROWS, COLS);

   public TestTextArea2(int heightGap, int sideGap) {
      setBorder(BorderFactory.createEmptyBorder(heightGap, sideGap, heightGap, sideGap));

      textArea.setBackground(Color.LIGHT_GRAY);
      JScrollPane scrollPane = new JScrollPane(textArea);

      JPanel txtAreaPanel = new JPanel(); 
      int ebGap = 40;
      txtAreaPanel.setBackground(Color.white);
      txtAreaPanel.setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap));
      txtAreaPanel.setLayout(new GridBagLayout());
      txtAreaPanel.add(scrollPane);

      JPanel myPanel2 = new JPanel();
      Border outerBorder = BorderFactory.createEtchedBorder();
      int heightGap2 = 5;
      int sideGap2 = 5;
      Border innerBorder = BorderFactory.createEmptyBorder(heightGap2, sideGap2, heightGap2, sideGap2);
      myPanel2.setBorder(BorderFactory.createCompoundBorder(outerBorder, innerBorder));

      myPanel2.setLayout(new GridBagLayout());
      myPanel2.add(txtAreaPanel);

      setBackground(BG);
      setLayout(new GridBagLayout());
      add(myPanel2);
   }


   private static void createAndShowGui() {
      JFrame frame = new JFrame("TestTextArea2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(new TestTextArea2(100, 100));
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

And note, not one setSize(...) or setPreferredSize(...).

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Thanks a lot! I'm gonna try that. Thanks for all your tips. In my tutorial, it said to ALWAYS do all the drawing in the paintComponent method. That's why i did that. So thanks for clarifying that. – mcCat Oct 15 '14 at 08:17
  • @mcCat: yes, do your drawing inside of `paintComponent`, but just drawing. Don't add or remove components or perform program logic from within that method. – Hovercraft Full Of Eels Oct 15 '14 at 12:53