Olá pessoal!
A partir de um problema encontrei outro. Encontrei a necessidade de usar um gerenciador de layout mas estou apanhando pra dedéu e não estou conseguindo.
Usei esse tutorial para entender o que é um gerenciador de layout e para que serve: http://www.devmedia.com.br/articles/viewcomp.asp?comp=6609. De fato intendi, inclusive pudi ver que eu não precisaria fazer gambiarra no meu projeto se eu soubesse isso antes. Copiei a classe exemplo desse tutorial e fiz uns testes para testar os meus conhecimentos. O problema que encontro é o seguinte: Como alterar o layout dos componentes quando a janela for redimensionada?
Tentei fazer uma alteração no layout e reaplicar mas não deu certo, só para fins de testes coloquei isso no evento de um botão.
Este é o meu exemplo:
package VE;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
public class SimpleForm extends JFrame{
private JButton buttonOk;
private JLabel labelCPF;
private JLabel labelDescricao;
private JLabel labelEndereco;
private JLabel labelNome;
private JTextField txtCPF;
private JTextArea txtDescricao;
private JTextField txtEndereco;
private JTextField txtNome;
private JScrollPane jScrollPane;
public SimpleForm(){
this.setTitle("Formulário Simples");
//super.setTitle("Formulário Simples");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new FlowLayout(FlowLayout.LEFT));
//super.setLayout(new FlowLayout(FlowLayout.LEFT));
buttonOk = new JButton("Enviar dados >>>");
labelCPF = new JLabel("CPF:");
labelDescricao = new JLabel("Descrição:");
labelNome = new JLabel("Nome:");
labelEndereco = new JLabel("Endereço:");
txtCPF = new JTextField(20);
txtDescricao = new JTextArea(7, 50);
txtNome = new JTextField(30);
txtEndereco = new JTextField(30);
jScrollPane = new JScrollPane(txtDescricao);
getContentPane().add(getPanelForm());
//super.add(getPanelForm());
pack();
//super.pack();
}
public JPanel getPanelForm(){
FormLayout formLayout = new FormLayout(
"2dlu, pref, 2dlu, 200, 2dlu, pref, 2dlu, 100px, 2dlu",
"2dlu, top:pref, 2dlu, top:pref, 2dlu, top:pref, 2dlu, pref, 2dlu" );
final JPanel jPanel = new JPanel(formLayout);
jPanel.setBorder(BorderFactory.createTitledBorder("Preencha os dados"));
CellConstraints cellConstraints = new CellConstraints();
jPanel.add(labelNome, cellConstraints.xy(2, 2));
jPanel.add(labelCPF, cellConstraints.xy(6, 2));
jPanel.add(labelEndereco, cellConstraints.xy(2, 4));
jPanel.add(labelDescricao, cellConstraints.xy(2, 6));
jPanel.add(txtNome, cellConstraints.xy(4, 2));
jPanel.add(txtCPF, cellConstraints.xy(8, 2));
jPanel.add(txtEndereco, cellConstraints.xyw(4, 4, 5));
jPanel.add(jScrollPane, cellConstraints.xyw(4, 6, 5));
jPanel.add(buttonOk, cellConstraints.xy(4, 8));
buttonOk.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int v = 400;
FormLayout formLayout2 = new FormLayout(
"2dlu, pref, 2dlu, "+v+", 2dlu, pref, 2dlu, 100px, 2dlu",
"2dlu, top:pref, 2dlu, top:pref, 2dlu, top:pref, 2dlu, pref, 2dlu" );
System.out.println("Antes");
jPanel.setLayout(formLayout2);
System.out.println("Depois");
}
});
return jPanel;
}
public static void main(String[] args) {
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e){}
SimpleForm simpleForm = new SimpleForm();
simpleForm.setVisible(true);
}
}
Para testar isso é preciso do gerenciador de layoute. Coloque ele na pasta do jre/lib/ext (para que já sabe disso, ignore essa dica)