Forums Rue-Montgallet.com
Rue-Montgallet.comRue-Hardware.comRue-Occasion.comRue-DVD.comRue-Jeuxvideo.comRue-AudioVideo.comRue-Telephone.comForums
S'inscrire | S'identifier |
| Recherche avancée | Aide
 
 

Achat - Vente Divers : schloups et 32 utilisateurs inconnus

 Mot :   Pseudo :  
 
Bas de page
Auteur
 Sujet :

JSplitPane customisé et méthod paintComponent() récalcitrante [RESOLU]

 
n°5806
ollone
Part of the 'One brothers
Profil : Vieux de la vieille
Posté le 19-11-2003 à 14:47:39  profilanswer
 

Salut !
J'ai un souci avec la méthode paintComponent(...) d'un JSplitPane. Ce dernier est customisé afin d'accueillir en son Divider, une ToolBar...
 
Mon souci est le suivant : l'affichage de mon JSplitPane fait pêter les 100% de mon CPU :o  
Il est clair que c'est la méthode paintComponent(...) qui ne s'arrête jamais d'être appelée.
 
Le flag de la méthode m'a permis de me rendre compte que getDividerLocation() me retourne tjrs 0 !!!
 
Help siou plait, j'ai du mal à concevoir le problème, j'suis pas fortiche en 'painting issues'...
 

Code :
  1. private class MySplitPane extends JSplitPane {
  2.     public MySplitPane(Component comp1, Component comp2) {
  3.       super(JSplitPane.VERTICAL_SPLIT, comp1, comp2);
  4.       JToolBar toolbar = new JToolBar();
  5.       /*
  6.       toolbar.setBorder(BorderFactory.createCompoundBorder(
  7.           BorderFactory.createMatteBorder(1,0,0,0,new Color(102,102,102)),
  8.           BorderFactory.createMatteBorder(0,0,1,0,new Color(102,102,102))));
  9.       toolbar.setBorderPainted(true);
  10.           */
  11.       toolbar.setBorder(BorderFactory.createEmptyBorder(0,3,0,0));
  12.       toolbar.setFloatable(false);
  13.       toolbar.setRollover(true);
  14.       toolbar.setBackground(Color.white);
  15.       JButton button1 = new JButton();
  16.       JButton button2 = new JButton();
  17.       JButton button3 = new JButton();
  18.       JButton button4 = new JButton();
  19.       JButton button5 = new JButton();
  20.       button1.setIcon(new ImageIcon("images/gras.png" ));
  21.       button2.setIcon(new ImageIcon("images/italic.png" ));
  22.       button3.setIcon(new ImageIcon("images/url.png" ));
  23.       button4.setIcon(new ImageIcon("images/image.png" ));
  24.       button5.setIcon(new ImageIcon("images/icons.gif" ));
  25.       button1.setBackground(Color.white);
  26.       button2.setBackground(Color.white);
  27.       button3.setBackground(Color.white);
  28.       button4.setBackground(Color.white);
  29.       button5.setBackground(Color.white);
  30.       toolbar.add(button1);
  31.       toolbar.add(button2);
  32.       toolbar.add(button3);
  33.       toolbar.add(button4);
  34.       toolbar.add(button5);
  35.       setDivider(toolbar);
  36.     }
  37.     private void setDivider(Component comp) {
  38.       try {
  39.         addImpl(comp, JSplitPane.DIVIDER, 0);
  40.       }
  41.       catch (IllegalArgumentException e) {
  42.         System.out.println("> Custom Divider Problem : "+e.toString());
  43.       }
  44.     }
  45.     protected void paintComponent(Graphics g) {
  46.       super.paintComponent(g);
  47.       if (getDividerLocation() != getLastDividerLocation()) {
  48.         System.out.println("> location,last = "+getDividerLocation()+","+getLastDividerLocation());
  49.         if (this.getMaximumDividerLocation() > 100) {
  50.           setDividerLocation(this.getMaximumDividerLocation() - 50);
  51.           setLastDividerLocation(this.getMaximumDividerLocation() - 50);
  52.         }
  53.         else {
  54.           setDividerLocation(2*(this.getMaximumDividerLocation()/3));
  55.           setLastDividerLocation(2*(this.getMaximumDividerLocation()/3));
  56.         }
  57.       }
  58.     }
  59.   }


 
Merci d'avoir lu jusque là  ;)  
 :jap:  :jap:  :jap:


Message édité par ollone le 19-11-2003 à 18:32:32
n°5807
ollone
Part of the 'One brothers
Profil : Vieux de la vieille
Posté le 19-11-2003 à 18:30:04  profilanswer
 

OK, problème résolu !!!  :D Là j'suis content :sol:  
 
Le problème était le suivant :
j'ajoutais ma toolbar de la mauvaise manière! Dès lors la méthode getDividerLocation() me retournait systématiquement la valeur 0.
Alors ma méthode paintComponent() était systématiquement appelée et faisait anormalement monter le CPU en charge.
 
La solution est simple :
J'ajoute ma toolbar via le BasicSplitPaneDivider contenu par défaut dans un JSplitPane, et ca marche nickel !
Mon divider reste draggable (ce qui n'était pas le cas avant) et mon divider se positionne correctement.
 
Voici le code complet de la classe :
 

Code :
  1. private class MySplitPane extends JSplitPane {
  2.     BasicSplitPaneDivider dividerContainer;
  3.     JToolBar toolbar;
  4.     Cursor toolbarCursor;
  5.     public MySplitPane(Component comp1, Component comp2) {
  6.       super(JSplitPane.VERTICAL_SPLIT, comp1, comp2);
  7.       toolbar = new JToolBar();
  8.       toolbarCursor = new Cursor(Cursor.DEFAULT_CURSOR);
  9.       toolbar.setCursor(toolbarCursor);
  10.       toolbar.setBorder(BorderFactory.createEmptyBorder(0,0,0,0));
  11.       toolbar.setFloatable(false);
  12.       toolbar.setRollover(true);
  13.       toolbar.setBackground(Color.white);
  14.       JButton button1 = new JButton();
  15.       JButton button2 = new JButton();
  16.       JButton button3 = new JButton();
  17.       JButton button4 = new JButton();
  18.       JButton button5 = new JButton();
  19.       button1.setIcon(new ImageIcon("images/gras.png" ));
  20.       button2.setIcon(new ImageIcon("images/italic.png" ));
  21.       button3.setIcon(new ImageIcon("images/url.png" ));
  22.       button4.setIcon(new ImageIcon("images/image.png" ));
  23.       button5.setIcon(new ImageIcon("images/icons.gif" ));
  24.       button1.setBackground(Color.white);
  25.       button2.setBackground(Color.white);
  26.       button3.setBackground(Color.white);
  27.       button4.setBackground(Color.white);
  28.       button5.setBackground(Color.white);
  29.       toolbar.add(button1);
  30.       toolbar.add(button2);
  31.       toolbar.add(button3);
  32.       toolbar.add(button4);
  33.       toolbar.add(button5);
  34.       try {
  35.         dividerContainer = (BasicSplitPaneDivider) this.getComponent(2);
  36.         dividerContainer.setBackground(Color.white);
  37.         dividerContainer.setBorder(BorderFactory.createEmptyBorder(0,4,0,4));
  38.         dividerContainer.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
  39.         dividerContainer.add(toolbar);
  40.         dividerContainer.setDividerSize(toolbar.getPreferredSize().height);
  41.       }
  42.       catch (Exception e) {
  43.         System.out.println("> EXCEPTION :: "+e.toString());
  44.       }
  45.     }
  46.     protected void paintComponent(Graphics g) {
  47.       super.paintComponent(g);
  48.       //System.out.println("> location,last = "+getDividerLocation()+","+getLastDividerLocation());
  49.       int lowLimit = getLastDividerLocation() - 5*(getMaximumDividerLocation()/100);
  50.       int highLimit = getLastDividerLocation() + 5*(getMaximumDividerLocation()/100);
  51.       if (lowLimit<0) lowLimit=0;
  52.       if (highLimit>getMaximumDividerLocation()) highLimit = getMaximumDividerLocation();
  53.       //System.out.println("> limits : "+lowLimit+" - "+highLimit);
  54.       if (getDividerLocation() < lowLimit || getDividerLocation() > highLimit) {
  55.         if (this.getMaximumDividerLocation() > 100) {
  56.           setDividerLocation(this.getMaximumDividerLocation() - 50);
  57.           setLastDividerLocation(this.getMaximumDividerLocation() - 50);
  58.         }
  59.         else {
  60.           setDividerLocation(2*(this.getMaximumDividerLocation()/3));
  61.           setLastDividerLocation(2*(this.getMaximumDividerLocation()/3));
  62.         }
  63.       }
  64.     }
  65.   }

n°9120
blackshark
j'ai toujours faim ;)
Profil : Jeune recrue
Posté le 16-06-2004 à 11:46:45  profilanswer
 

salut j'ai remarqué que tu fesai du java et j'ai une question a un probleme auquel tu a ptet deja pense
en fait je voudrai faire une fenetre en l'occurence une JFrame mais je veut pas avoir la fenetre de base associé au systemme d'exploitation...
en fait je voudrai personnaliser le contour du style faire un joli contour de fenetre et l'associer a ma JFrame
alors tout d'abord est ce que c possible et ensuite est ce que tu peut m'expliquer comment faire ou alors mieux si tu a vu un bout de code qui fais cela est ce que tu pourai me le montrer je te remercie bcp....
BlAcKsHARk :wahoo:

n°9124
ollone
Part of the 'One brothers
Profil : Vieux de la vieille
Posté le 16-06-2004 à 12:04:56  profilanswer
 

Alors... Déjà, oui c'est possible :D
 
Ta solution se trouve du côté des Look & Feel. Toutefois, ce n'est pas une tache aisée de réaliser son propre L&F...
Il en existe en téléchargement, et des sympas parfois (next, oyoaha...).
 
Tu peux te rendre compte de la différence de présentation des fenêtres selon le L&F dans un des exemples du site de sun, faut que je retrouve le lien...
En attendant :: Comment setter le Look & Feel
 
 
EDIT :: Commetn créer son propre L&F


Message édité par ollone le 16-06-2004 à 12:08:18

---------------
C'est naturel que les filles soient plus belles, et les garçons plus cons ^^ [un peu bonobo sur les bords]

Aller à :
Ajouter une réponse