Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #9927

Re: How to align swing buttons vertically ?

From Knute Johnson <nospam@knutejohnson.com>
Newsgroups comp.lang.java.programmer
Subject Re: How to align swing buttons vertically ?
Date 2011-11-13 11:01 -0800
Organization A noiseless patient Spider
Message-ID <j9p461$eqc$1@dont-email.me> (permalink)
References <4ebf9c51$0$5055$ba620e4c@news.skynet.be> <j9oef0$ti8$2@localhost.localdomain> <4ebfc473$0$5044$ba620e4c@news.skynet.be> <j9okpi$va3$1@localhost.localdomain> <4ebffe4c$0$5045$ba620e4c@news.skynet.be>

Show all headers | View raw


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<labels.length; i++) {
             add(new JButton(labels[i]),c);
             ++c.gridy;
         }
     }

     public static void main(String[] args) {
         EventQueue.invokeLater(new Runnable() {
             public void run() {
                 JFrame f = new JFrame();
                 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                 f.add(new test(),BorderLayout.EAST);
                 f.setSize(700,500);
                 f.setVisible(true);
             }
         });
     }
}

-- 

Knute Johnson

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to align swing buttons vertically ? Olivier Scalbert <olivier.scalbert@algosyn.com> - 2011-11-13 11:30 +0100
  Re: How to align swing buttons vertically ? Martin Gregorie <martin@address-in-sig.invalid> - 2011-11-13 12:50 +0000
    Re: How to align swing buttons vertically ? Olivier Scalbert <olivier.scalbert@algosyn.com> - 2011-11-13 14:21 +0100
      Re: How to align swing buttons vertically ? Martin Gregorie <martin@address-in-sig.invalid> - 2011-11-13 14:38 +0000
        Re: How to align swing buttons vertically ? Olivier Scalbert <olivier.scalbert@algosyn.com> - 2011-11-13 18:28 +0100
          Re: How to align swing buttons vertically ? Knute Johnson <nospam@knutejohnson.com> - 2011-11-13 11:01 -0800
            Re: How to align swing buttons vertically ? Olivier Scalbert <olivier.scalbert@algosyn.com> - 2011-11-13 21:17 +0100
              Re: How to align swing buttons vertically ? Knute Johnson <nospam@knutejohnson.com> - 2011-11-13 16:16 -0800
          Re: How to align swing buttons vertically ? Martin Gregorie <martin@address-in-sig.invalid> - 2011-11-13 19:17 +0000
            Re: How to align swing buttons vertically ? Olivier Scalbert <olivier.scalbert@algosyn.com> - 2011-11-13 21:01 +0100
              Re: How to align swing buttons vertically ? "John B. Matthews" <nospam@nospam.invalid> - 2011-11-13 17:33 -0500
          Re: How to align swing buttons vertically ? Lew <lewbloch@gmail.com> - 2011-11-13 11:03 -0800
            Re: How to align swing buttons vertically ? Knute Johnson <nospam@knutejohnson.com> - 2011-11-13 16:28 -0800
  Re: How to align swing buttons vertically ? Roedy Green <see_website@mindprod.com.invalid> - 2011-11-13 09:27 -0800

csiph-web