0

I'm looking to center the JTextArea I have in the bottom of my swing application. I've been playing with the coordinates of the gridbag layout, but cannot seem to figure out how to extend the JTEXT AREA to fill up the space under the enter button. gui

package guiprojj;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.json.parsers.JSONParser;
import com.json.parsers.JsonParserFactory;


public class gui {
    public static void main(String[] args)
    {
        JFrame maingui = new JFrame("Gui");
        maingui.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.VERTICAL;
        JButton enter = new JButton("Enter");
        c.gridx = 2;
        c.gridy = 1;
        maingui.add(enter, c);
        final JTextArea movieinfo = new JTextArea(5,20);
        movieinfo.setBorder(BorderFactory.createMatteBorder(2,2,2,2,Color.red));
        final JTextField movietext = new JTextField(18);
        c.gridx = 1;
        c.gridy = 1;
        maingui.add(movietext, c);
        final JScrollPane scrolll = new JScrollPane(movieinfo);
        c.gridx = 1;
        c.gridy = 4;
        maingui.add(scrolll, c);

        final JLabel titlee = new JLabel("Enter movie name below!");
        c.gridx = 1;
        c.gridy = 0;
        maingui.add(titlee, c);
        maingui.setResizable(false);
        maingui.setVisible(true);
        movieinfo.setLineWrap(true);
        movieinfo.setWrapStyleWord(true);
        movieinfo.setEditable(false);

        scrolll.getPreferredSize();
        //pangui.setPreferredSize(new Dimension(300, 150));
        //pangui.add(scrolll, BorderLayout.CENTER);
        //movieinfo.add(scrolll);
        maingui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        maingui.pack();
        enter.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)  
            {
                System.out.println(Test.getMovieInfo(movietext.getText()));
                 JsonParserFactory factory=JsonParserFactory.getInstance();
                 JSONParser parser=factory.newJsonParser();
                 Map jsonData=parser.parseJson(Test.getMovieInfo(movietext.getText()));
                 String Title = (String)jsonData.get("Title");
                 String Year = (String)jsonData.get("Year");
                 String Plot = (String)jsonData.get("Plot");
                 movieinfo.setText("Title: "+Title+"\nYear: "+ Year +"\nPlot: "+Plot);
            }
            });

        }
}
mre
  • 43,520
  • 33
  • 120
  • 170
Eric Lang
  • 274
  • 1
  • 2
  • 16
  • 1
    Read the link to Swing tutorial that you were given in you last question related to this topic (http://stackoverflow.com/questions/18876646/creating-gridbag-layout/18876704#18876704). Did you read the section on spanning cells? Also you are allowed to use more than one layout manager. Since you haven't thanked anybody for the help given there, I'm not about to spoon feed you an answer again. – camickr Sep 18 '13 at 16:23
  • 1
    you have to insert something like a flow layout in row 1 and the `JTextArea` in row 2. I suggest you to use `BoxLayout` that in my opinion is better, please read this answer of mine for explanation. http://stackoverflow.com/questions/12835198/positioning-of-components-how-to-place-a-few-buttons-center-screen-same-size/12837118#12837118 and http://stackoverflow.com/questions/12774432/jframe-not-the-right-size-when-its-executed-any-reason-why/12780478#12780478 – Gianmarco Sep 18 '13 at 16:25
  • @camickr My apologies, I had plans of thanking people for helping me. – Eric Lang Sep 18 '13 at 16:54
  • @EricLang, and yet you still haven't accepted either of the answers here either. – camickr Sep 19 '13 at 01:36

2 Answers2

0

You can achieve it by adding gridwidth constraint like below:

    final JScrollPane scrolll = new JScrollPane(movieinfo);
    c.gridx = 1;
    c.gridy = 4;
    c.gridwidth = 2;
    maingui.add(scrolll, c);
Paweł Piecyk
  • 2,749
  • 15
  • 17
0
  • Use the constructor of JTextArea that doesnt specify the number of rows & columns
  • Set fill property for GridBagConstraints to BOTH
  • Set non-zero weighty for expansion along Y axis, e.g. c.weighty = 1
Reimeus
  • 158,255
  • 15
  • 216
  • 276