Werokk
Werokk

Reputation: 89

JScrollPane and responsive GUI with gridbaglayout

Hi everyone I trying to create a chat session GUI. I managed to position all the components in the correct order. The only problem is that the Frame is not responding, whenever I try to resize the window the component stays with the same dimension, also when I type in text in the JtextArea, they border enlarges taking over any other component in the frame. I have tried using JScrollPane or setting the maximum dimension but it doesn't work. Can anyone help me. This is my code.

import java.awt.*;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.text.DefaultCaret;

public class ClientGUI extends JPanel {

    public ClientGUI() {
        Dimension size = getPreferredSize();
        size.width = 500;
        setPreferredSize(size);
        setBorder(BorderFactory.createTitledBorder("Peron"));

        GridBagConstraints gbc = new GridBagConstraints();
        JTextArea chat, list;
        JTextField wm;
        JButton sm, sf, pm, lo;
        JFrame fr = new JFrame("FRAME");
        fr.setVisible(true);
        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fr.setSize(200, 200);
        fr.setMinimumSize(new Dimension(1400, 1000));
        JPanel panel = new JPanel(new GridBagLayout());
        fr.add(panel);

        gbc.insets = new Insets(40, 40, 40, 40);
        chat = new JTextArea("Welcome to the chat room");
        // chat.setEditable(false);
        JScrollPane scroll = new JScrollPane(chat); // place the JTextArea in a
                                                    // scroll pane
        panel.add(scroll);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = 5;
        gbc.gridheight = 7;
        // gbc.gridwidth = java.awt.GridBagConstraints.RELATIVE;
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.ipady = 400;
        gbc.ipadx = 200;
        panel.add(chat, gbc);

        wm = new JTextField("Insert message", 10);
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridwidth = 1;
        gbc.gridheight = 2;
        gbc.ipady = 150;
        gbc.ipadx = 300;
        gbc.gridx = 0;
        gbc.gridy = 10;
        panel.add(wm, gbc);

        list = new JTextArea("User online");

        gbc.gridx = 5;
        gbc.gridy = 2;
        gbc.ipady = 400;
        gbc.ipadx = 300;
        panel.add(list, gbc);

        sm = new JButton("Send");
        gbc.gridheight = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 3;
        gbc.gridy = 10;
        gbc.ipady = 20;
        gbc.ipadx = 200;
        panel.add(sm, gbc);

        pm = new JButton("Private message");
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 4;
        gbc.gridy = 10;
        gbc.ipady = 20;
        gbc.ipadx = 20;
        panel.add(pm, gbc);

        lo = new JButton("LOGOUT");
        gbc.gridx = 5;
        gbc.gridy = 1;
        gbc.ipady = 20;
        panel.add(lo, gbc);

        sf = new JButton("Send File");
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.gridx = 5;
        gbc.gridy = 10;
        gbc.ipady = 20;
        gbc.ipadx = 20;
        panel.add(sf, gbc);

    }

}

Upvotes: 0

Views: 612

Answers (1)

KyleKW
KyleKW

Reputation: 300

You problem with the text area is where you put panel.add(scroll). Delete this line. Also, you should be adding the scroll pane and not the text area to the panel. Change panel.add(chat,gbc) to panel.add(scroll,gbc)

Upvotes: 2

Related Questions