Resizing a display

By: Nicholas Duchon, Apr 1, 2017

Key Concepts:




In a face to face class, I would create all this code as we talked and show you how the screens behave as they are resized and point out the effective use of screen area, vs wasted space.

In place of that, you should download the code I have put at the end of this posting and experiment with the code yourself. Try other layout managers, and consider using JSplitPane for even more control over the displays, giving the user even more control over exactly how they want to see the output.

Here are three ways to put two panels on a screen. The Java code for this example is shown at the end of this posting.

Approach
JFrame screen capture
Comments

FlowLayout


BAD
This is the simplest in one sense, but it doesn't resize nicely - the JTextArea size is fixed at the number of rows and columns specified as the area was instantiated.

Thus, if you want to see the stuff in the display, you MUST scroll, no matter how large or small your actual display is.

Code:

    setLayout (new FlowLayout ());
    add (jpButtons);
    add (jsp);

GridLayout


BETTER

A little better, but now the text area is limited to half the available area. as far as conveying useful information goes, the other half of a JFrame wasted space.

Code:

    setLayout (new GridLayout (0, 1));
    add (jpButtons);
    add (jsp);

BorderLayout


BEST!

Here, the text area will fill in all the display except the that required for the buttons.

If the JFrame is enlarged, the text area will fill in the new space, which makes an efficient use of the available display.

If you have a large enough display, you can resize this frame to see the entire contents of the text area at once.

Code:

    setLayout (new BorderLayout ());
    add (jpButtons, BorderLayout.NORTH);
    add (jsp, BorderLayout.CENTER);


Code Example:

// File: ResizeNicelyDemo.java
// Date: Apr 4, 2014
// Author: Nicholas Duchon
// Purpose: demonstrate the meaning of resize nicely

import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;

public class ResizeNicelyDemo extends JFrame {
   static final long serialVersionUID = 23432L; // stop warning about this
  
   public static void main (String args []) {
      // each new creates a new JFrame
      new ResizeNicelyDemo (0, 100, 100); // flow
      new ResizeNicelyDemo (1, 500, 100); // grid
      new ResizeNicelyDemo (2, 900, 100); // border
   } // end main
  
   public ResizeNicelyDemo (int type, int xloc, int yloc) {
      setTitle ("Frame: " + type);
      setLocation (xloc, yloc);
      setVisible (true);
      setSize (300, 300);
      setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
     
      JPanel jpButtons = new JPanel ();
     
      jpButtons.add (new JButton ("One"));
      jpButtons.add (new JButton ("Two"));
      jpButtons.add (new JButton ("Three"));

      JTextArea   jta = new JTextArea (10, 10);
      JScrollPane jsp = new JScrollPane (jta);
      jta.setText (someText (30, 150));
     
      switch (type) {
         case 0:
            setLayout (new FlowLayout ());
            add (jpButtons);
            add (jsp);
            break;
         case 1:
            setLayout (new GridLayout (0, 1));
            add (jpButtons);
            add (jsp);
            break;
         case 2:
            setLayout (new BorderLayout ());
            add (jpButtons, BorderLayout.NORTH);
            add (jsp, BorderLayout.CENTER);
            break;
      } // end select type?
      validate (); // Force drawing GUI elements after adding to frame
  
} // end int constructor
  
   // create some random text to fill text area and show scroll bars
   String someText (int rows, int cols) {
      String st = "";
      for (int i = 0; i < rows; i++) {
         for (int j = 1; j <= cols; j++) {
            st += (char)('a' + (i+j)%26);
            if (j % 6 == 0) st += " ";
         } // end for each character
         st += "\n";
      } // end for
      return st;
  
} // end someText
  
} // end class ResizeNicelyDemo