Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.glorb.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: How to align swing buttons vertically ? Date: Sun, 13 Nov 2011 11:03:39 -0800 (PST) Organization: http://groups.google.com Lines: 48 Message-ID: <2442894.209.1321211019540.JavaMail.geo-discussion-forums@prew38> References: <4ebf9c51$0$5055$ba620e4c@news.skynet.be> <4ebfc473$0$5044$ba620e4c@news.skynet.be> <4ebffe4c$0$5045$ba620e4c@news.skynet.be> Reply-To: comp.lang.java.programmer@googlegroups.com NNTP-Posting-Host: 173.164.137.214 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1321212646 14334 127.0.0.1 (13 Nov 2011 19:30:46 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sun, 13 Nov 2011 19:30:46 +0000 (UTC) In-Reply-To: <4ebffe4c$0$5045$ba620e4c@news.skynet.be> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=173.164.137.214; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Google-Web-Client: true Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:9930 Olivier Scalbert wrote: > Perhaps I have not understand, but with the following code, buttons are > not well aligned: > > import java.awt.*; > import javax.swing.*; > > public class TestViewer { > > public static void main(String[] args) { > EventQueue.invokeLater(new Runnable() { > public void run() { > JFrame frame = new ViewerFrame(); > frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); > frame.setSize(800, 600); Shouldn't you call 'pack()' right here? Layouts often don't work well without it. > frame.setVisible(true); > } > }); > } > } > > class ViewerFrame extends JFrame { > > public ViewerFrame() { > getContentPane().add(new JPanel(), BorderLayout.CENTER); > getContentPane().add(createBtnPanel(), BorderLayout.EAST); > } > > private JPanel createBtnPanel() { > JPanel btnPanel = new JPanel(); > > btnPanel.setLayout(new BoxLayout(btnPanel, BoxLayout.Y_AXIS)); > > btnPanel.add(new JButton("Button 1")); > btnPanel.add(new JButton("Button 2")); > btnPanel.add(new JButton("Long Button 3")); > btnPanel.add(new JButton("Button 4")); > btnPanel.add(new JButton("Button 5")); > > return btnPanel; > } > } -- Lew