final JDialog measuresDialog = new JDialog(); measuresDialog.setLayout(new BorderLayout());  final JPanel measuresPanel = new JPanel(); JScrollPane scrollPane = new JScrollPane(measuresPanel); scrollPane.getVerticalScrollBar().setUnitIncrement(10); measuresDialog.getContentPane().add(scrollPane);  JButton cancel = new JButton("Cancel"); cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { measuresDialog.setVisible(false); } }); JPanel panel = new JPanel(); panel.add(cancel); measuresDialog.getContentPane().add(panel, BorderLayout.SOUTH);  measuresDialog.setPreferredSize(new Dimension(300, 400)); measuresDialog.pack(); measuresDialog.setVisible(true);  new Thread(new Runnable() { public void run() { JTextArea first = null; measuresPanel.setLayout(new BoxLayout(measuresPanel, BoxLayout.Y_AXIS)); for (int i = 0; i < MEASURES.length; i++) { final JPanel measurePanel = new JPanel(new BorderLayout()); JTextArea text = new JTextArea(); text.setEditable(false); text.setLineWrap(true); text.setWrapStyleWord(true); text.setOpaque(false); text.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));  text.append(MEASURES[i].getDescription()); measurePanel.add(text); JButton add = new JButton("Add"); final int index = i; add.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { measure.addObject(MEASURES[index]); measuresDialog.setVisible(false); } }); measurePanel.add(add, BorderLayout.EAST); if (first == null) { first = text; EventQueue.invokeLater(new Runnable() { public void run() { measuresPanel.add(measurePanel); } }); } else { EventQueue.invokeLater(new Runnable() { public void run() { JPanel separator = new JPanel(); separator.setPreferredSize(new Dimension(0,5)); measuresPanel.add(separator); measuresPanel.add(measurePanel); measuresPanel.revalidate(); } }); } }  final JTextArea ffirst = first; EventQueue.invokeLater(new Runnable() { public void run() { ffirst.setCaretPosition(0); } }); } }).start();
measuresDialog.setVisible(false);
			public void actionPerformed(ActionEvent e) {				final JDialog measuresDialog = new JDialog();				measuresDialog.setLayout(new BorderLayout());								final JPanel measuresPanel = new JPanel();				JScrollPane scrollPane = new JScrollPane(measuresPanel);				scrollPane.getVerticalScrollBar().setUnitIncrement(10);				measuresDialog.getContentPane().add(scrollPane);								JButton cancel = new JButton("Cancel");				cancel.addActionListener(new ActionListener() {					public void actionPerformed(ActionEvent e) {						measuresDialog.setVisible(false);					}				});				JPanel panel = new JPanel();				panel.add(cancel);				measuresDialog.getContentPane().add(panel, BorderLayout.SOUTH);								measuresDialog.setPreferredSize(new Dimension(300, 400));				measuresDialog.pack();				measuresDialog.setVisible(true);								new Thread(new Runnable() {					public void run() {						JTextArea first = null;						measuresPanel.setLayout(new BoxLayout(measuresPanel, BoxLayout.Y_AXIS));						for (int i = 0; i < MEASURES.length; i++) {							final JPanel measurePanel = new JPanel(new BorderLayout());							JTextArea text = new JTextArea();							text.setEditable(false);							text.setLineWrap(true);							text.setWrapStyleWord(true);							text.setOpaque(false);							text.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));														text.append(MEASURES[i].getDescription());							measurePanel.add(text);							JButton add = new JButton("Add");							final int index = i;							add.addActionListener(new ActionListener() {								public void actionPerformed(ActionEvent e) {									measure.addObject(MEASURES[index]);									measuresDialog.setVisible(false);								}							});							measurePanel.add(add, BorderLayout.EAST);							if (first == null) {								first = text;								EventQueue.invokeLater(new Runnable() {									public void run() {										measuresPanel.add(measurePanel);									}								});							} else {								EventQueue.invokeLater(new Runnable() {									public void run() {										JPanel separator = new JPanel();										separator.setPreferredSize(new Dimension(0,5));										measuresPanel.add(separator);										measuresPanel.add(measurePanel);										measuresPanel.revalidate();									}								});							}						}												final JTextArea ffirst = first; // must be final						EventQueue.invokeLater(new Runnable() {							public void run() {								ffirst.setCaretPosition(0);							}						});					}				}).start();			}