javaswingjtextfieldjtextareasetbounds

JTextArea position, setBounds does not work?


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;
}

Solution

  • 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(...).