How to display JTextArea in the form of a table with GridLayout in Java?

Java 8Object Oriented ProgrammingProgramming

Display a component in the form of rows and columns using the GridLayout. Here, we have set a panel, within which we will create a layout with 3 rows and 5 columns −

JPanel panel = new JPanel(new GridLayout(3, 5, 5, 5));

Now, loop through and display JTextArea from 1 to 15 i.e. 3 rows and 5 columns −

for (int i = 1; i <= 15; i++) {
   panel.add(new JTextArea("Displaying TextArea "+String.valueOf(i)));
}

The following is an example to display text area in the form of a table with GridLayout -

Example

package my;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class SwingDemo {
   public static void main(String[] args) {
      JFrame frame = new JFrame("GridLayout Demo");
      JPanel panel = new JPanel(new GridLayout(3, 5, 5, 5));
      for (int i = 1; i <= 15; i++) {
         panel.add(new JTextArea("Displaying TextArea "+String.valueOf(i)));
      }
      frame.add(panel);
      frame.setSize(650, 300);
      frame.setVisible(true);
   }
}

Output

raja
Published on 03-Jun-2019 11:44:12
Advertisements