0

Here's a code of a silly application for data entry. I would like that the "oggetto" JTextArea to have a size so that the user can enter different lines of information:

JPanel body = new JPanel();
body.setLayout(new GridLayout(10, 1));
body.add(new JLabel("Inserimento di un nuovo protocollo"));

JTextArea oggetto = new JTextArea(5,1);
oggetto.setOpaque(true);
oggetto.setBackground(Color.cyan);
//oggetto.setSize(100, 100);

...
body.add(oggetto);
jframe.add(body);
jframe.setVisible(true);

I've tried many ways to make it but when I launch the app I only get a "oggetto" jtextarea of a unique line... can you help me?

SagittariusA
  • 5,289
  • 15
  • 73
  • 127
  • Please explain a bit. Not clear what you want to try? – Braj Jul 19 '14 at 10:21
  • Sorry...I mean that I want the oggetto JTextArea to have a certain height...so that the user can enter and read two or more lines of data. Now It displays me only a line, like the picture below: http://uploadpie.com/VMpNu – SagittariusA Jul 19 '14 at 10:25
  • The size of `oggetto` will be the same size as the largest component added to the `GridLayout` since it makes all components the same size. For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal, Complete, Verifiable Example). – Andrew Thompson Jul 19 '14 at 10:31

3 Answers3

4

I suggest you to use another layout such as GridBagLayout or BorderLayout or others that suits here.

Using GridBagLayout you can give the components size in percentage along horizontally and vertically.

Read more about How to Use GridBagLayout.

Note: Never call setSize() for the components and leave it for Layout Manager to decide the size and position of the component.

Read more Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?


Sample code (using GridBagLayout)

JPanel body = new JPanel();
body.setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.fill = GridBagConstants.BOTH;
gc.weightx = 1.0; // 100%

gc.gridx = 0;
gc.gridy = 0;
gc.weighty = 0.1;// 10%
body.add(new JLabel("Inserimento di un nuovo protocollo"), gc);

JTextArea oggetto = new JTextArea(5, 1);
oggetto.setOpaque(true);
oggetto.setRows(10);
oggetto.setBackground(Color.cyan);

gc.gridx = 0;
gc.gridy = 1;
gc.weighty = 0.9; // 90%
body.add(oggetto, gc);
Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
4

Problem is with the layout you have chosen. Because GridLayout divides your component into equal size blocks:

The GridLayout class is a layout manager that lays out a container's components in a rectangular grid. The container is divided into equal-sized rectangles, and one component is placed in each rectangle.

You should use GridBagLayout for more flexibility.

Sanjeev
  • 9,876
  • 2
  • 22
  • 33
2
       public static void main(String[] args) {

            JFrame frame = new JFrame();
            frame.setSize(new Dimension(400, 200));

            JPanel body = new JPanel();
            body.setLayout(new GridBagLayout());

            GridBagConstraints c1 = new GridBagConstraints();
            c1.fill = GridBagConstraints.HORIZONTAL;
            c1.weightx = 1.0;// 100%
            c1.gridx = 0;// column 0
            c1.gridy = 0;// row 0
            body.add(new JLabel("Inserimento di un nuovo protocollo"), c1);

            JTextArea oggetto = new JTextArea(5,1);
            oggetto.setOpaque(true);
            oggetto.setBackground(Color.cyan);

            GridBagConstraints c2 = new GridBagConstraints();
            c2.fill = GridBagConstraints.BOTH;
            c2.weightx = 1.0; // 100%
            c2.gridx = 0; // column 0
            c2.gridy = 1; // row 1

            body.add(oggetto, c2);
            frame.add(body);
            frame.setVisible(true);

        }

I recommend to use TableLayout since it the best layout to understand and to implement.

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(new Dimension(400, 200));

    JPanel body = new JPanel();

    double[][] dim = {
            {20, TableLayout.FILL, 20}, // column widths: 20 distance right/left, TableLayout.FILL usable place
            {20, 17, 5, TableLayout.FILL, 20}  // row height: 20 distance up/down, 17 place for label, 5 distance between label-textarea
            };

    body.setLayout(new TableLayout(dim));

    JTextArea oggetto = new JTextArea(5,1);
    oggetto.setOpaque(true);
    oggetto.setBackground(Color.cyan);


    body.add(new JLabel("Inserimento di un nuovo protocollo"), "1,1"); // col: 1, row: 1
    body.add(oggetto, "1,3");// col: 1(TableLayout.FILL), row: 3 (TableLayout.FILL)
    frame.add(body);
    frame.setVisible(true);
}

For more components to add define "dim" like this example shows:

double[][] dim = {
            {20, TableLayout.FILL, 20},
            {20, 17, 5, TableLayout.FILL /*label1,distance,component1*/, 17, 5, TableLayout.FILL/*label2,distance,component2 etc...*/, 20} 
    };
alex
  • 8,904
  • 6
  • 49
  • 75
  • Why is, what you recommending and what you portrayed in this code, doesn't coincides? – nIcE cOw Jul 19 '14 at 10:50
  • @LoryLory look at my TableLayout example. – alex Jul 19 '14 at 11:03
  • @dit Thanks, I think i've made it work. I'd have another question to put to you: how can I do so that all words are contained inside the text area? I mean that, unless I put the ENTER key, words keep on being written all in a single line and they are hidden by the border of the text area. I would like that when the border of the text area is reached, then a new line should be started. Can you help me? – SagittariusA Jul 19 '14 at 13:42
  • @LoryLory just use oggetto.setLineWrap(true); – alex Jul 19 '14 at 13:45
  • @dit I've become familiar with TableLayout and now I'd need to know the following: how can I initialize the array double[][] dim if I know the number of columns or rows only at runtime? For example I need to print a list of objects, books. I'd need only a column and as many rows as the list of book size. how can I do? – SagittariusA Jul 24 '14 at 14:02
  • double[][] arr = new double[1][list.size()]; and then init using for each loop – alex Jul 24 '14 at 14:14