Problemas com JTabbedPane

11 respostas
renatotn7

olá,

implementei um código com essa classe criando duas abas, um para cada painel, porém quando eu maximiso ou restauro ou por qualquer outro motivo aparece ao lado das 2 abas outras partes do propio painel,
ísso acontece até mesmo quando digito algo num jComboBox que tem dentro desse painel. como eu resolvo isso?

obs não funcionou sobrescrever o método: paintComponent(Graphics g)…

obrigado

11 Respostas

von.juliano

Isso acontece por causa do layout que vc está usando no painel (vc deve estar usando o layout nullo) no qual vc coloca o JTabbedPane. Tente colocar o layout desse painel como GridLayout e vê se funciona como vc quer.

renatotn7

von.juliano não funcionou…

von.juliano

O q vc tá usando? Netbeans ou eclipse?

renatotn7

eclipse

von.juliano

Esse painel só contem o JTabbedPane? Pq se for assim, vc pode setar o layout do painel para gridlayou, 1 X 1, que ele vai colocar só o tab no painel ocupando todo o espaço, redimensionando ele junto, caso o painel tb seja.

renatotn7

só tem ele e curiosamente captura até mesmo cores de jpanels que não estão ligados a ele

von.juliano

Cara, põe o código aí.

renatotn7

//
/
TabbedComponente /
/
/
/
/
import java.awt.Color;
/**

  • Summary description for TabbedComponente
*/

public class TabbedComponente extends JPanel

{

// Variables declaration

private final Color corDefault = CoresDefault.getCorPaineis();

private FrameSil framePai;

private JTabbedPane jTabbedPane2;

private JPanel contentPane;

//-----

private JPanel jPanel4;

//-----

private JPanel jPanel5;
Dimension d,e = new Dimension();
//-----
// End of variables declaration

public void paintComponent(Graphics g) 
{ 
	d = framePai.getSize();
	
	if ( e.width != d.width  || e.height != d.height) 
	{
		
		
		
		this.remove(contentPane);
	
		dimensionando();
		
	}
	
}



public TabbedComponente(FrameSil

framePai)
{

super();
			
	initializeComponent(framePai, wa);
	//
	// TODO: Add any constructor code after initializeComponent call
	//

	this.setVisible(true);
}


	private void initializeComponent(FrameSil framePai)
{
	this.setBackground(corDefault);
	this.framePai=framePai;
	jTabbedPane2 = new JTabbedPane();
	jTabbedPane2.setBackground(corDefault);
	
	
	 JTabbedPane b= new JTabbedPane();

	
	 contentPane =this;		contentPane.setBackground(corDefault);
	//-----
	jPanel4 = new JPanel();
	jPanel4.setBackground(corDefault);
	jPanel4.add(new PainelAvaliacaoWeb(framePai));
	
	//-----
	jPanel5 = new JPanel();
	jPanel5.setBackground(corDefault);
	jPanel5.add(new PainelAvaliacaoLocal(framePai));
	//-----

	//
	// jTabbedPane2
	//
	jTabbedPane2.addTab("Analise Web", jPanel4);
	jTabbedPane2.addTab("Analise Local", jPanel5);

	
	//
	// contentPane
	//
	contentPane.setLayout(new GridLayout());
	
	d = framePai.getSize();
	dimensionando();

	//add(contentPane);
	//
	// jPanel4
	//
	jPanel4.setLayout(new CardLayout());
	
	//
	// jPanel5
	//
	jPanel5.setLayout(new CardLayout());
	//
	// TabbedComponente
	//
//	this.setTitle("TabbedComponente - extends JFrame");
//	this.setLocation(new Point(0, 0));
//	this.setSize(new Dimension(390, 300));
}

/** Add Component Without a Layout Manager (Absolute Positioning) */
private void addComponent(Container container,Component c,int x,int y,int width,int height)
{
	c.setBounds(x,y,width,height);
	container.add(c);
}

//
// TODO: Add any appropriate code in the following Event Handling Methods
//
private void jTabbedPane2_stateChanged(ChangeEvent e)
{
	
	System.out.println("\njTabbedPane2_stateChanged(ChangeEvent e) called.");
	// TODO: Add any handling code here

}

public void dimensionando(){
	e=d;
	addComponent(contentPane, jTabbedPane2, 0,0,d.width-8,d.height-317);
renatotn7

}
}

von.juliano

Olha só, eu peguei o seu código e adaptei, por causa das suas classes e etc. Tá aí, funcionando direitinho, vc só tem q adaptar da forma q vc precisa:


//
/
TabbedComponente /
/
/
/
/
import java.awt.*;

import javax.swing.*;
import javax.swing.event.ChangeEvent;

public class TabbedComponent extends JPanel {

// Variables declaration

private final Color corDefault = new Color(0, 0, 0).blue;
private JFrame framePai;

private JTabbedPane jTabbedPane2;

private JPanel contentPane;

private JPanel jPanel4;

private JPanel jPanel5;

Dimension d, e = new Dimension();

public void paintComponent(Graphics g) {
	d = framePai.getSize();

	if (e.width != d.width || e.height != d.height) {
		this.remove(contentPane);
		dimensionando();
	}
}

public TabbedComponent(JFrame framePai) {
	super();
	initializeComponent(framePai);

	this.setVisible(true);
}

private void initializeComponent(JFrame framePai) {
	this.setBackground(corDefault);
	this.framePai = framePai;
	jTabbedPane2 = new JTabbedPane();
	jTabbedPane2.setBackground(corDefault);

	JTabbedPane b = new JTabbedPane();

	contentPane = this;
	contentPane.setBackground(corDefault);
	// -----
	jPanel4 = new JPanel();
	jPanel4.setBackground(corDefault);
	jPanel4.add(new JPanel());

	jPanel5 = new JPanel();
	jPanel5.setBackground(corDefault);
	jPanel5.add(new JPanel());

	jTabbedPane2.addTab("Analise Web", jPanel4);
	jTabbedPane2.addTab("Analise Local", jPanel5);


	contentPane.setLayout(new GridLayout());

	d = framePai.getSize();
	dimensionando();


	jPanel4.setLayout(new CardLayout());

	jPanel5.setLayout(new CardLayout());
	//

}

/** Add Component Without a Layout Manager (Absolute Positioning) */
private void addComponent(Container container, Component c, int x, int y,
		int width, int height) {
	c.setBounds(x, y, width, height);
	container.add(c);
}

private void jTabbedPane2_stateChanged(ChangeEvent e) {

	System.out
			.println("\njTabbedPane2_stateChanged(ChangeEvent e) called.");
	// TODO: Add any handling code here

}

 public void dimensionando(){
	 e=d;
	 addComponent(contentPane, jTabbedPane2, 0,0,d.width-8,d.height-317); 
 }
 
 // Só para teste
 public static void main(String[] args) {
	 JFrame f = new JFrame();
	 TabbedComponent t = new TabbedComponent(new JFrame());
	 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	 f.setLayout(new GridLayout(1,1));
	 f.setSize(800, 600);
	 f.add(t);
	 f.setVisible(true);
	System.out.println("Teste");
}

}

Blz! :thumbup:

renatotn7

ainda não deu certo… o problema deve estar em outra parte do código…
vou analisar mais… valeu pela ajuda!!!

Criado 24 de janeiro de 2007
Ultima resposta 24 de jan. de 2007
Respostas 11
Participantes 2