Question

I have the code below.

A similar code works. Also a friend said it should work. But it doesn't.

Anyone have a clue?

Thanks!

Problem Description:

Instead of painting the desired layout, everything is tiny small... .(

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ChatGUI extends JPanel {

    private static final long serialVersionUID = -3069354983197916315L;
    private Image imgBackground;

    private GridBagLayout gblChat;
    private GridBagConstraints constraintsChat;

    private JPanel jPanelChat;
    private JList clientList;

    private JColorChooser font_chooser;

    private JButton privateMessage_button;
    private JButton sendMessage_button;

    private JScrollPane OutputOfChatMessages;
    private JScrollPane ShowConnectedClients;
    private JScrollPane InputOfChatMessage;

    public static void main(String[] args) {

        JFrame jf = new JFrame();

        ChatGUI cg1 = new ChatGUI(500, 500);

        jf.add(cg1);

        jf.setExtendedState(Frame.MAXIMIZED_BOTH);
        jf.setVisible(true);
    }

    /**
     * Layout
     */
    public ChatGUI(int width, int height) {

        // used for scaling
        this.setMinimumSize(new Dimension(width / 2,
                height / 4));
        this.setPreferredSize(new Dimension(width / 2,
                height / 4));

        jPanelChat = new JPanel();
        gblChat = new GridBagLayout();
        constraintsChat = new GridBagConstraints();

        this.setLayout(gblChat);
        this.setBackground(Color.LIGHT_GRAY);
        constraintsChat.fill = GridBagConstraints.BOTH;

        /**
         * Panel 1 = OutputOfChatMessages
         */
        OutputOfChatMessages = new JScrollPane();

        constraintsChat.gridx = 0;
        constraintsChat.gridy = 0;
        constraintsChat.gridwidth = 1;
        constraintsChat.gridheight = 1;
        gblChat.setConstraints(OutputOfChatMessages, constraintsChat);

        this.add(OutputOfChatMessages);

        /**
         * Panel 2 = ShowConnectedClients
         */
        ShowConnectedClients = new JScrollPane();

        constraintsChat.gridx = 1;
        constraintsChat.gridy = 0;
        constraintsChat.gridwidth = 4;
        constraintsChat.gridheight = 4;
        gblChat.setConstraints(ShowConnectedClients, constraintsChat);

        this.add(ShowConnectedClients);

        /**
         * Panel 3 = InputOfChatMessage
         */
        InputOfChatMessage = new JScrollPane();

        constraintsChat.gridx = 0;
        constraintsChat.gridy = 5;
        constraintsChat.gridwidth = 2;
        constraintsChat.gridheight = 2;
        gblChat.setConstraints(InputOfChatMessage, constraintsChat);

        this.add(ShowConnectedClients);

        /**
         * Panel 4 = privateMessage_button
         */
        privateMessage_button = new JButton();

        constraintsChat.gridx = 1;
        constraintsChat.gridy = 6;
        constraintsChat.gridwidth = 1;
        constraintsChat.gridheight = 1;
        gblChat.setConstraints(privateMessage_button, constraintsChat);

        this.add(privateMessage_button);

        /**
         * Panel 5 = sendMessage_button
         */
        sendMessage_button = new JButton();

        constraintsChat.gridx = 1;
        constraintsChat.gridy = 7;
        constraintsChat.gridwidth = 1;
        constraintsChat.gridheight = 1;
        gblChat.setConstraints(sendMessage_button, constraintsChat);

        this.add(sendMessage_button);

        this.validate();
//this.revalidate();

    }
}
Was it helpful?

Solution

  1. You set no views for your scroll panes, therefore they have no means by which to determine their preferred size.
  2. You set no text on your buttons, so it only calculates the size of the button on the requirements of the borders
  3. Don't mess with setXxxSize, your ChatGUI should have no concept of the viewable size it might need to occupy.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top