Question

These images show a chat box that needs to be resized with the application frame. I'm using GroupLayout to do so. The hierarchy of the frame goes something like this

chatDiv(JPanel, GroupLayout)->chatTabbedPane(JTabbedPane, GroupLayout)
->chatPanel(extended JPanel, GroupLayout)

But, these individual chat_tabs(chatPanel) are added to the chatTabbedPane dynamically during application runtime.

Problem is: These chatPanel(s) are expanding with the expansion of application 
frame but they are not shrinking with the shrink in width of application frame.

initialized frame

and when I expand and again shrink the application frame this happens (size of chatPanel doesn't shrink leading to invisible overflown region)..

expanded and again shrinked frame

Here is the class chatPanel that extends JPanel:

class chatPanel extends JPanel{
private static final long serialVersionUID = 1L;
GroupLayout gl_panel;
JTextArea textArea;
JTextField textField;
JButton sendTextButton;
JButton closeChatButton;
int replyRef;

public chatPanel(int ref){
    //this.setLayout(null);

    closeChatButton = new JButton("");
    sendTextButton = new JButton("SEND");
    textField = new JTextField();
    textArea = new JTextArea();
    replyRef = ref;

    textArea.setWrapStyleWord(true);
    textArea.setEditable(false);
    textArea.setLineWrap(true);
    //textArea.setBounds(0, 0, 370, 99);
    this.add(textArea);

    //textField.setBounds(0, 101, 290, 20);
    //textField.setColumns(10);
    this.add(textField);

    sendTextButton.setFont(new Font("Tahoma", Font.PLAIN, 10));
    //sendTextButton.setBounds(290, 101, 60, 19);
    this.add(sendTextButton);

    closeChatButton.setIcon(new ImageIcon("files/close.png"));
    closeChatButton.setSelectedIcon(null);
    closeChatButton.setToolTipText("CLOSE");
    closeChatButton.setFont(new Font("Tahoma", Font.PLAIN, 5));
    //closeChatButton.setBounds(350, 101, 20, 19);
    this.add(closeChatButton);

    gl_panel = new GroupLayout(this);
    gl_panel.setHorizontalGroup(
        gl_panel.createParallelGroup(Alignment.TRAILING)
            .addGroup(gl_panel.createSequentialGroup()
                .addGap(1)
                .addComponent(textField, GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE)
                .addGap(1)
                .addComponent(sendTextButton, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
                .addGap(1)
                .addComponent(closeChatButton, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE)
                .addGap(1))
            .addComponent(textArea, GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
    );
    gl_panel.setVerticalGroup(
        gl_panel.createParallelGroup(Alignment.LEADING)
            .addGroup(gl_panel.createSequentialGroup()
                .addComponent(textArea, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
                .addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
                    .addGroup(gl_panel.createSequentialGroup()
                        .addGap(1)
                        .addGroup(gl_panel.createParallelGroup(Alignment.TRAILING, false)
                            .addGroup(gl_panel.createSequentialGroup()
                                .addComponent(sendTextButton, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(ComponentPlacement.RELATED))
                            .addGroup(gl_panel.createSequentialGroup()
                                .addComponent(closeChatButton, GroupLayout.PREFERRED_SIZE, 19, GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(ComponentPlacement.RELATED)))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(gl_panel.createSequentialGroup()
                        .addGap(1)
                        .addComponent(textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                        .addGap(1))))
    );
    this.setLayout(gl_panel);
}
}

and this is how I add these chatPanel(s) to chatTabbedPane

chatPanel newChat = new chatPanel(chatRef);
chatTabbedPane.addTab("title", null, newChat, null);

I want to shrink the size of chatPanel with shrink in size of application frame.

No correct solution

OTHER TIPS

I've just run into this issue as well, and an answer like "just use another layout manager" isn't an answer I'm willing to accept.

To answer your question, the line wrap is messing things up.

textArea.setLineWrap(true);

Comment that line out and test again to see it will resize down with the window/frame like expected.

However, the side effect is obviously no line wraps. What's not so obvious is when your text length becomes greater than the textarea's width, when you resize the window the textarea's width will lock to the text's length. I still haven't found a fix for this new problem yet.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top