Swing: align JLabel, JTextField inside TitledBorder

A

albert kao

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);
}
}
 
M

markspace

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

}
 
M

markspace

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

}
 
J

John B. Matthews

markspace <-@.> said:
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.
 
M

markspace

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>
 
J

John B. Matthews

markspace <-@.> said:
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.
 
M

markspace

<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)
 
K

Knute Johnson

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);
}
});
}
}
 
J

John B. Matthews

markspace <-@.> said:
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>
 
M

markspace

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top