Alterar o tamanho do JTabel

1 resposta
rodrigo.turini

Pessoal, a minha dificuldade está em alterar o tamanho do JTabel.
Obs: Apenas reforçando, não é o tamanho da largura das colunas/ linhas e sim da Tabela inteira.
Ja tentei setBounds(0, 0, 340, 200), também setSize(340, 200)... mas o tamanho continua o mesmo.

Segue abaixo a classe inteira p/ que possam dar uma olhada...

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.*;
public class relatorioClientes extends JFrame {

	private static final long serialVersionUID = 1L;
	private Connection con;
	private Statement st;
	private JTable tabela;

	public relatorioClientes() throws SQLException
	{
		super("Tabela de clientes existentes no banco");
		
		Container janela = getContentPane();
		janela.setLayout(new FlowLayout());
		tabela = new JTable();
	
		tabela.setModel(new DefaultTableModel( 	new Object [][]{ }, 
		new String [] { "Cod.Cliente", "Nome" ,"CPF", "Email", "Rua", "Bairro", "Cidade", "Estado", "Telefone", "Celular"}));
				
				//define largura das colunas -- permite ou não, ajustar a largura 
				tabela.getColumnModel().getColumn(0).setPreferredWidth(10);	    
		        tabela.getColumnModel().getColumn(0).setResizable(false); 
		        
				tabela.getColumnModel().getColumn(1).setPreferredWidth(60);
				tabela.getColumnModel().getColumn(1).setResizable(true);
				
				tabela.getColumnModel().getColumn(2).setPreferredWidth(10);
				tabela.getColumnModel().getColumn(2).setResizable(true);
				
				tabela.getColumnModel().getColumn(3).setPreferredWidth(10);
				tabela.getColumnModel().getColumn(3).setResizable(true);
				
				tabela.getColumnModel().getColumn(4).setPreferredWidth(10);
				tabela.getColumnModel().getColumn(4).setResizable(true);
				
				tabela.getColumnModel().getColumn(5).setPreferredWidth(10);
				tabela.getColumnModel().getColumn(5).setResizable(true);
				
				tabela.getColumnModel().getColumn(6).setPreferredWidth(10);
				tabela.getColumnModel().getColumn(6).setResizable(true);
				
				tabela.getColumnModel().getColumn(7).setPreferredWidth(10);
				tabela.getColumnModel().getColumn(7).setResizable(true);
				
				tabela.getColumnModel().getColumn(8).setPreferredWidth(10);
				tabela.getColumnModel().getColumn(8).setResizable(true);
				
				tabela.getColumnModel().getColumn(9).setPreferredWidth(10);
				tabela.getColumnModel().getColumn(9).setResizable(true);
		
		JScrollPane rolagemTabela = new JScrollPane(tabela);
		janela.add(rolagemTabela,"Center");
		
		Scrollbar sb = new Scrollbar(Scrollbar.VERTICAL);
		janela.add(sb);
		consultaBanco();
			
		setVisible(true);
		
		janela.setBounds(0, 0, 34000, 20000);
		setSize(470, 200);
		setLocation(600,300);
		

	}
	
	
	public void consultaBanco(){
		String url = "jdbc:postgresql://localhost:5432/postgres"; 
		String usuario = "postgres";
		String psw = "root";
	
		
			try {
				Class.forName("org.postgresql.Driver");
			} catch (ClassNotFoundException e) {
				// TODO Bloco catch gerado automaticamente
				e.printStackTrace();
			}
			try {
				con = DriverManager.getConnection(url, usuario, psw);
			} catch (SQLException e) {
				// TODO Bloco catch gerado automaticamente
				e.printStackTrace();
			} 
		

			try{
				st = con.createStatement();
				
				//processa resultados da consulta
				ResultSet resultSet = st.executeQuery("SELECT codcliente,nomecliente,cpf,email,rua,bairro,cidade,estado,telefone,celular FROM cadastroclientes");
				DefaultTableModel dtm = (DefaultTableModel)tabela.getModel();

				while (resultSet.next()) {
					int coluna1 = resultSet.getInt("codcliente");
					String coluna2 = resultSet.getString("nomecliente");
					String coluna3 = resultSet.getString("cpf");
					String coluna4 = resultSet.getString("email");
					String coluna5 = resultSet.getString("rua");
					String coluna6 = resultSet.getString("bairro");
					String coluna7 = resultSet.getString("cidade");
					String coluna8 = resultSet.getString("estado");
					String coluna9 = resultSet.getString("telefone");
					String coluna10 = resultSet.getString("celular");
					
					dtm.addRow(new Object[]{ coluna1, coluna2, coluna3, coluna4, coluna5, coluna6, coluna7, coluna8, coluna9, coluna10});

		        }

		}catch(SQLException eSQL){
			JOptionPane.showMessageDialog(this,	"Erro na expressão do SELECT!\nMensagem: " + eSQL.getMessage(),	"Erro", JOptionPane.ERROR_MESSAGE);
			System.exit(1);
		}
	
		try {
			st.close();
			con.close();
		}catch(Exception exception){
			exception.printStackTrace();
			System.exit(2);
		}
		
	}


	public static void main(String[] args) throws SQLException {
		relatorioClientes relat_clientes = new relatorioClientes();
		relat_clientes.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

}

1 Resposta

ViniGodoy

Não use setBounds. Aprenda a trabalhar com gerenciadores de layout:
http://download.oracle.com/javase/tutorial/uiswing/layout/using.html
http://download.oracle.com/javase/tutorial/uiswing/layout/visual.html
http://www.miglayout.com/
http://www.javalobby.org/articles/miglayout/

Criado 14 de novembro de 2010
Ultima resposta 14 de nov. de 2010
Respostas 1
Participantes 2