user3760100
user3760100

Reputation: 685

How to set the size of the JTextArea and JTextField to my liking in the following code?

Ignore the functionality. I am only interested about the GUI.

I have used a JPanel component called mainPanel and added a JTextArea called receivingWindow and a JTextField called sendWindow to it.

The receiveWindow and sendWindow take up the entire frame space despite me specifying their sizes in the following lines:

receivingWindow = new JTextArea(50,50);
sendWindow = new JTextField(30);

How do I set their size to my liking?

Here is the full code:

package hf;

import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;

public class ChatClient 
{
    private JFrame frame;
    private JTextArea receivingWindow;
    private JTextField sendWindow;
    private JButton sendButton;
    private JPanel mainPanel;
    private Socket connectionSocket;
    private final static int serverPortNumber = 5000;
    private PrintWriter printWriter;
    private BufferedReader incomingReader;
    private InputStreamReader incomingInputStreamReader;
    private JScrollPane scrollPane;

    public static void main(String[] args)
    {
        ChatClient chatClient = new ChatClient();
        chatClient.startUp();
    }

    public void startUp()
    {
        setUpGui();
        setUpNetworking();
        handleEvents();
        setUpListenerThread();
        displayFrame();
    }

    private void setUpGui()
    {
        frame = new JFrame("Chat Client");
        receivingWindow = new JTextArea(50,50);
        sendWindow = new JTextField(30);
        sendButton = new JButton("Send");
        mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.PAGE_AXIS));
        scrollPane = new JScrollPane(receivingWindow);
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        mainPanel.add(scrollPane);
        mainPanel.add(sendWindow);
        mainPanel.add(sendButton);
        frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
    }

    private void handleEvents()
    {
        sendButton.addActionListener(new MySendButtonListener());
    }

    private void setUpNetworking()
    {
        try
        {
            connectionSocket = new Socket("127.0.0.1",serverPortNumber);
            printWriter = new PrintWriter(connectionSocket.getOutputStream());
            incomingInputStreamReader = new InputStreamReader(connectionSocket.getInputStream());
            incomingReader = new BufferedReader(incomingInputStreamReader);
        }
        catch(Exception ex)
        {
            System.out.println("Error in connecting to the server.");
        }
    }

    private class MySendButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            String message = sendWindow.getText();
            printWriter.println(message);
            printWriter.flush();
            sendWindow.setText("");
            sendWindow.requestFocus();
        }
    }

    private void setUpListenerThread()
    {
        try
        {
            Runnable incomingThread = new IncomingMessageRunnable();
            Thread thread = new Thread(incomingThread);
            thread.start();
        }
        catch(Exception ex)
        {
            System.out.println("Error in setting up thread for listening to incoming menssages.");
        }
    }

    private class IncomingMessageRunnable implements Runnable
    {
        public void run()
        {
            String incomingMessage = null;
            try
            {
                while((incomingMessage = incomingReader.readLine())!=null)
                {
                    receivingWindow.append(incomingMessage+"\n");
                }
            }
            catch(Exception e)
            {
                System.out.println("Error in receiving incoming messages");
            }
        }
    }

    private void displayFrame()
    {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }
}

Upvotes: 1

Views: 619

Answers (1)

Marv
Marv

Reputation: 3557

What you specified in your code aren't really the sizes on the panel but rather how many columns and rows or columns your JTextField and your JTextArea have respectively. From the Oracle Docs:

JTextArea(int rows, int columns)
    Constructs a new empty TextArea with the specified number of rows and columns.

JTextField(int columns)
    Constructs a new empty TextField with the specified number of columns.

Your components are taking up the whole screen space because your BoxLayout fits every component added to the panel into the panel and aligns them vertically while respecting their maximum sizes and since you haven't set any they appear to be filling up the whole space.

I would refrain from trying to archive pixel-perfect GUI anyways and rather find a better LayoutManager to archive what you have in mind. I would suggest you look at A Visual Guide to Layout Managers and see which layout best suits your needs.

Upvotes: 2

Related Questions