Created
December 8, 2012 13:59
-
-
Save jnizet/4240355 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bar.baz; | |
import java.awt.BorderLayout; | |
import java.awt.Color; | |
import java.awt.Dimension; | |
import javax.swing.BorderFactory; | |
import javax.swing.Box; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
import javax.swing.JPanel; | |
import javax.swing.JScrollPane; | |
import javax.swing.SwingUtilities; | |
public class ContactDemo { | |
private static class ContactPanel extends JPanel { | |
public ContactPanel(String name) { | |
setLayout(new BorderLayout()); | |
add(new JLabel(name)); | |
setBorder(BorderFactory.createLineBorder(Color.BLUE)); | |
} | |
@Override | |
public Dimension getMaximumSize() { | |
return new Dimension(super.getMaximumSize().width, super.getPreferredSize().height); | |
} | |
} | |
private static void showGUI() { | |
JFrame f = new JFrame(); | |
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
Box contactsPanel = Box.createVerticalBox(); | |
contactsPanel.add(new ContactPanel("Bob")); | |
contactsPanel.add(new ContactPanel("John")); | |
contactsPanel.add(new ContactPanel("Melissa")); | |
contactsPanel.add(Box.createVerticalGlue()); | |
JScrollPane scroll = new JScrollPane(contactsPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); | |
f.add(scroll); | |
f.pack(); | |
f.setVisible(true); | |
} | |
public static void main(String... args) { | |
SwingUtilities.invokeLater(new Runnable() { | |
@Override | |
public void run() { | |
showGUI(); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment