Swing: align JLabel, JTextField inside TitledBorder

Discussion in 'Java' started by albert kao, Mar 10, 2011.

  1. albert kao

    albert kao Guest

    How to align JLabel & JTextField which are outside a TitledBorder with
    the JLabel & JTextField which are inside a TitledBorder?
    My simplified Swing test program is as follows.
    I use GridBagLayout for more powerful control.
    i.e. how to align lastNameLabel with cityLabel & streetNumberLabel,
    lastNameText with cityText & streetNumberText?

    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.border.LineBorder;
    import javax.swing.border.TitledBorder;

    public class SwingAlignQuestion {
    public static void main(String args[]) {
    JPanel namePanel;
    JPanel addressPanel;
    JLabel lastNameLabel;
    JTextField lastNameText;
    JLabel cityLabel;
    JTextField cityText;
    JLabel streetNumberLabel;
    JTextField streetNumberText;
    TitledBorder addressBorder;

    JFrame frame = new JFrame("Alignment Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new GridBagLayout());
    namePanel = new JPanel(new GridBagLayout());
    addressPanel = new JPanel(new GridBagLayout());
    addressBorder = new
    TitledBorder(LineBorder.createGrayLineBorder(), "");
    addressPanel.setBorder(addressBorder);
    lastNameLabel = new JLabel();
    lastNameText = new JTextField();
    cityLabel = new JLabel();
    cityText = new JTextField();
    streetNumberLabel = new JLabel();
    streetNumberText = new JTextField();
    lastNameLabel.setText("lastName");
    cityLabel.setText("co");
    streetNumberLabel.setText("streetNumber");
    Insets insets1 = new Insets(10, 10, 0, 0);
    Insets insets2 = new Insets(10, 10, 0, 0);
    GridBagConstraints constraints = new GridBagConstraints(
    0, 12,
    1, 1,
    0.0, 0.0,
    GridBagConstraints.WEST,
    GridBagConstraints.NONE,
    insets1,
    0, 0);
    constraints.gridx = 0;
    constraints.gridy = 0;
    constraints.anchor = GridBagConstraints.EAST;
    constraints.insets = insets1;
    namePanel.add(lastNameLabel, constraints);
    constraints.gridx = 1;
    constraints.anchor = GridBagConstraints.WEST;
    constraints.insets = insets2;
    namePanel.add(lastNameText, constraints);
    constraints.gridx = 0;
    constraints.gridy = 0;
    constraints.anchor = GridBagConstraints.EAST;
    constraints.fill = GridBagConstraints.NONE;
    constraints.insets = insets1;
    addressPanel.add(cityLabel, constraints);
    constraints.gridx = 1;
    constraints.anchor = GridBagConstraints.WEST;
    constraints.insets = insets2;
    addressPanel.add(cityText, constraints);
    constraints.gridx = 0;
    constraints.gridy = 1;
    constraints.anchor = GridBagConstraints.EAST;
    constraints.fill = GridBagConstraints.NONE;
    constraints.insets = insets1;
    addressPanel.add(streetNumberLabel, constraints);
    constraints.gridx = 1;
    constraints.anchor = GridBagConstraints.WEST;
    constraints.insets = insets2;
    addressPanel.add(streetNumberText, constraints);
    constraints.gridx = 0;
    constraints.gridy = 0;
    constraints.anchor = GridBagConstraints.WEST;
    constraints.insets = insets1;
    frame.add(namePanel, constraints);
    constraints.gridx = 0;
    constraints.gridy = 1;
    constraints.insets = insets1;
    frame.add(addressPanel, constraints);
    frame.setSize(423, 171);
    frame.setVisible(true);
    }
    }
     
    albert kao, Mar 10, 2011
    #1
    1. Advertising

  2. albert kao

    markspace Guest

    Try Matisse:

    <http://netbeans.org/kb/trails/matisse.html>


    package test;

    /**
    *
    * @author Brenden
    */
    public class LabelAlign extends javax.swing.JFrame {

    /** Creates new form LabelAlign */
    public LabelAlign() {
    initComponents();
    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jTextField2 = new javax.swing.JTextField();
    jTextField3 = new javax.swing.JTextField();
    jLabel3 = new javax.swing.JLabel();
    jTextField1 = new javax.swing.JTextField();


    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);


    jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new
    java.awt.Color(0, 0, 0)));

    jLabel1.setText("jLabel1");

    jLabel2.setText("jLabel2");

    jTextField2.setText("jTextField2");

    jTextField3.setText("jTextField3");

    javax.swing.GroupLayout jPanel1Layout = new
    javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(

    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addContainerGap(114, Short.MAX_VALUE)

    ..addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

    ..addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
    jPanel1Layout.createSequentialGroup()
    .addComponent(jLabel1)
    .addGap(18, 18, 18)
    .addComponent(jTextField2,
    javax.swing.GroupLayout.PREFERRED_SIZE,
    javax.swing.GroupLayout.DEFAULT_SIZE,
    javax.swing.GroupLayout.PREFERRED_SIZE))

    ..addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
    jPanel1Layout.createSequentialGroup()
    .addComponent(jLabel2)
    .addGap(18, 18, 18)
    .addComponent(jTextField3,
    javax.swing.GroupLayout.PREFERRED_SIZE,
    javax.swing.GroupLayout.DEFAULT_SIZE,
    javax.swing.GroupLayout.PREFERRED_SIZE)))
    .addContainerGap())
    );
    jPanel1Layout.setVerticalGroup(

    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addContainerGap()

    ..addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jTextField2,
    javax.swing.GroupLayout.PREFERRED_SIZE,
    javax.swing.GroupLayout.DEFAULT_SIZE,
    javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jLabel1))
    .addGap(26, 26, 26)

    ..addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jTextField3,
    javax.swing.GroupLayout.PREFERRED_SIZE,
    javax.swing.GroupLayout.DEFAULT_SIZE,
    javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jLabel2))
    .addContainerGap(91, Short.MAX_VALUE))
    );

    jLabel3.setText("jLabel3");

    jTextField1.setText("jTextField1");

    javax.swing.GroupLayout layout = new
    javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(

    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()

    ..addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(84, 84, 84)
    .addComponent(jPanel1,
    javax.swing.GroupLayout.PREFERRED_SIZE,
    javax.swing.GroupLayout.DEFAULT_SIZE,
    javax.swing.GroupLayout.PREFERRED_SIZE))

    ..addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
    layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jLabel3)
    .addGap(18, 18, 18)
    .addComponent(jTextField1,
    javax.swing.GroupLayout.PREFERRED_SIZE,
    javax.swing.GroupLayout.DEFAULT_SIZE,
    javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(14, 14, 14)))
    .addContainerGap(79, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(

    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(59, 59, 59)

    ..addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jTextField1,
    javax.swing.GroupLayout.PREFERRED_SIZE,
    javax.swing.GroupLayout.DEFAULT_SIZE,
    javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jLabel3))

    ..addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
    .addComponent(jPanel1,
    javax.swing.GroupLayout.PREFERRED_SIZE,
    javax.swing.GroupLayout.DEFAULT_SIZE,
    javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(40, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new LabelAlign().setVisible(true);
    }
    });
    }

    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration

    }
     
    markspace, Mar 10, 2011
    #2
    1. Advertising

  3. In article <ilb4qf$p92$-september.org>, markspace <-@.>
    wrote:

    > Try Matisse:
    >
    > <http://netbeans.org/kb/trails/matisse.html>
    >
    > package test;

    [...]

    It's a little off on Aqua L&F:

    <http://i56.tinypic.com/23ua53r.png>

    It might be worth experimenting with an "invisible" titled border, e.g.:

    TitledBorder nameBorder;
    ...
    namePanel = new JPanel(new GridBagLayout());
    nameBorder = new TitledBorder(
    new LineBorder(namePanel.getBackground()), "");
    namePanel.setBorder(nameBorder);

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Mar 10, 2011
    #3
  4. albert kao

    markspace Guest

    On 3/10/2011 11:57 AM, John B. Matthews wrote:

    > It's a little off on Aqua L&F:



    It's a little off on mine too, just because I was aligning them by eye
    and didn't really do it well. You idea to add a panel with out a border
    to aid the alignment is a pretty good one. It took me less than 30
    seconds to do in Matisse.

    package test;

    /**
    *
    * @author Brenden
    */
    public class LabelAlign extends javax.swing.JFrame {

    /** Creates new form LabelAlign */
    public LabelAlign() {
    initComponents();
    }

    /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jTextField2 = new javax.swing.JTextField();
    jTextField3 = new javax.swing.JTextField();
    jPanel2 = new javax.swing.JPanel();
    jTextField1 = new javax.swing.JTextField();
    jLabel3 = new javax.swing.JLabel();


    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);


    jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new
    java.awt.Color(0, 0, 0)));

    jLabel1.setText("jLabel1");

    jLabel2.setText("jLabel2");

    jTextField2.setText("jTextField2");

    jTextField3.setText("jTextField3");

    javax.swing.GroupLayout jPanel1Layout = new
    javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(

    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addContainerGap(114, Short.MAX_VALUE)

    ..addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

    ..addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
    jPanel1Layout.createSequentialGroup()
    .addComponent(jLabel1)
    .addGap(18, 18, 18)
    .addComponent(jTextField2,
    javax.swing.GroupLayout.PREFERRED_SIZE,
    javax.swing.GroupLayout.DEFAULT_SIZE,
    javax.swing.GroupLayout.PREFERRED_SIZE))

    ..addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
    jPanel1Layout.createSequentialGroup()
    .addComponent(jLabel2)
    .addGap(18, 18, 18)
    .addComponent(jTextField3,
    javax.swing.GroupLayout.PREFERRED_SIZE,
    javax.swing.GroupLayout.DEFAULT_SIZE,
    javax.swing.GroupLayout.PREFERRED_SIZE)))
    .addContainerGap())
    );
    jPanel1Layout.setVerticalGroup(

    jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(jPanel1Layout.createSequentialGroup()
    .addContainerGap()

    ..addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jTextField2,
    javax.swing.GroupLayout.PREFERRED_SIZE,
    javax.swing.GroupLayout.DEFAULT_SIZE,
    javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jLabel1))
    .addGap(26, 26, 26)

    ..addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jTextField3,
    javax.swing.GroupLayout.PREFERRED_SIZE,
    javax.swing.GroupLayout.DEFAULT_SIZE,
    javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jLabel2))
    .addContainerGap(91, Short.MAX_VALUE))
    );

    jTextField1.setText("jTextField1");

    jLabel3.setText("jLabel3");

    javax.swing.GroupLayout jPanel2Layout = new
    javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(

    jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
    jPanel2Layout.createSequentialGroup()
    .addContainerGap(116, Short.MAX_VALUE)
    .addComponent(jLabel3)
    .addGap(18, 18, 18)
    .addComponent(jTextField1,
    javax.swing.GroupLayout.PREFERRED_SIZE,
    javax.swing.GroupLayout.DEFAULT_SIZE,
    javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap())
    );
    jPanel2Layout.setVerticalGroup(

    jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
    jPanel2Layout.createSequentialGroup()
    .addContainerGap(39, Short.MAX_VALUE)

    ..addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
    .addComponent(jTextField1,
    javax.swing.GroupLayout.PREFERRED_SIZE,
    javax.swing.GroupLayout.DEFAULT_SIZE,
    javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jLabel3))
    .addContainerGap())
    );

    javax.swing.GroupLayout layout = new
    javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(

    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addGap(84, 84, 84)

    ..addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addComponent(jPanel1,
    javax.swing.GroupLayout.DEFAULT_SIZE,
    javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    .addComponent(jPanel2,
    javax.swing.GroupLayout.PREFERRED_SIZE,
    javax.swing.GroupLayout.DEFAULT_SIZE,
    javax.swing.GroupLayout.PREFERRED_SIZE))
    .addContainerGap(79, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(

    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
    .addGroup(layout.createSequentialGroup()
    .addContainerGap()
    .addComponent(jPanel2,
    javax.swing.GroupLayout.PREFERRED_SIZE,
    javax.swing.GroupLayout.DEFAULT_SIZE,
    javax.swing.GroupLayout.PREFERRED_SIZE)

    ..addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
    .addComponent(jPanel1,
    javax.swing.GroupLayout.PREFERRED_SIZE,
    javax.swing.GroupLayout.DEFAULT_SIZE,
    javax.swing.GroupLayout.PREFERRED_SIZE)
    .addContainerGap(62, Short.MAX_VALUE))
    );

    pack();
    }// </editor-fold>

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
    public void run() {
    new LabelAlign().setVisible(true);
    }
    });
    }

    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration

    }
     
    markspace, Mar 10, 2011
    #4
  5. In article <ilbhmf$o8c$-september.org>, markspace <-@.>
    wrote:

    > On 3/10/2011 11:57 AM, John B. Matthews wrote:
    >
    > > It's a little off on Aqua L&F:

    >
    >
    > It's a little off on mine too, just because I was aligning them by eye
    > and didn't really do it well. You idea to add a panel with out a border
    > to aid the alignment is a pretty good one. It took me less than 30
    > seconds to do in Matisse.
    >
    > package test;

    [...]

    Excellent: <http://i54.tinypic.com/1zx9ro8.png>

    I can see/edit the generated code in the fold, but I think that I'd need
    src/test/LabelAlign.form to edit the project in NetBeans/Matisse design
    mode.

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Mar 11, 2011
    #5
  6. albert kao

    markspace Guest

    On 3/10/2011 6:37 PM, John B. Matthews wrote:

    > I can see/edit the generated code in the fold, but I think that I'd need
    > src/test/LabelAlign.form to edit the project in NetBeans/Matisse design
    > mode.
    >



    <?xml version="1.1" encoding="UTF-8" ?>

    <Form version="1.3" maxVersion="1.7"
    type="org.netbeans.modules.form.forminfo.JFrameFormInfo">
    <Properties>
    <Property name="defaultCloseOperation" type="int" value="3"/>
    </Properties>
    <SyntheticProperties>
    <SyntheticProperty name="formSizePolicy" type="int" value="1"/>
    </SyntheticProperties>
    <AuxValues>
    <AuxValue name="FormSettings_autoResourcing"
    type="java.lang.Integer" value="0"/>
    <AuxValue name="FormSettings_autoSetComponentName"
    type="java.lang.Boolean" value="false"/>
    <AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean"
    value="true"/>
    <AuxValue name="FormSettings_generateMnemonicsCode"
    type="java.lang.Boolean" value="false"/>
    <AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean"
    value="false"/>
    <AuxValue name="FormSettings_layoutCodeTarget"
    type="java.lang.Integer" value="1"/>
    <AuxValue name="FormSettings_listenerGenerationStyle"
    type="java.lang.Integer" value="0"/>
    <AuxValue name="FormSettings_variablesLocal"
    type="java.lang.Boolean" value="false"/>
    <AuxValue name="FormSettings_variablesModifier"
    type="java.lang.Integer" value="2"/>
    </AuxValues>

    <Layout>
    <DimensionLayout dim="0">
    <Group type="103" groupAlignment="0" attributes="0">
    <Group type="102" attributes="0">
    <EmptySpace min="-2" pref="84" max="-2" attributes="0"/>
    <Group type="103" groupAlignment="0" attributes="0">
    <Component id="jPanel1" alignment="0" max="32767"
    attributes="1"/>
    <Component id="jPanel2" alignment="0" min="-2"
    max="-2" attributes="1"/>
    </Group>
    <EmptySpace pref="79" max="32767" attributes="0"/>
    </Group>
    </Group>
    </DimensionLayout>
    <DimensionLayout dim="1">
    <Group type="103" groupAlignment="0" attributes="0">
    <Group type="102" alignment="0" attributes="0">
    <EmptySpace max="-2" attributes="0"/>
    <Component id="jPanel2" min="-2" max="-2" attributes="0"/>
    <EmptySpace type="unrelated" max="-2" attributes="0"/>
    <Component id="jPanel1" min="-2" max="-2" attributes="0"/>
    <EmptySpace pref="62" max="32767" attributes="0"/>
    </Group>
    </Group>
    </DimensionLayout>
    </Layout>
    <SubComponents>
    <Container class="javax.swing.JPanel" name="jPanel1">
    <Properties>
    <Property name="border" type="javax.swing.border.Border"
    editor="org.netbeans.modules.form.editors2.BorderEditor">
    <Border
    info="org.netbeans.modules.form.compat2.border.LineBorderInfo">
    <LineBorder/>
    </Border>
    </Property>
    </Properties>

    <Layout>
    <DimensionLayout dim="0">
    <Group type="103" groupAlignment="0" attributes="0">
    <Group type="102" attributes="0">
    <EmptySpace pref="114" max="32767" attributes="0"/>
    <Group type="103" groupAlignment="0" attributes="0">
    <Group type="102" alignment="1" attributes="0">
    <Component id="jLabel1" min="-2" max="-2"
    attributes="0"/>
    <EmptySpace type="separate" max="-2"
    attributes="0"/>
    <Component id="jTextField2" min="-2" max="-2"
    attributes="0"/>
    </Group>
    <Group type="102" alignment="1" attributes="0">
    <Component id="jLabel2" min="-2" max="-2"
    attributes="0"/>
    <EmptySpace type="separate" max="-2"
    attributes="0"/>
    <Component id="jTextField3" min="-2" max="-2"
    attributes="0"/>
    </Group>
    </Group>
    <EmptySpace max="-2" attributes="0"/>
    </Group>
    </Group>
    </DimensionLayout>
    <DimensionLayout dim="1">
    <Group type="103" groupAlignment="0" attributes="0">
    <Group type="102" alignment="0" attributes="0">
    <EmptySpace max="-2" attributes="0"/>
    <Group type="103" groupAlignment="3" attributes="0">
    <Component id="jTextField2" alignment="3"
    min="-2" max="-2" attributes="0"/>
    <Component id="jLabel1" alignment="3" min="-2"
    max="-2" attributes="0"/>
    </Group>
    <EmptySpace min="-2" pref="26" max="-2" attributes="0"/>
    <Group type="103" groupAlignment="3" attributes="0">
    <Component id="jTextField3" alignment="3"
    min="-2" max="-2" attributes="0"/>
    <Component id="jLabel2" alignment="3" min="-2"
    max="-2" attributes="0"/>
    </Group>
    <EmptySpace pref="91" max="32767" attributes="0"/>
    </Group>
    </Group>
    </DimensionLayout>
    </Layout>
    <SubComponents>
    <Component class="javax.swing.JLabel" name="jLabel1">
    <Properties>
    <Property name="text" type="java.lang.String" value="jLabel1"/>
    </Properties>
    </Component>
    <Component class="javax.swing.JLabel" name="jLabel2">
    <Properties>
    <Property name="text" type="java.lang.String" value="jLabel2"/>
    </Properties>
    </Component>
    <Component class="javax.swing.JTextField" name="jTextField2">
    <Properties>
    <Property name="text" type="java.lang.String"
    value="jTextField2"/>
    </Properties>
    </Component>
    <Component class="javax.swing.JTextField" name="jTextField3">
    <Properties>
    <Property name="text" type="java.lang.String"
    value="jTextField3"/>
    </Properties>
    </Component>
    </SubComponents>
    </Container>
    <Container class="javax.swing.JPanel" name="jPanel2">

    <Layout>
    <DimensionLayout dim="0">
    <Group type="103" groupAlignment="0" attributes="0">
    <Group type="102" alignment="1" attributes="0">
    <EmptySpace pref="116" max="32767" attributes="0"/>
    <Component id="jLabel3" min="-2" max="-2"
    attributes="0"/>
    <EmptySpace type="separate" max="-2" attributes="0"/>
    <Component id="jTextField1" min="-2" max="-2"
    attributes="0"/>
    <EmptySpace max="-2" attributes="0"/>
    </Group>
    </Group>
    </DimensionLayout>
    <DimensionLayout dim="1">
    <Group type="103" groupAlignment="0" attributes="0">
    <Group type="102" alignment="1" attributes="0">
    <EmptySpace pref="39" max="32767" attributes="0"/>
    <Group type="103" groupAlignment="3" attributes="0">
    <Component id="jTextField1" alignment="3"
    min="-2" max="-2" attributes="0"/>
    <Component id="jLabel3" alignment="3" min="-2"
    max="-2" attributes="0"/>
    </Group>
    <EmptySpace max="-2" attributes="0"/>
    </Group>
    </Group>
    </DimensionLayout>
    </Layout>
    <SubComponents>
    <Component class="javax.swing.JTextField" name="jTextField1">
    <Properties>
    <Property name="text" type="java.lang.String"
    value="jTextField1"/>
    </Properties>
    </Component>
    <Component class="javax.swing.JLabel" name="jLabel3">
    <Properties>
    <Property name="text" type="java.lang.String" value="jLabel3"/>
    </Properties>
    </Component>
    </SubComponents>
    </Container>
    </SubComponents>
    </Form>
     
    markspace, Mar 11, 2011
    #6
  7. In article <ilc7jp$d5a$-september.org>, markspace <-@.>
    wrote:

    > On 3/10/2011 6:37 PM, John B. Matthews wrote:
    >
    > > I can see/edit the generated code in the fold, but I think that I'd
    > > need src/test/LabelAlign.form to edit the project in
    > > NetBeans/Matisse design mode.

    >
    > <?xml version="1.1" encoding="UTF-8" ?>
    > [...]
    > </Form>


    Interesting. Thanks for abetting this experiment. Restoring the .form
    file allowed me to open the .java file in the designer, but with the
    error described here:

    <http://wiki.netbeans.org/FormGuardedBlockError>

    Restoring the missing comments fixed it. Looking back, you apparently
    copied and pasted the code from the NetBeans editor, which seems to
    elide the //GEN-BEGIN:name //GEN-END:name comment pairs. The restored
    comments are visible in a text editor such as vi, while remaining
    invisible in NetBeans.

    It looks like this may impact sharing designer files via text media such
    as usenet.

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Mar 11, 2011
    #7
  8. albert kao

    markspace Guest

    On 3/10/2011 10:09 PM, John B. Matthews wrote:

    > <http://wiki.netbeans.org/FormGuardedBlockError>
    >
    > Restoring the missing comments fixed it. Looking back, you apparently
    > copied and pasted the code from the NetBeans editor, which seems to
    > elide the //GEN-BEGIN:name //GEN-END:name comment pairs. The restored
    > comments are visible in a text editor such as vi, while remaining
    > invisible in NetBeans.



    Huh, I actually did copy it with vim. What version of NetBeans are you
    using? I'm on 6.9.1.

    Product Version: NetBeans IDE 6.9.1 (Build 201011082200)
    Java: 1.6.0_24; Java HotSpot(TM) Client VM 19.1-b02
    System: Windows 7 version 6.1 running on x86; Cp1252; en_US (nb)
     
    markspace, Mar 11, 2011
    #8
  9. On 03/10/2011 09:58 AM, albert kao wrote:
    > How to align JLabel& JTextField which are outside a TitledBorder with
    > the JLabel& JTextField which are inside a TitledBorder?
    > My simplified Swing test program is as follows.
    > I use GridBagLayout for more powerful control.
    > i.e. how to align lastNameLabel with cityLabel& streetNumberLabel,
    > lastNameText with cityText& streetNumberText?


    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/
     
    Knute Johnson, Mar 11, 2011
    #9
  10. In article <xDuep.47$>,
    Knute Johnson <> wrote:

    > public class test extends JPanel {...}


    Excellent!

    <http://i56.tinypic.com/24qs8pi.png>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Mar 12, 2011
    #10
  11. In article <ildh17$lht$-september.org>, markspace <-@.>
    wrote:

    > On 3/10/2011 10:09 PM, John B. Matthews wrote:
    >
    > > <http://wiki.netbeans.org/FormGuardedBlockError>
    > >
    > > Restoring the missing comments fixed it. Looking back, you
    > > apparently copied and pasted the code from the NetBeans editor,
    > > which seems to elide the //GEN-BEGIN:name //GEN-END:name comment
    > > pairs. The restored comments are visible in a text editor such as
    > > vi, while remaining invisible in NetBeans.

    >
    >
    > Huh, I actually did copy it with vim. What version of NetBeans are
    > you using? I'm on 6.9.1.
    >
    > Product Version: NetBeans IDE 6.9.1 (Build 201011082200)
    > Java: 1.6.0_24; Java HotSpot(TM) Client VM 19.1-b02
    > System: Windows 7 version 6.1 running on x86; Cp1252; en_US (nb)


    Essentially the same:

    Product Version: NetBeans IDE 6.9.1 (Build 201011082200)
    Java: 1.6.0_24; Java HotSpot(TM) 64-Bit Server VM 19.1-b02-334
    System: Mac OS X version 10.5.8 running on x86_64; MacRoman; en_US (nb)

    Looking at the last such comment, in NetBeans, I see this:

    // End of variables declaration

    In vim, et al., I see this:

    // End of variables declaration//GEN-END:variables

    Your post:

    <http://groups.google.com/group/comp.lang.java.programmer/msg/3132c45930563a18>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Mar 12, 2011
    #11
  12. albert kao

    markspace Guest

    On 3/11/2011 4:51 PM, John B. Matthews wrote:

    > In vim, et al., I see this:
    >
    > // End of variables declaration//GEN-END:variables



    Ah, in the .java file, not the form file. Yup, they're there in vim, I
    just was looking at the wrong file.
     
    markspace, Mar 12, 2011
    #12
  13. On 03/11/2011 04:39 PM, John B. Matthews wrote:
    > In article<xDuep.47$>,
    > Knute Johnson<> wrote:
    >
    >> public class test extends JPanel {...}

    >
    > Excellent!
    >
    > <http://i56.tinypic.com/24qs8pi.png>


    Thank you.

    Is that an Apple window or some sort of Linux? It's very clean and bright.

    --

    Knute Johnson
    s/knute/nospam/
     
    Knute Johnson, Mar 12, 2011
    #13
  14. In article <K4Bep.16831$>,
    Knute Johnson <> wrote:

    > On 03/11/2011 04:39 PM, John B. Matthews wrote:
    > > In article<xDuep.47$>,
    > > Knute Johnson<> wrote:
    > >
    > >> public class test extends JPanel {...}

    > >
    > > Excellent!
    > >
    > > <http://i56.tinypic.com/24qs8pi.png>

    >
    > Thank you.


    You're very welcome.

    > Is that an Apple window or some sort of Linux? It's very clean and bright.


    It's Mac OS X 10.5.8; cf. Ubuntu 10.04:

    <http://i55.tinypic.com/2qb86jd.png>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Mar 12, 2011
    #14
  15. On 03/11/2011 09:30 PM, John B. Matthews wrote:
    >
    > <http://i55.tinypic.com/2qb86jd.png>
    >


    This one is a Ubuntu theme? It's very nice, what is its name? I'm not
    a fan of the orange.

    Thanks,

    --

    Knute Johnson
    s/knute/nospam/
     
    Knute Johnson, Mar 12, 2011
    #15
  16. In article <WyPep.18828$>,
    Knute Johnson <> wrote:

    > On 03/11/2011 09:30 PM, John B. Matthews wrote:
    > >
    > > <http://i55.tinypic.com/2qb86jd.png>
    > >

    >
    > This one is a Ubuntu theme? It's very nice, what is its name? I'm
    > not a fan of the orange.


    I'd forgotten having changed it! Clearlooks:

    <http://en.wikipedia.org/wiki/File:Ubuntu_Clearlooks.png>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Mar 12, 2011
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Prashanth Ravi
    Replies:
    2
    Views:
    536
    Andrew Thompson
    Oct 7, 2004
  2. Prashanth Ravi
    Replies:
    1
    Views:
    434
    Andrew Thompson
    Oct 7, 2004
  3. Replies:
    5
    Views:
    3,053
    Andrew Thompson
    Dec 26, 2005
  4. David
    Replies:
    3
    Views:
    532
    Kenneth P. Turvey
    Nov 12, 2006
  5. brf
    Replies:
    2
    Views:
    940
    Knute Johnson
    Dec 4, 2006
Loading...

Share This Page