pessoal o meu problema com gridbaglayout é pq o panel que contem os check box fica sempre centralizado.
preciso que ele fique sempre NORTHWEST. canto superior direito.
package br.com.topsoft.topBI.Interface;
import br.com.topsoft.topBI.Entidade.Relatorio;
import br.com.topsoft.topBI.Entidade.Tabela;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.TitledBorder;
/**
*
* @author Thiago
*/
public class FormAdicionaRestricao extends JDialog {
private Relatorio relatorio;
private JPanel panelprincipal;
public FormAdicionaRestricao(Relatorio relatorio) {
super();
this.relatorio = relatorio;
this.panelprincipal = new JPanel(new GridBagLayout());
this.setSize(this.getToolkit().getScreenSize());
JScrollPane s = new JScrollPane();
s.setViewportView(this.panelprincipal);
this.getContentPane().add(s);
this.setModal(true);
addPanelsTabela(this.relatorio.getTabelas());
this.setVisible(true);
}
private void addPanelsTabela(List<Tabela> tabelas) {
for (Tabela tabela : tabelas) {
JPanel panel = getPanel(tabela.getDescricao());
panel.setName(tabela.getDescricao());
GridBagConstraints grid = null;
for (int i = 1; i <= tabela.getColunas().size(); i++) {
if ((i % 6 == 0) || (i == 1)) {
grid = new GridBagConstraints();
grid.insets = new Insets(4, 4, 4, 4);
grid.anchor = GridBagConstraints.NORTHWEST;
grid.weightx = 1;
grid.gridwidth = 1;
}
JCheckBox check = new JCheckBox(tabela.getColunas().get(i - 1).getDescricao());
check.setSize(check.getPreferredSize());
if ((i % 6 == 0) && (i > 0)) {
grid.gridwidth = GridBagConstraints.REMAINDER;
panel.add(check, grid);
} else {
grid.gridwidth = 1;
panel.add(check, grid);
}
}
GridBagConstraints grids = new GridBagConstraints();
grids.anchor = GridBagConstraints.NORTH;
grids.gridwidth = GridBagConstraints.REMAINDER;
grids.fill = GridBagConstraints.BOTH;
grids.weightx = 1;
grids.insets = new Insets(4, 4, 4, 4);
this.panelprincipal.add(panel, grids);
panel.repaint();
}
}
private JPanel getPanel(String descricao) {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.black, 1),
BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), descricao,
TitledBorder.LEFT, TitledBorder.CENTER,
new Font("Dialog", Font.BOLD, 14), Color.BLACK)));
panel.setVisible(true);
return panel;
}
}