Layout Jtable

16 respostas
M

Galera, estou querendo que minha Jtable independente do numero de linhas e colunas sempre ocupe todo o espaço do meu panel
ja tentei usando BoxLayout, BorderLayout como está ai embaixo e também setando o preferredsize da tabela usando getsize do panel e também não deu certo
a largura da table sempre fica no tamanho certinho mas a altura eu não consigo controlar, ela muda de acordo com o número de linhas q a tabela tem.
essa ai foi minha última tentativa:

JPanel panel = new JPanel();
this.setSize(900, 900);
MyImagePanel imagepanel = new MyImagePanel(loadImage(path));
imagepanel.setLayout(new BorderLayout());
imagepanel.setMinimumSize(new Dimension(750,750));
imagepanel.setPreferredSize(new Dimension(750,750));
imagepanel.setMaximumSize(new Dimension(750,750));
imagepanel.add(table, BorderLayout.CENTER);
panel.setLayout(new BorderLayout());
panel.add(imagepanel, BorderLayout.WEST);
this.getContentPane().add(panel);
this.setResizable(false);
this.pack();
this.setVisible(true);

alguém tem alguma solução ?

16 Respostas

mauricioadl

coloque ela como borderlayout e o seu panel se estiver adicionado a um jframe tambem coloque como border layout;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;

public class MyPanel extends JFrame {
	private JTable jcomp1;
	private JPanel meuPanel;

	public MyPanel() {
		jcomp1 = new JTable(new Object[][] { { "A", "B", "C" },
				{ "A", "B", "C" }, { "A", "B", "C" } }, new Object[] {
				"coluna 1", "coluna 1", "coluna 1" });
		meuPanel = new JPanel();
		meuPanel.setLayout(new BorderLayout());
		meuPanel.add(jcomp1);

		setLayout(new BorderLayout());
		setSize(800, 600);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		add(meuPanel);
	}

	public static void main(String[] args) {
		new MyPanel().setVisible(true);
	}
}
M
this.setSize(900, 900);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
MyImagePanel imagepanel = new MyImagePanel(loadImage(path));
imagepanel.setLayout(new BorderLayout());
table.setLayout(new BorderLayout());		

Dimension imagepanelsize = new Dimension(800,800);
imagepanel.setMinimumSize(imagepanelsize);
imagepanel.setPreferredSize(imagepanelsize);
imagepanel.setMaximumSize(imagepanelsize);
imagepanel.add(table, BorderLayout.CENTER);

panel.add(imagepanel, BorderLayout.WEST);
this.getContentPane().add(panel);
this.setResizable(false);
this.pack();
this.setVisible(true);

Está assim agora e a tabela continua com tamanho diferente do imagepanel

Luan_Kevin

E se vc tentar colocar ela em um JScrollPane?

mauricioadl

tenta remover essas linhas, elas sao desnecessarias

Dimension imagepanelsize = new Dimension(800,800); imagepanel.setMinimumSize(imagepanelsize); imagepanel.setPreferredSize(imagepanelsize); imagepanel.setMaximumSize(imagepanelsize);

M

o problema é que eu criei um imagepanel derivado do janel para por uma imagem de fundo e a tabela teria que vim por cima e usando transparência da pra ver a imagem de fundo,
por isso q a tabela tem q ser exatamente do tamanho do panel.
não sei se da pra fazer o que eu fiz com o jpanel no jscrollpane (imagem no fundo)

M

mauricioadl:
tenta remover essas linhas, elas sao desnecessarias

Dimension imagepanelsize = new Dimension(800,800); imagepanel.setMinimumSize(imagepanelsize); imagepanel.setPreferredSize(imagepanelsize); imagepanel.setMaximumSize(imagepanelsize);

tirando essas linhas o panel não aparece, ele fica sem dimensão nenhuma e,
também eu queria o panel fosse nesse tamanho q está ai msm e que a tabela ficasse com tamanho igual.

obs.: Meu Panel não ocupará a tela inteira, outro panel será colocado ao lado, por isso eu definir o tamanho dele ai.

mauricioadl

matheushac:
mauricioadl:
tenta remover essas linhas, elas sao desnecessarias

Dimension imagepanelsize = new Dimension(800,800); imagepanel.setMinimumSize(imagepanelsize); imagepanel.setPreferredSize(imagepanelsize); imagepanel.setMaximumSize(imagepanelsize);

tirando essas linhas o panel não aparece, ele fica sem dimensão nenhuma e,
também eu queria o panel fosse nesse tamanho q está ai msm e que a tabela ficasse com tamanho igual.

obs.: Meu Panel não ocupará a tela inteira, outro panel será colocado ao lado, por isso eu definir o tamanho dele ai.

entendi. coloque o codigo do frame todo pra mim ver.

mauricioadl

outra coisa. se o o tamamanho do panel eh fixo vc nem precisa colocar a jtable dentro dela, basta vc adicionar ela diretamente no jframe

M

mas se eu adicionar a table direto no frame ela não terá a mesma medida do panel, ou terá?

M
mauricioadl:
matheushac:
mauricioadl:
tenta remover essas linhas, elas sao desnecessarias
Dimension imagepanelsize = new Dimension(800,800);
imagepanel.setMinimumSize(imagepanelsize);
imagepanel.setPreferredSize(imagepanelsize);
imagepanel.setMaximumSize(imagepanelsize);

tirando essas linhas o panel não aparece, ele fica sem dimensão nenhuma e,
também eu queria o panel fosse nesse tamanho q está ai msm e que a tabela ficasse com tamanho igual.

obs.: Meu Panel não ocupará a tela inteira, outro panel será colocado ao lado, por isso eu definir o tamanho dele ai.

entendi. coloque o codigo do frame todo pra mim ver.

public class Squares extends JFrame {

	private static final long serialVersionUID = 1L;

	private MyTableModel model; 
	private MyTable table; 	
	
	private static BufferedImage loadImage(String file) throws IOException {
		return ImageIO.read(new File(file));
	}
	 
	
	
	private void ResizeColumns(){
		int columns = table.getColumnCount();
		for(int i=0; i<columns; i++){
			table.getColumnModel().getColumn(i).setMinWidth(1);
		}
	}

	public Squares(String[][] data,Double min, Double max, Float[][] colormap, String path) throws Throwable{
		super("Quadrículas");
		BuildMatrix aux = new BuildMatrix();
		Object dados[][] = null;
		dados = aux.BuildM(data.length, data[0].length);
		model = new MyTableModel(dados);
		table = new MyTable(model, data){		
			Action action = new AbstractAction() {
				public void actionPerformed(ActionEvent e){
			        TableCellListener tcl = (TableCellListener)e.getSource();
			        TableCellRenderer tcr = table.getDefaultRenderer(getClass());
			        if(tcr instanceof CustomTableCellRenderer){
			        	double min = ((CustomTableCellRenderer) tcr).GetMin();
			        	double max = ((CustomTableCellRenderer) tcr).GetMax();
			        	String newval = (String)tcl.getNewValue() ;
			        	Double newvalue = Double.parseDouble(newval);
			        	if (newvalue >< min){
			        		((CustomTableCellRenderer) tcr).SetMin(newvalue);
			        		table.repaint();
			        	}
			        	if (newvalue > max){
			        		((CustomTableCellRenderer) tcr).SetMax(newvalue);
			        		table.repaint();
			        	}
			        }
			        
			        table.setData(tcl.getRow(), tcl.getColumn(), tcl.getNewValue());
				    table.setValueAt("", tcl.getRow(), tcl.getColumn());
				}
			};
			TableCellListener tcl = new TableCellListener(this, action);
		};
		
		ResizeColumns();
		
		this.setSize(900, 900);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		MyImagePanel imagepanel = new MyImagePanel(loadImage(path));
		imagepanel.setLayout(new BorderLayout());
		table.setLayout(new BorderLayout());		
		
		Dimension imagepanelsize = new Dimension(800,800);
		imagepanel.setMinimumSize(imagepanelsize);
		imagepanel.setPreferredSize(imagepanelsize);
		imagepanel.setMaximumSize(imagepanelsize);
		imagepanel.add(table, BorderLayout.CENTER);
		
		JPanel ColorBarPanel = new JPanel();
		Dimension ColorBarPanelSize = new Dimension(50,800);
		ColorBarPanel.setMinimumSize(ColorBarPanelSize);
		ColorBarPanel.setPreferredSize(ColorBarPanelSize);
		ColorBarPanel.setMaximumSize(ColorBarPanelSize);
		ColorBarPanel.setBackground(Color.red);
		
		
		panel.add(imagepanel, BorderLayout.WEST);
		panel.add(ColorBarPanel, BorderLayout.EAST);
		this.getContentPane().add(panel);
		this.setResizable(false);
		this.pack();
		
		this.setVisible(true);
		
		this.table.setDefaultRenderer(Object.class, new CustomTableCellRenderer(min, max, colormap));
	}	
		
	
	public static void main (String [] args) throws Throwable{
		
	}
}
mauricioadl

aparentemente seu codigo esta correto, nao vi nenhum erro. pode ser a sua classe MyImagePanel que tenha algum bug. vc mesmo a fez?

M

eu mesmo a fiz, mas a única coisa q fiz foi o paintcomponent para conseguir botar uma imagem de fundo no painel.

será que pode ser problema do sistema operacional ? estou usando linux (UBUNTU)

mauricioadl

eu mesmo a fiz, mas a única coisa q fiz foi o paintcomponent para conseguir botar uma imagem de fundo no painel.

será que pode ser problema do sistema operacional ? estou usando linux (UBUNTU)

faça um teste simples, troque ele por um panel normal e ve se resolve.

M

eu mesmo a fiz, mas a única coisa q fiz foi o paintcomponent para conseguir botar uma imagem de fundo no painel.

será que pode ser problema do sistema operacional ? estou usando linux (UBUNTU)

faça um teste simples, troque ele por um panel normal e ve se resolve.

Boa idéia, mais tarde vou testar. Não estou com o programa aki agora.

M

Não resolveu, troquei

por

e continuou do mesmo jeito. Vou trabalhar em outras partes aki e depois testo em Windows para ver se eh problema do SO

Vlw ai pelas tentativas

M

Meu paintcomponent da table está assim:

public void paintComponent(Graphics g) {  
	        Graphics2D g2d = (Graphics2D) g; 
	        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,  
	                alpha));  
	        super.paintComponent(g2d);  
	        
	    }

será que está faltando alguma coisa ???

Criado 16 de fevereiro de 2012
Ultima resposta 17 de fev. de 2012
Respostas 16
Participantes 3