Search code examples
javaswinglayout-managerjtextareagridbaglayout

JTextArea and GridBagLayout: how to avoid changing column width as we write some text?


I would like to design a JFrame whose layout consists in 3 columns:

  • a left column containing a text editor (I have chosen a JTextArea);
  • center column containing a single button (JButton);
  • a right column containing another text editor.

I managed to do this using the GridBagLayout class. However, when I write some text in an editor, the width of the editor gets larger and the width of the other editor gets smaller.

Could help me to fix this?

Here is my code:

JFrame frame = new JFrame("My frame");
frame.setSize(1000, 1000);
frame.setLocation(500, 0;

Container pane = frame.getContentPane();
GridBagLayout layout = new GridBagLayout();
pane.setLayout(layout);

JTextArea textEditor = new JTextArea();

// LEFT TEXT EDITOR
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = 0;
constraints.gridy = 0;
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1;
constraints.weighty = 1;
JPanel leftPanel = new JPanel();
leftPanel.setBackground(Color.WHITE);
leftPanel.add(textEditor);
layout.setConstraints(leftPanel, constraints);
pane.add(leftPanel);

// CENTER BUTTON
constraints = new GridBagConstraints();
constraints.gridx = 1;
constraints.gridy = 0;
constraints.fill = GridBagConstraints.NONE;
JButton button = new JButton("Do");
layout.setConstraints(button, constraints);
pane.add(button);

// RIGHT TEXT EDITOR
constraints = new GridBagConstraints();
constraints.gridx = 2;
constraints.gridy = 0;
contraintes.fill = GridBagConstraints.BOTH;
contraintes.weightx = 1;
contraintes.weighty = 1;
JPanel rightPanel = new JPanel();
rightPanel.setBackground(Color.WHITE);
rightPanel.add(textEditor);
miseEnPage.setConstraints(rightPanel, constraints);
pane.add(rightPanel);

Solution

  • in your code i think you should use 2 JTextAreas

    and set each JTextArea

    setLineWrap(true)
    

    like so

    JFrame frame = new JFrame("My frame");
            frame.setSize(500, 500);
            frame.setLocation(500, 0);
    
            Container pane = frame.getContentPane();
            GridBagLayout layout = new GridBagLayout();
            pane.setLayout(layout);
    
            JTextArea textEditor = new JTextArea();//JTextArea 1
            textEditor.setLineWrap(true);//setLineWrap(true)
            JTextArea textEditor1 = new JTextArea();//JTextArea 2
            textEditor1.setLineWrap(true);//setLineWrap(true)
            // LEFT TEXT EDITOR
            GridBagConstraints constraints = new GridBagConstraints();
            constraints.gridx = 0;
            constraints.gridy = 0;
            constraints.fill = GridBagConstraints.BOTH;
            constraints.weightx = 1;
            constraints.weighty = 1;
            JPanel leftPanel = new JPanel();
            leftPanel.setBackground(Color.WHITE);
            leftPanel.add(textEditor);
            layout.setConstraints(leftPanel, constraints);
            pane.add(leftPanel);
    
            // CENTER BUTTON
            constraints = new GridBagConstraints();
            constraints.gridx = 1;
            constraints.gridy = 0;
            constraints.fill = GridBagConstraints.NONE;
            JButton button = new JButton("Do");
            layout.setConstraints(button, constraints);
            pane.add(button);
    
            // RIGHT TEXT EDITOR
            constraints = new GridBagConstraints();
            constraints.gridx = 2;
            constraints.gridy = 0;
            constraints.fill = GridBagConstraints.BOTH;
            constraints.weightx = 1;
            constraints.weighty = 1;
            JPanel rightPanel = new JPanel();
            rightPanel.setBackground(Color.WHITE);
            rightPanel.add(textEditor1);
            layout.setConstraints(rightPanel, constraints);
            pane.add(rightPanel);