TextArea is overflowing bounds assigned to it as well as the center of the Borderlayout and covering the entire window

StackOverflow https://stackoverflow.com//questions/21031287

Question

So I asked something similar to this earlier and got the answer to the question I asked, but I apparently didn't ask the right question. So, what I'm trying to accomplish here is have the text area populate only in the center of the jFrame. As you can see, it is set up as a borderlayout, and there are buttons on the bottom, a label on the top (which will then - on the full version of this program - be copied into the text frame when it is replaced with text from the action listener on the button)

The problem is, the text area fills the entire window and covers up every other component on the window. I've tried to use preferred size, and I tried to specify columns/rows and I've read the tutorials on docs.oracle, although I suppose since I'm still having trouble i may have missed one.

Also, the offset commented lines I found in the docs.oracle information and it would be a good idea for this to text-wrap because it's going to be a log of what has occurred. I tried adding all the imports suggested on that website but netbeans still gives me a red underline that they're not recognized as commands. Have they been deprecated, did I not use them right, or am I missing an import?

I know I'm asking a lot, but thanks for your time and patience!

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package theproblem;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.TextArea;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/**
 *
 * @author Heather
 */
public class TheProblem {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {

        JFrame window2 = new JFrame();
        TextArea battleLogging = new TextArea(3,10);
        JScrollPane logScrollPane = new JScrollPane(battleLogging);
        JLabel BattleLog = new JLabel();
        JLabel p1HPLabel= new JLabel();
        JLabel p2HPLabel= new JLabel();
        String attack1ButtonContents = "Just an attack";
        String attack2ButtonContents = "Just another attack";
        JButton attack1=new JButton(attack1ButtonContents);
        JButton attack2=new JButton(attack2ButtonContents);


        window2.setLayout(new BorderLayout());
        window2.setSize(400,400);
        JPanel attackPanel = new JPanel();
        attackPanel.add(attack1);
        attackPanel.add(attack2);

        window2.add(battleLogging, BorderLayout.CENTER);
        battleLogging.setEditable(false);
        logScrollPane.setVerticalScrollBarPolicy(
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        logScrollPane.setPreferredSize(new Dimension(50, 50));


        //battleLogging.setLineWrap(true);
        //battleLogging.setWrapStyleWord(true);


        window2.add(BattleLog, BorderLayout.NORTH);
        window2.add(p1HPLabel, BorderLayout.WEST);
        window2.add(p2HPLabel, BorderLayout.EAST);
        window2.setVisible(true);
        window2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


}
Was it helpful?

Solution

Problems:

  • When adding any component BorderLayout.CENTER, it fills the center position, no matter what size or preferred size you give it.
  • You shouldn't even be setting sizes.
  • Don't use TextAreas with Swing apps. Use JTextAreas
  • Set the JTextArea's column and row count and let that do its sizing for you.
  • Don't forget to pack your GUI before displaying it.

For example:

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.*;

public class TheProblem2 {
   private static void createAndShowGUI() {
      int rows = 5;
      int cols = 20;
      JTextArea textArea = new JTextArea(rows, cols);
      JScrollPane scrollPane = new JScrollPane(textArea);
      scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

      JButton button1 = new JButton("Button 1");
      JButton button2 = new JButton("Button 1");
      JPanel btnPanel = new JPanel(new GridLayout(1, 0, 5, 0));
      btnPanel.add(button1);
      btnPanel.add(button2);

      JPanel mainPanel = new JPanel(new BorderLayout());
      mainPanel.add(scrollPane);
      mainPanel.add(btnPanel, BorderLayout.SOUTH);

      JFrame frame = new JFrame("EG 2");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGUI();
         }
      });
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top