0

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

    }

}
Werokk
  • 89
  • 2
  • 14

1 Answers1

2

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)

KyleKW
  • 300
  • 1
  • 11
  • Do you know how to get the component to respond from the user resizing the window? – Werokk Dec 14 '16 at 15:55
  • @Werokk can you clarify what you mean? I seemed to noticed the frame acting odd as well. I remove the two lines of `fr.setMinimumSize()` and `fr.setSize()` and added `fr.pack()` at the end of the code. I would highly suggest restructuring the code to separate the panel and frame configuration for easier troubleshooting. – KyleKW Dec 14 '16 at 15:59
  • @Werokk, `Thank you so much, it worked! ` - then don't forge to "accept" the answer by clicking on the checkmark so people know the problem has been solved. – camickr Dec 14 '16 at 16:06
  • I had to fr.setMinimumSize(); because in that dimension the components are aligned correctly, but if I make the window smaller I can see only some of the components because the components don't change dimension – Werokk Dec 14 '16 at 16:12
  • @Werokk You may want to take a look at [this](http://stackoverflow.com/questions/30198953/how-to-dynamically-control-auto-resize-components-in-java-swing) – KyleKW Dec 14 '16 at 16:16