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


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

Re: align Swing JLabels and JTextFields vertically with

From Knute Johnson <nospam@knutejohnson.com>
Newsgroups comp.lang.java.programmer
Subject Re: align Swing JLabels and JTextFields vertically with
References <af65db34-f2a6-474b-9a33-6a4c4b8f902f@a19g2000prj.googlegroups.com> <1S0sp.9618$__1.1031@newsfe03.iad>
Message-ID <Nb1sp.13785$vC5.10856@newsfe01.iad> (permalink)
Organization NewsDemon
Date 2011-04-21 14:19 -0700

Show all headers | View raw


On 04/21/2011 01:56 PM, Knute Johnson wrote:

> I posted a simple solution to this a few weeks ago. I don't remember if
> it was you who posted or not but nothing was heard out of the original
> poster.
>
> The solution basically to not use multiple JPanels but to put all of the
> fields in one panel, add some extra padding where you want the borders
> and then to put the border around the bounds of the components.
>
> I'll see if I can find it again.
>

I found it.  It won't work quite as you have specified as it only allows 
one border per JComponent.  I've got a couple of ideas though, I'll get 
back to you.

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;

public class test extends JPanel {
     JLabel l1,l2;
     JTextField tf1,tf2;

     public test() {
         super(new GridBagLayout());

         GridBagConstraints c = new GridBagConstraints();
         c.gridy = 0;  c.insets = new Insets(2,2,2,2);
         c.fill = GridBagConstraints.HORIZONTAL;

         add(new JLabel("Label 000"),c);
         add(new JTextField("Field 0"),c);

         ++c.gridy;
         c.insets = new Insets(20,2,2,2);
         l1 = new JLabel("Label 1");
         add(l1,c);
         tf1 = new JTextField("Field 00001");
         add(tf1,c);

         ++c.gridy;
         c.insets = new Insets(2,2,2,2);
         l2 = new JLabel("Label 2");
         add(l2,c);
         tf2 = new JTextField("Field 2");
         add(tf2,c);

         setBorder(new MyTitledBorder(
          BorderFactory.createLineBorder(Color.BLACK,2),"Title"));
     }

     class MyTitledBorder extends TitledBorder {
         Rectangle rv;

         public MyTitledBorder(Border border, String title) {
             super(border,title);
         }

         public void paintBorder(Component c, Graphics g, int x, int y,
          int width, int height) {
             rv = l1.getBounds();
             rv.add(tf1.getBounds());
             rv.add(l2.getBounds());
             rv.add(tf2.getBounds());
             rv.grow(10,10);

             super.paintBorder(c,g,(int)rv.getX(),(int)rv.getY()-10,
              (int)rv.getWidth(),(int)rv.getHeight()+10);
         }
     }

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

-- 

Knute Johnson
s/knute/nospam/

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


Thread

align Swing JLabels and JTextFields vertically with albert kao <albertkao3@gmail.com> - 2011-04-21 09:49 -0700
  Re: align Swing JLabels and JTextFields vertically with Knute Johnson <nospam@knutejohnson.com> - 2011-04-21 13:56 -0700
    Re: align Swing JLabels and JTextFields vertically with Knute Johnson <nospam@knutejohnson.com> - 2011-04-21 14:19 -0700
      Re: align Swing JLabels and JTextFields vertically with Knute Johnson <nospam@knutejohnson.com> - 2011-04-21 16:47 -0700
        Re: align Swing JLabels and JTextFields vertically with albert kao <albertkao3@gmail.com> - 2011-04-26 07:02 -0700
          Re: align Swing JLabels and JTextFields vertically with Knute Johnson <nospam@knutejohnson.com> - 2011-04-26 10:23 -0700
  Re: align Swing JLabels and JTextFields vertically with Roedy Green <see_website@mindprod.com.invalid> - 2011-04-22 12:22 -0700

csiph-web