-1

I'm trying to create a window with a scrollable JTextArea and a JTextField below it. I want the frame to look like a chat window; one, large scrollable text area and a single lined text frame. I've tried variations but I can't get the text area scrollable without making the entire window scrollable. It's incredibly annoying. My current iteration only draws one panel to the screen:

private void buildGUI() {

    Container chatClientContainer = getContentPane();
    chatClientContainer.setLayout(new BorderLayout());

    JPanel messagesReceivedPanel = new JPanel();
    messagesReceivedPanel.setLayout(new GridLayout(1, 1, 5, 5));
    JTextArea messagesReceived = new JTextArea("area");
    messagesReceivedPanel.add(messagesReceived);

    JPanel draftPanel = new JPanel();
    draftPanel.setLayout(new GridLayout(1, 1, 5, 5));
    JTextField draftMessage = new JTextField("field");
    draftPanel.add(draftMessage);

    chatClientContainer.add(new JScrollPane(messagesReceivedPanel));
    chatClientContainer.add(draftPanel);

    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    int windowWidth = 400;
    int windowHeight = 600;
    int posX = ((int) screenSize.getWidth())/2 - windowWidth/2;
    int posY = (int) screenSize.getHeight()/2 - windowHeight/2;

    setBounds(posX, posY, windowWidth, windowHeight);
    setResizable(true);
    setVisible(true);
}

How can I position this the way I want?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Nanor
  • 2,400
  • 6
  • 35
  • 66
  • 1
    Best replace `Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); int windowWidth = 400; int windowHeight = 600; int posX = ((int) screenSize.getWidth())/2 - windowWidth/2; int posY = (int) screenSize.getHeight()/2 - windowHeight/2; setBounds(posX, posY, windowWidth, windowHeight);` with **`pack();`** followed by `setLocationRelativeTo(null);` or (better) **`setLocationBylatform(true);`** – Andrew Thompson Apr 09 '15 at 15:09

1 Answers1

2

Why not just use BorderLayout? Place the JTextArea's JScrollPane BorderLayout.CENTER and the JTextField (not JTextFrame) BorderLayout.PAGE_END.

For example:

import java.awt.BorderLayout;

import javax.swing.*;

public class ChatPanel extends JPanel {
   private static final long serialVersionUID = 1L;
   private static final int ROWS = 15;
   private static final int COLS = 30;
   private JTextArea textArea = new JTextArea(ROWS, COLS);
   private JTextField textField = new JTextField(COLS);

   public ChatPanel() {
      JScrollPane scrollPane = new JScrollPane(textArea);
      scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

      setLayout(new BorderLayout());
      add(scrollPane, BorderLayout.CENTER);
      add(textField, BorderLayout.PAGE_END);
   }

   private static void createAndShowGUI() {
      ChatPanel paintEg = new ChatPanel();

      JFrame frame = new JFrame("ChatPanel");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(paintEg);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGUI();
         }
      });
   }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • How can I set the size of the JTextArea manually? setSize() doesn't work. – Nanor Apr 09 '15 at 14:45
  • Ah, discovered `setPrefferedSize()` – Nanor Apr 09 '15 at 14:54
  • 3
    1+, `discovered setPrefferedSize()` - No, don't use setPreferredSize(), this is just a random guess. Use `new JTextArea(5, 30)`. Now the UI will calculate the size so that exactly 5 lines are displayed on all LAF. – camickr Apr 09 '15 at 15:07
  • See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) The size of a `JTextArea` is best suggested in the constructor (columns and rows). The size of a `JTextField` is best suggested in the constructor (columns). – Andrew Thompson Apr 09 '15 at 15:07
  • @Nanor: never set the preferred size of a JTextArea, not if you want it to scroll properly. Set its row and column properties. – Hovercraft Full Of Eels Apr 09 '15 at 15:12
  • @Nanor: please see edit to answer for an example of what I mean. – Hovercraft Full Of Eels Apr 09 '15 at 15:48