Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: Knute Johnson Newsgroups: comp.lang.java.programmer Subject: Re: How to align swing buttons vertically ? Date: Sun, 13 Nov 2011 11:01:24 -0800 Organization: A noiseless patient Spider Lines: 92 Message-ID: References: <4ebf9c51$0$5055$ba620e4c@news.skynet.be> <4ebfc473$0$5044$ba620e4c@news.skynet.be> <4ebffe4c$0$5045$ba620e4c@news.skynet.be> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 13 Nov 2011 19:01:21 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="mz/LDSJwiWnk3Jnnqg7x+Q"; logging-data="15180"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+gio2M8f0mofybvL4XBCgj" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20111105 Thunderbird/8.0 In-Reply-To: <4ebffe4c$0$5045$ba620e4c@news.skynet.be> Cancel-Lock: sha1:udCKPQvf34qfEgvuxHwBxb6w8sU= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:9927 On 11/13/2011 9:28 AM, Olivier Scalbert wrote: > On 11/13/2011 03:38 PM, Martin Gregorie wrote: >> That's nothing to do with the layout. It aligns the objects you're laying >> out by doing its best with the sizes of the objects you created. >> >> JButton inherits size setting methods (setMaximumSize, setMinimumSize, >> setPreferredSize) from JComponent. Use them to control button size. >> >> >> > > 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); > 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; > } > } > > Olivier import java.awt.*; import java.awt.event.*; import javax.swing.*; public class test extends JPanel { public test() { super(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL; c.gridy = 0; String labels[] = {"Button 1","Button 2", "Long Button 3","Button 4","Button 5"}; for (int i=0; i