Hola, estoy tratando de implementar mi propio layout. La idea es un label y un componente, que vendria a ser el campo, dispuestos por fila.
Mi problema es que impelmento preferredLayoutSize y minimumLayoutSize, pero en el frame en el que lo estoy probando esto no se respeta, es decir puedo reducir todo lo que quiero el frame. Aqui va el codigo. Alguien me puede ayudar?. Gracias
LAYOUT
public class ColumnLayout implements LayoutManager{
private int _V_Space = 5;
private int _H_Space = 5;
private Vector Labels = new Vector();
private Vector Campos = new Vector();
/** Creates a new instance of ColumnLayout */
public ColumnLayout() {
}
public void layoutContainer(Container parent) {
Insets Bordes = parent.getInsets();
Component comp;
int maxwidth = parent.getWidth() - (Bordes.left + Bordes.right + _H_Space*3);
int PosY = Bordes.top - 8 ;
int Ancho;
int Alto;
int Alto_Campo;
int maxwidth_Label = 0;
int i = 0;
Enumeration labelIt = Labels.elements();
while (labelIt.hasMoreElements()) {
comp = (Component)labelIt.nextElement();
Ancho = comp.getPreferredSize().width;
Alto = comp.getPreferredSize().height;
Alto_Campo = ((Component)Campos.get(i)).getPreferredSize().heig ht;
PosY += (_V_Space+2);
comp.setBounds(_H_Space + Bordes.left, PosY, Ancho, Alto);
PosY += Alto_Campo;
maxwidth_Label = Math.max(maxwidth_Label, Ancho);
i++;
}
PosY = Bordes.top - 8;
Enumeration camposIt = Campos.elements();
while (camposIt.hasMoreElements()){
comp = (Component)camposIt.nextElement();
Ancho = maxwidth - maxwidth_Label;
Alto = comp.getPreferredSize().height;
PosY += _V_Space;
comp.setBounds(_H_Space*2 + maxwidth_Label + Bordes.left, PosY, Ancho, Alto);
PosY += Alto;
}
}
public Dimension minimumLayoutSize(Container parent) {
Insets Bordes = parent.getInsets();
Component comp;
Component compCampo;
int Ancho = 0;
int Alto = 0;
int MaxAncho = 0;
int i = 0;
Enumeration labelIt = Labels.elements();
while (labelIt.hasMoreElements()) {
comp = (Component)labelIt.nextElement();
compCampo = (Component)Campos.get(i);
Ancho = comp.getPreferredSize().width + _H_Space*3 + compCampo.getPreferredSize().width;
MaxAncho = Math.max(Ancho, MaxAncho);
i++;
}
Enumeration camposIt = Campos.elements();
while (camposIt.hasMoreElements()){
comp = (Component)camposIt.nextElement();
Alto += comp.getPreferredSize().height + _V_Space ;
}
Dimension d = new Dimension(MaxAncho + Bordes.left + Bordes.right, Alto + _V_Space + Bordes.top + Bordes.bottom );
return d;
}
public Dimension preferredLayoutSize(Container parent) {
Dimension d = minimumLayoutSize(parent);
d.width = d.width*2;
return d;
}
public void removeLayoutComponent(Component comp) {
}
public void addLayoutComponent(String name, Component comp) {
if (name.equals("label")) {
Labels.addElement(comp);
}
else {
Campos.addElement(comp);
}
}
}
FRAME
public class NewClass extends JFrame{
private final String LF_WINDOWS = "com.sun.java.swing.plaf.windows.WindowsLookAndFee l";
private JLabel lblCodigo;
private JLabel lblDesc;
private JLabel lblSexo;
private JLabel lblLista;
private JTextField txtCodigo;
private JTextField txtDesc;
private JComboBox cboSexo;
private JList lstLista;
/** Creates a new instance of NewClass */
public NewClass() {
super("Prueba");
setDefaultCloseOperation(WindowConstants.EXIT_ON_C LOSE);
SetCampos();
CambiarAspecto();
pack();
setVisible(true);
}
private void SetCampos(){
Vector items = new Vector();
items.add("Masculino");
items.add("Femenino");
Vector items_lista = new Vector();
items_lista.add("Dato 1");
items_lista.add("Dato 2");
items_lista.add("Dato 3");
lblCodigo = new JLabel("Código:");
lblDesc = new JLabel("Descripción:");
lblSexo = new JLabel("Sexo:");
lblLista = new JLabel("Lista de datos:");
txtCodigo = new JTextField("");
txtDesc = new JTextField();
cboSexo = new JComboBox(items);
lstLista = new JList(items_lista);
lstLista.setBorder(BorderFactory.createEtchedBorde r());
JPanel _panel = new JPanel(true);
_panel.setBorder(BorderFactory.createTitledBorder( "Datos"));
_panel.setLayout(new ColumnLayout());
_panel.add( "label", lblCodigo);
_panel.add("", txtCodigo);
_panel.add("label", lblDesc);
_panel.add("", txtDesc);
_panel.add("label", lblSexo);
_panel.add("", cboSexo);
_panel.add("label", lblLista);
_panel.add("", lstLista);
getContentPane().setLayout(new BorderLayout());
getContentPane().add(_panel, BorderLayout.CENTER);
}
public static void main(String args[]){
new NewClass();
}
private void CambiarAspecto(){
try {
UIManager.setLookAndFeel(LF_WINDOWS);
SwingUtilities.updateComponentTreeUI(this);
this.pack();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}