Dimension de panel nulle... :$

cs_olaaa Messages postés 48 Date d'inscription jeudi 20 janvier 2005 Statut Membre Dernière intervention 11 mai 2005 - 27 janv. 2005 à 00:13
dmaillet Messages postés 500 Date d'inscription mercredi 20 août 2003 Statut Membre Dernière intervention 11 juillet 2007 - 27 janv. 2005 à 16:07
Salut!
Alors j'explique mon petit pb:
Je crée un JTabbedPane, dans le quel j'ajoute un JPanel (en fait une classe qui hérite de JPanel) qui a pour layout un gridLayout de une ou deux colone seulement (en fonction d'un parametre dans le constructeur) et une seule ligne.
Dans ce JPanel, j'ajoute, en fonction du nb de colone, un (ou deux... ^^) autres panel dont le layout est null.
En fait j'aimerai ensuite ajouter des composants en fonction de la taille de ces panels, mais le probleme c que quand je cherche a récupérer les dimentions du panel il me renvoi toujours une dimension (0,0)... pareil pour les methodes getHeight() et getWidth().

Donc voila je comprend pas et pour l'insatnt mon seul moyen deplacer mes composant ds les panel c de metre des valeur numérique au pif pour leur coordonnée (je veu les metre au centre du panel en fait, c pourquoi j'ai besoin des dimension du panel...)

Voila merci d'avance de votre aide :)

9 réponses

dmaillet Messages postés 500 Date d'inscription mercredi 20 août 2003 Statut Membre Dernière intervention 11 juillet 2007
27 janv. 2005 à 09:17
Sauf si tu as fait un setPreferredSize et que tu essaies de faire un getPreferredSize

sinon, tant que tu n'as rien affiche (TabbedPane non visible), c'est normal que tu recuperes une dimension00

Pourquoi tu n'utilises pas un GridBagLayout???

-------------------
dams
-------------------
0
cs_olaaa Messages postés 48 Date d'inscription jeudi 20 janvier 2005 Statut Membre Dernière intervention 11 mai 2005
27 janv. 2005 à 12:55
Bah en fait mon tabPane il est visible... j'affiche avec un System.out.println la dimension du panel et c toujours a 0,0.
Et pour le setPreferedSize... jveu bien masi je fai encore metre une dimenstion au pif, et moi je voudrai que le panel il ai une taille au maximum, qu'il prenne tout l'espace du tabPane...

Pr le gridBadLayout... bah déja je sai pas m'en servir, et le peu que j'ai vu ca a l'air plutot compliqué..
De plus moi je veu juste afficher des boutons au milieu d'un panel c pr ca que jveu récupérer les dim du panel... ms aprés si on peu le faire avec un gridBagLayout pk pas ms je sai pas encore comment faire
0
dmaillet Messages postés 500 Date d'inscription mercredi 20 août 2003 Statut Membre Dernière intervention 11 juillet 2007
27 janv. 2005 à 13:20
Arf!

Si c'est juste pour les mettre au milieu, utilises un BorderLayout et mets ton panels avec tes boutons au centre

-------------------
dams
-------------------
0
cs_olaaa Messages postés 48 Date d'inscription jeudi 20 janvier 2005 Statut Membre Dernière intervention 11 mai 2005
27 janv. 2005 à 13:33
lol certes mais la en fait j'ai plusieur types de
boutons... en fait par ligne jai un bouton avec en face un
JTextField... donc jpense pas que ca le fasse avec un borderLayout...
Le
gridBagLayout
est le plus aproprié je pense, mais je préfère le faire avec un layout
null, comme ca je les place ou je veu (au centre) je met l'espce quez
je veu entre chaque bouton etc... bref je fai ce que je veu mais vu que
jarive pas a choper ces dimension je vai avoir du mal ^^
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
dmaillet Messages postés 500 Date d'inscription mercredi 20 août 2003 Statut Membre Dernière intervention 11 juillet 2007
27 janv. 2005 à 14:22
tiens utilises cette classe montee sur le grid

et tu fais



GriddedPanel panel = GriddedPanel(new Inset(5,5,5,5));

panel.addComponent(..., 0, 0);


panel.addComponent(..., 0, 1);


panel.addComponent(..., 1, 0);

.....





import java.awt.*;

import javax.swing.*;



/**

* A simple helper class that provides various easy to understand methods

* that lays out the components using the GridBagLayout.

*

* @author James Tan

*/

public class GriddedPanel

extends JPanel {

private GridBagConstraints constraints;



// define some default constraints values

private static final int C_HORZ = GridBagConstraints.HORIZONTAL;

private static final int C_NONE = GridBagConstraints.NONE;

private static final int C_WEST = GridBagConstraints.WEST;

private static final int C_WIDTH = 1;

private static final int C_HEIGHT = 1;



/**

* Creates a grid bag layout panel using a default insets constraints.

*/

public GriddedPanel() {

this(new Insets(2, 2, 2, 2));

}



/**

* Creates a grid bag layout panel using the specified insets

* constraints.

*/

public GriddedPanel(Insets insets) {

super(new GridBagLayout());

// creates the constraints object and set the desired

// default values

constraints = new GridBagConstraints();

constraints.anchor = GridBagConstraints.WEST;

constraints.insets = insets;

}



/**

* Adds the component to the specified row and col.

*/

public void addComponent(JComponent component, int row, int col) {

addComponent(component, row, col, C_WIDTH,

C_HEIGHT, C_WEST, C_NONE);

}



/**

* Adds the component to the specified row and col that spans across

* a specified number of columns and rows.

*/

public void addComponent(JComponent component, int row, int col,


int width, int height) {

addComponent(component, row, col, width, height, C_WEST, C_NONE);

}



/**

* Adds the component to the specified row and col that anchors at

* the specified position.

*/

public void addAnchoredComponent(JComponent component, int row,


int col, int anchor) {

addComponent(component, row, col, C_WIDTH, C_HEIGHT, anchor, C_NONE);

}



/**

* Adds the component to the specified row and col that spans across

* a specified number of columns and rows that anchors at the specified

* position.

*/

public void addAnchoredComponent(JComponent component, int row, int col,


int width, int height, int anchor) {

addComponent(component, row, col, width, height, anchor, C_NONE);

}



/**

* Adds the component to the specified row and col filling the column

* horizontally.

*/

public void addFilledComponent(JComponent component, int row, int col) {

addComponent(component, row, col, C_WIDTH, C_HEIGHT, C_WEST, C_HORZ);

}



/**

* Adds the component to the specified row and col with the specified

* filling direction.

*/

public void addFilledComponent(JComponent component, int row, int col,


int fill) {

addComponent(component, row, col, C_WIDTH, C_HEIGHT, C_WEST, fill);

}



/**

* Adds the component to the specified row and col that spans across

* a specified number of columns and rows with the specified filling

* direction.

*/

public void addFilledComponent(JComponent component, int row, int col,


int width, int height, int fill) {

addComponent(component, row, col, width, height, C_WEST, fill);

}



/**

* Adds the component to the specified row and col that spans across

* a specified number of columns and rows with the specified filling

* direction and an anchoring position.

*/

public void addComponent(JComponent component, int row, int col,


int width, int height, int anchor, int fill) {

// sets the constraints object

constraints.gridx = col;

constraints.gridy = row;

constraints.gridwidth = width;

constraints.gridheight = height;

constraints.anchor = anchor;

double weightx = 0.0;

double weighty = 0.0;



// only use the extra horizontal or vertical spaces if the component

// spans more than one column or/and row.

if (width > 1) {

weightx = 1.0;

}

if (height > 1) {

weighty = 1.0;

}



switch (fill) {

case GridBagConstraints.HORIZONTAL:

constraints.weightx = weightx;

constraints.weighty = 0.0;

break;

case GridBagConstraints.VERTICAL:

constraints.weighty = weighty;

constraints.weightx = 0.0;

break;

case GridBagConstraints.BOTH:

constraints.weightx = weightx;

constraints.weighty = weighty;

break;

case GridBagConstraints.NONE:

constraints.weightx = 0.0;

constraints.weighty = 0.0;

break;

default:

break;

}

constraints.fill = fill;

add(component, constraints);

}

}


-------------------
dams
-------------------
0
cs_olaaa Messages postés 48 Date d'inscription jeudi 20 janvier 2005 Statut Membre Dernière intervention 11 mai 2005
27 janv. 2005 à 14:51
Merci, je vai voir cke ca donne...

J'avai commencé a utiliser un GridBagLayout, mais j'avai un probleme:
les boutons n'avaioent pas la même taille... ct bien centré, enfin ils
sont bien placé mais jarive pas a leur donner a tous la même taille..
0
dmaillet Messages postés 500 Date d'inscription mercredi 20 août 2003 Statut Membre Dernière intervention 11 juillet 2007
27 janv. 2005 à 15:48
Fais un setSize sur tes boutons avant de les inclure au panel...

-------------------
dams
-------------------
0
cs_olaaa Messages postés 48 Date d'inscription jeudi 20 janvier 2005 Statut Membre Dernière intervention 11 mai 2005
27 janv. 2005 à 15:56
loool tu pense bien que j'ai essayé.. mais pas moyen ils ont chacun leur taille (surement en fonction de leur label...) donc voila le layout semble pas prendre en compte la taille..
voila comment jai ajouté mon bouton:

this.panBoutons[j].add(this.tabBouton[j][i], new GridBagConstraints ((j+1), i, 1, 1, 0,0,GridBagConstraints.CENTER,
GridBagConstraints.CENTER,
new Insets (4,4,4,4), 0, 0));
0
dmaillet Messages postés 500 Date d'inscription mercredi 20 août 2003 Statut Membre Dernière intervention 11 juillet 2007
27 janv. 2005 à 16:07
Nan, mais essaie de la faire avec la classe que je t'ai file,

et en faisant des setPreferredSize pour chaque bouton...

chez moi en tout cas c'est bon...

-------------------
dams
-------------------
0
Rejoignez-nous