0

I have an application where I use two JPanels. One of them is a PaintPanel. The second panel,the jtextfield and the jtextarea work fine but they look too cramped against the right side. I tried changing the sizes with setSize() but it didn't work.

The code for the paintpanel

public void center() {
        jpCenter = new PaintPanel();
        jpCenter.addMouseListener(this);
        jpCenter.setSize(100, 100);
        jpCenter.setBackground(Color.white);
        add(jpCenter, BorderLayout.CENTER);
    }

The code for the panel of the chatbox

public void east() {

        // CREATE EAST Panel
        gl = new GridLayout(4, 1);
        jpEast = new JPanel();
        jpEast.setSize(200, 200);
        jpEast.setLayout(gl);
        jpEast.setBackground(Color.white);

        label = new JLabel("Number of shapes: ");
        jpEast.add(label);
        // ADD TEXT FIELD
        jtf = new JTextField();
        jtf.setText("");
        jtf.setSize(200, 200);
        jpEast.add(jtf);

        // ADD BUTTON
        jbSend = new JButton("Send");
        jbSend.setEnabled(false);
        jbSend.setSize(20, 60);
        jpEast.add(jbSend);
        jbSend.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                send(jtf.getText());
                jtf.setText("");
            }
        });

        // ADD TEXT AREA
        jta = new JTextArea("");
        jta.setSize(100, 100);
        jpEast.add(jta);

        // ADD EAST panel
        add(jpEast, BorderLayout.EAST);
    }

enter image description here enter image description here

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

1 Answers1

1

Avoid setting the size of components since it can make them not work well on all platforms, and with JTextArea in particular, it will not allow it to expand correctly if held within a JScrollPane (which is where a JTextArea belongs). Note also that most layout managers don't even respect a component's size but rather its preferred size.

Instead, set the row and column properties of your JTextAreas (done most easily via the JTextArea(int row, int column) constructor), the column property of your JTextField, the font sizes of other components (if need be). Then allow your container (JPanel) layout managers and component's own preferred sizes size all appropriately when you call pack() on your top-level window (often a JFrame), after adding all components but before setting it visible.

For more specific help, consider posting an image of the GUI you're getting vs. the one you're trying to achieve.

  • I just posted the images. The layout I'm getting is the image above and I'm trying to achieve a layout like the one below for the chatbox @DontKnowMuchBut Getting Better –  May 26 '20 at 11:35
  • @HecticPlatypus: thanks for that, but I'm still a bit confused as to what it is you're after. Sorry. – DontKnowMuchBut Getting Better May 26 '20 at 21:42