Urgente -- GridBagLayout

Tenho 2 JList e 4 botões em panel.
Quando clico em um botão para que um dado de uma JList passe para outra, o Layout fica redesenhando.

Como posso fazer isso parar???
Quero estabelecer sempre a mesma visualização.

Alguém pode me ajudar???

Para isso parar, tem algum método específico ou tem alguma var a definir???

Poste seu código aqui para entendermos melhor…
E para que vc possa entender melhor o GridBagLayout sugiro dar uma olhada neste tutorial.

A Paz!!

[quote=paulohbmetal]Poste seu código aqui para entendermos melhor…
E para que vc possa entender melhor o GridBagLayout sugiro dar uma olhada neste tutorial.

A Paz!![/quote]

Não postei os atributos nem o construtor… Basicamente é isso aí:


	private javax.swing.JPanel getJContentPane() {
		if(jContentPane == null) {
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			jLabel1 = new JLabel();
			jLabel = new JLabel();
			jContentPane = new javax.swing.JPanel();
			jContentPane.setLayout(new GridBagLayout());
			jLabel.setText("Serviços");
			jLabel1.setText("Serviços Permitidos");

			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.gridy = 2;
			gridBagConstraints1.gridheight = 4;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.weighty = 1.0;
			gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
			gridBagConstraints1.ipadx = 219;
			gridBagConstraints1.ipady = 159;
			gridBagConstraints1.insets = new java.awt.Insets(2,7,2,3);
			gridBagConstraints2.gridx = 2;
			gridBagConstraints2.gridy = 2;
			gridBagConstraints2.gridheight = 4;
			gridBagConstraints2.weightx = 1.0;
			gridBagConstraints2.weighty = 1.0;
			gridBagConstraints2.fill = java.awt.GridBagConstraints.BOTH;
			gridBagConstraints2.ipadx = 219;
			gridBagConstraints2.ipady = 159;
			gridBagConstraints2.insets = new java.awt.Insets(0,4,4,10);
			gridBagConstraints3.gridx = 0;
			gridBagConstraints3.gridy = 1;
			gridBagConstraints3.ipadx = 15;
			gridBagConstraints3.insets = new java.awt.Insets(3,7,0,157);
			gridBagConstraints4.gridx = 2;
			gridBagConstraints4.gridy = 1;
			gridBagConstraints4.ipadx = 13;
			gridBagConstraints4.ipady = -3;
			gridBagConstraints4.insets = new java.awt.Insets(4,4,2,101);
			gridBagConstraints5.gridx = 0;
			gridBagConstraints5.gridy = 0;
			gridBagConstraints5.gridwidth = 3;
			gridBagConstraints5.ipadx = 507;
			gridBagConstraints5.ipady = 68;
			gridBagConstraints5.insets = new java.awt.Insets(8,7,3,11);
			gridBagConstraints6.gridx = 1;
			gridBagConstraints6.gridy = 2;
			gridBagConstraints6.ipadx = 9;
			gridBagConstraints6.ipady = -7;
			gridBagConstraints6.insets = new java.awt.Insets(34,3,3,4);
			gridBagConstraints6.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints7.gridx = 1;
			gridBagConstraints7.gridy = 3;
			gridBagConstraints7.ipadx = 9;
			gridBagConstraints7.ipady = -7;
			gridBagConstraints7.insets = new java.awt.Insets(4,3,4,4);
			gridBagConstraints7.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints8.gridx = 1;
			gridBagConstraints8.gridy = 4;
			gridBagConstraints8.ipadx = 16;
			gridBagConstraints8.ipady = -7;
			gridBagConstraints8.insets = new java.awt.Insets(4,3,3,4);
			gridBagConstraints9.gridx = 1;
			gridBagConstraints9.gridy = 5;
			gridBagConstraints9.ipadx = 16;
			gridBagConstraints9.ipady = -7;
			gridBagConstraints9.insets = new java.awt.Insets(3,3,32,4);
			gridBagConstraints10.gridx = 0;
			gridBagConstraints10.gridy = 6;
			gridBagConstraints10.ipadx = 3;
			gridBagConstraints10.ipady = -7;
			gridBagConstraints10.insets = new java.awt.Insets(3,7,7,147);

			jContentPane.add(getJPanel(), gridBagConstraints5);
			jContentPane.add(getJButton1(), gridBagConstraints6);
			jContentPane.add(getJButton2(), gridBagConstraints7);
			jContentPane.add(getJList(), gridBagConstraints1);
			jContentPane.add(getJList1(), gridBagConstraints2);
			jContentPane.add(jLabel, gridBagConstraints3);
			jContentPane.add(jLabel1, gridBagConstraints4);
			jContentPane.add(getJButton3(), gridBagConstraints8);
			jContentPane.add(getJButton4(), gridBagConstraints9);
			jContentPane.add(getGravar(), gridBagConstraints10);
		}
		return jContentPane;
		
	}
   
	private JList getJList() {
		Conexao conexao = new Conexao();	
		Connection con = null;
		
		try{
			con = conexao.retornaConexao();
		}
		catch(Exception e){
			msg = e.getMessage();
		}
		
		try{
			if(con != null){
				
				listajList = new DefaultListModel();
				jList = new JList(listajList);
				
				List item = new ArrayList();
				ResultSet rs;
				Statement stmt = con.createStatement();
				String sql = "SELECT * from TB_SERVICO";

				rs = stmt.executeQuery(sql);
				
				while(rs.next()){
					item.add(rs.getString("DS_SERVICO"));
				}
				
				Iterator it = item.iterator();
				
				List[] items = new ArrayList[]{};
					while(it.hasNext()){
						listajList.addElement(it.next());
						for(int j=0; j<=items.length; j++){
							listajList.addElement(it.next());
						}
					}
			}
		}
		catch (Exception e){
			try {
				con.close();
			} catch (SQLException e1) {	
				msg = "ERRO " + e.getMessage(); 
			}
		}
		return jList;
	}

	private JList getJList1() {
		if (jList1 == null) {
			listajList1 = new DefaultListModel();
			jList1 = new JList(listajList1);
		}
		return jList1;
	}
  
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jLabel3 = new JLabel();
			jPanel.setLayout(null);
			jPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
			jLabel3.setText("Selecionar Perfil");
			jLabel3.setBounds(11, 9, 114, 16);
			jPanel.add(jLabel3, null);
			jPanel.add(getJComboBox(), null);
		}
		return jPanel;
	}
  
	private JComboBox getJComboBox() {
		if (jComboBox == null) {
			jComboBox = new JComboBox();
			jComboBox.setBounds(11, 26, 355, 22);

			Conexao conexao = new Conexao();	
			Connection con = null;
			
			try{
				con = conexao.retornaConexao();
			}catch(Exception e){
				msg = e.getMessage();
			}
			
			try{
				if(con != null){
					ResultSet rs;
					Statement stmt = con.createStatement();
					String sql = "SELECT * from TB_PERFIL";
					rs = stmt.executeQuery(sql);
					
					while(rs.next()){
						jComboBox.addItem(rs.getString("DS_PERFIL"));
					}	
				}
			}catch (Exception e){
				try {
					con.close();
				} catch (SQLException e1) {}
				msg = "ERRO " + e.getMessage(); 
			}
		}
		return jComboBox;
	}
   
	private JButton getJButton1() {
		if (jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText(">");
			jButton1.setPreferredSize(new java.awt.Dimension(48,26));
			jButton1.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {    
					
					int pos = jList1.getModel().getSize();
					listajList1.add(pos,jList.getSelectedValue());
					
					//remove do jList (Serviços)
					listajList.remove(jList.getSelectedIndex());

					
				}
			});
			
		}
		return jButton1;
	}

	private JButton getJButton2() {
		if (jButton2 == null) {
			jButton2 = new JButton();
			jButton2.setText("<");
			jButton2.setPreferredSize(new java.awt.Dimension(48,26));
			jButton2.addActionListener(new java.awt.event.ActionListener() { 
				public void actionPerformed(java.awt.event.ActionEvent e) {    
					
					int pos = jList.getModel().getSize();
					listajList.add(pos,jList1.getSelectedValue());
					
					//remove do jList (Serviços)
					listajList1.remove(jList1.getSelectedIndex());
				}
			});
		}
		return jButton2;
	}
 
	private JButton getJButton3() {
		if (jButton3 == null) {
			jButton3 = new JButton();
			jButton3.setText(">>");
		}
		return jButton3;
	}
  
	private JButton getJButton4() {
		if (jButton4 == null) {
			jButton4 = new JButton();
			jButton4.setText("<<");
		}
		return jButton4;
	}
  
	private JButton getGravar() {
		if (Gravar == null) {
			Gravar = new JButton();
			Gravar.setText("Gravar");
		}
		return Gravar;
	}

Estava observando, nas constraints dos JList’s(JList e JList1) vc está definindo os pesos weightx e weighty como 1 para ambos os componentes.

[code]gridBagConstraints1.gridx = 0;
gridBagConstraints1.gridy = 2;
gridBagConstraints1.gridheight = 4;
gridBagConstraints1.weightx = 1.0;
gridBagConstraints1.weighty = 1.0;
gridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints1.ipadx = 219;
gridBagConstraints1.ipady = 159;
gridBagConstraints1.insets = new java.awt.Insets(2,7,2,3);

gridBagConstraints2.gridx = 2;
gridBagConstraints2.gridy = 2;
gridBagConstraints2.gridheight = 4;
gridBagConstraints2.weightx = 1.0;
gridBagConstraints2.weighty = 1.0;
gridBagConstraints2.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints2.ipadx = 219;
gridBagConstraints2.ipady = 159;
gridBagConstraints2.insets = new java.awt.Insets(0,4,4,10);[/code]

Quando vc coloca isso vc está falando que quando o container(JPanel) redimencionar, seus JList’s irão ter, vamos dizer assim, 100% de liberdade para redimencionar junto com o container, ou seja, eles irão tomar todo o espaço possível quando precisarem redimencionar. Então deve ser por isso que está dando problema. Procure calcular quanto cada componente deve ter de weightx(horizontal) e weighty(vertical), mude estes valores e então teste.

A Paz!!

Paulo, creio que é por aí só q está faltando mais alguma coisa.
Faça um teste com este código

import javax.swing.*;
import java.awt.*;

public class GridBagLayouTest extends JFrame
{
    public GridBagLayouTest()
    {
        GridBagLayout layout = new GridBagLayout();        
        GridBagConstraints c = new GridBagConstraints();
        
        getContentPane().setLayout(layout);
        
        JTextArea txta = new JTextArea(7,7);

        //TextArea
        c.gridy = 2;		//posição
        c.gridx = 1;		//posição
        c.gridwidth = 1;	//tamanho por cel
        c.gridheight = 1;	//tamanho por cel
        c.weightx = 7;		//
        c.weighty = 7;
//        c.ipadx = 10;
//        c.ipady = 10;
        layout.setConstraints(txta, c);
        getContentPane().add(txta);
        
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }
    
    public static void main(String args[])
    {
        new GridBagLayouTest();
    }
}

ao rodar o código do post acima faça um teste digitando em uma só linha e também dando enter várias vezes…
vc verá que o componente fica redimensionando…

acontece algo parecido… só que com os 2 JLists e os 4 botões andando na tela

Vc entendeu a função do weightx e do weighty?

Pq vc colocou 7 para os dois e o valor máximo é 1.
Ou seja 1 equivale a 100%.

Neste caso ele vai tomar a tela inteira pois só existe ele no JFrame.
Experimente colocar o seu JTextArea num JScrollPane e colocar as Constraints no JScrollPane, verá que além de ser melhor, vai resolver o seu problema.

Segue o exemplo:

[code]
import javax.swing.;
import java.awt.
;

public class GridBagLayouTest extends JFrame
{
public GridBagLayouTest()
{
GridBagLayout layout = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();

    getContentPane().setLayout(layout);
     
    JScrollPane scroll = new JScrollPane();
    JTextArea txta = new JTextArea(7,7);
    scroll.setViewportView(txta);
     
     //TextArea
     c.gridy = 2;	        //posição
     c.gridx = 1;	        //posição
     c.gridwidth = 1;     //tamanho por cel
     c.gridheight = 1;    //tamanho por cel
     c.weightx = .5;      // 50%
     c.weighty = .5;      // 50%
     c.fill = GridBagConstraints.VERTICAL;
     
     setLayout(new GridBagLayout());
     add(scroll, c);
     add(new JButton("Teste"));
     
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     pack();
     setVisible(true);
 }
 
 public static void main(String args[])
 {
     new GridBagLayouTest();
 }

}[/code]

A Paz!!

Cara mesmo com outros componentes(buttons) o text-area ficara redimensionando…
Asim como fez com o text-area sozinho, coloque outros componentes …
Muito Obrigado…

Vc rodou o exemplo acima?!

A Paz!!

Paulo, muito obrigado pela sua atenção…
Mas cara continua dando o msm erro.

Paulo, muito obrigado pela sua atenção…
Mas cara continua dando o msm erro.

Cara, coloque o seu JList dentro de um JScrollPane…

Vc não vai sair daqui sem resolver seu problema, não desanime… :slight_smile:

A Paz!!

Valeu Paulo… muito obrigado pela grande e imensa ajuda…
Basta adicionar os JList numa simples ScrollPane