Home Segments Top Top Previous Next

730: Mainline

For a layout manager to place components in a content pane where you want those components, you may have to define getMinimumSize, getMaximumSize, or getPreferredSize methods for the components. Java uses those methods, if they are defined, to determine the ultimate size of each component.

To place a Meter instance in a surrounding MovieApplication's content pane, with other components, you should define getMinimumSize and getPreferredSize methods:

import java.awt.*;
import javax.swing.*;
public class Meter extends JComponent implements MeterInterface {
 String title = "Title to be Supplied";
 int minValue, maxValue, value;
 public Meter (int x, int y) {
  minValue = x;
  maxValue = y;
  value = (y + x) / 2;
 }
 public void setValue(int v) {value = v; repaint();}
 public void setTitle(String t) {title = t; repaint();}
 // Getter to be defined
 public void paint(Graphics g) {
  g.drawLine(0, 50, 100, 50);
  g.drawLine(50, 50, 50, 40);
 }
 public Dimension getMinimumSize() {return new Dimension(150, 150);}   
 public Dimension getPreferredSize() {return new Dimension(150, 150);} 
}