import java.awt.Button; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.List; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class ListTest extends Frame implements ActionListener { private final List list; private final Button button; public static void main(String ar[]) { Frame f = new ListTest(); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); f.setSize(350, 350); f.setVisible(true); } ListTest() { super("ListTest"); setLayout(new FlowLayout()); button = new Button("List.getPreferredSize()"); button.addActionListener(this); add(button); list = new List(5) { public Dimension getPreferredSize() { Dimension d = super.getPreferredSize(); System.out.println("List.getPreferredSize = " + d.width + "x" + d.height); return d; }; }; list.add("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); add(list); } public void actionPerformed(ActionEvent e) { Dimension d = list.getPreferredSize(); button.setBackground(Color.YELLOW); button.setLabel("Width:" + d.width + " Height:" + d.height); } }