IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

AWT/Swing Java Discussion :

[JSplitPane]Changer le contenu sans bouger le divider


Sujet :

AWT/Swing Java

  1. #1
    Membre éclairé Avatar de biozaxx
    Profil pro
    Inscrit en
    Août 2004
    Messages
    403
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2004
    Messages : 403
    Par défaut [JSplitPane]Changer le contenu sans bouger le divider
    salut,

    j'ai une JFrame global contenant un jtabbedpane.
    Les onglets du JtabbedPane se partagent des jpanel et des jsplitpane.

    a l'initialisation, j'appelle setPreferredSize() sur mes panel ainsi que setDividerLocation() sur les splitpanel pour agencer le tout correctement.

    Le probleme est le suivant:
    si je modifie la taille du splitpane dans l'onglet 1 (disons qu'il est initialisé a 50/50 et je le glisse pour avoir une repartition 20/80) , qu'ensuite je selectionne l'onglet 2 (mes splitpane revienne a une taille initiale, ce qui n'est pas genant la premiere fois que j'arrive dans un onglet) , qd je reviens sur l'onglet 1 , la repartition n'est plus de 20/80 mais de 50/50.

    comment faire pour garder en memoire la position non pas initiale mais que j'ai changer manuellement ?

    en esperant avoir été clair

  2. #2
    Membre éclairé
    Avatar de seiryujay
    Profil pro
    Inscrit en
    Mars 2004
    Messages
    950
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2004
    Messages : 950
    Par défaut
    Ca peut servir : (http://java.sun.com/docs/books/tutor...e.html#divider)

    By default, a split pane's preferred size and divider location are initialized so that the two components in the split pane are at their preferred sizes. If the split pane isn't displayed at this preferred size and the program hasn't set the divider's location explicitly, then the initial position of the divider (and thus the sizes of the two components) depends on a split pane property called the resize weight. If the split pane is initially at its preferred size or bigger, then the contained components start out at their preferred sizes, before adjusting for the resize weight. If the split pane is initially too small to display both components at their preferred sizes, then they start out at their minimum sizes, before adjusting for the resize weight.

    A split pane's resize weight has a value between 0.0 and 1.0 and determines how space is distributed between the two contained components when the split pane's size is set — whether programmatically or by the user resizing the split pane (enlarging its containing window, for example). The resize weight of a split pane is 0.0 by default, indicating that the left or top component's size is fixed, and the right or bottom component adjusts its size to fit the remaining space. Setting the resize weight to 0.5 splits any extra or missing space evenly between the two components. Setting the resize weight to 1.0 makes the right or bottom component's size remain fixed. The resize weight has no effect, however, when the user drags the divider.

    The user can drag the divider to any position as long as neither contained component goes below its minimum size. If the divider has one-touch buttons, the user can use them to make the divider move completely to one side or the other — no matter what the minimum sizes of the components are.

    Now that you know the factors that affect a split pane's size and divider location, here are some rules for making them work well:

    To ensure that the divider can be dragged when the split pane is at its preferred size, make sure the minimum size of one or both contained components is smaller than the contained component's preferred size. You can set the minimum size of a component either by invoking setMinimumSize on it or by overriding its getMinimumSize method. For example, if you want the user to be able to drag the divider all the way to both sides:
    Dimension minimumSize = new Dimension(0, 0);
    leftComponent.setMinimumSize(minimumSize);
    rightComponent.setMinimumSize(minimumSize);

    To guarantee that both contained components appear, make sure that either the split pane is initially at or above its preferred size, or the minimum sizes of the contained components are greater than zero.

    If you want the bottom or right component to stay the same size and the top or left component to be flexible when the split pane gets bigger, set the resize weight to 1.0. You can do this by invoking setResizeWeight:
    splitPane.setResizeWeight(1.0);


    If you want both halves of the split pane to share in the split pane's extra or removed space, set the resize weight to 0.5:
    splitPane.setResizeWeight(0.5);


    Make sure each component contained by a split pane has a reasonable preferred size. If the component is a panel that uses a layout manager, you can generally just use the value it returns. If the component is a scroll pane, you have a few choices. You can invoke the setPreferredSize method on the scroll pane, invoke the appropriate method on the component in the scroll pane (such as the setVisibleRowCount method for JList or JTree), or just set the split pane's preferred size and the divider's location.

    Make sure each component contained by a split pane can display itself reasonably in varying amounts of space. For example, panels that contain multiple components should use layout managers that use extra space in a reasonable way.

    If you want to set the size of contained components to something other than their preferred sizes, use the setDividerLocation method. For example, to make the left component 150 pixels wide:
    splitPane.setDividerLocation(150 + splitPane.getInsets().left);

    To make the right component 150 pixels wide:
    splitPane.setDividerLocation(splitPane.getSize().width
    - splitPane.getInsets().right
    - splitPane.getDividerSize()
    - 150);

    If the split pane is already visible, you can set the divider location as a percentage of the split pane. For example, to make 25% of the space go to left/top:
    splitPane.setDividerLocation(0.25);

    To lay out the split pane as if it just came up, likely repositioning the divider in the process, invoke resetToPreferredSizes() on the split pane.


    --------------------------------------------------------------------------------
    Note: Just changing the contained components' preferred sizes — even if you invoke revalidate afterwards — is not enough to cause the split pane to lay itself out again. You must invoke resetToPreferredSizes as well.
    --------------------------------------------------------------------------------


  3. #3
    Membre éclairé Avatar de biozaxx
    Profil pro
    Inscrit en
    Août 2004
    Messages
    403
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2004
    Messages : 403
    Par défaut
    a priori je gere deja ce qui est decrit mais j'ai peut etre un panel qui ne rempli pas les conditions ! je vais essayer de verifier cela.
    en tout cas merci

    si quelqu'un d'autre à rencontré le pb ...

  4. #4
    Membre éclairé Avatar de biozaxx
    Profil pro
    Inscrit en
    Août 2004
    Messages
    403
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2004
    Messages : 403
    Par défaut
    bonjour,

    je reviens sur le probleme que j'ai décris la semaine derniere.
    seiryujay, j'ai maté le tutorial, j'en ai retiré 2 ou 3 enseignements cependant cela ne resoud pas mon probleme de depart si ce n'est que j'ai pu affiner d'où vient le probleme.
    Les tabbedpane n'interviennent absolument pas dans mon probleme et il semble que ce soit lorsque je reaffecte les panels (qui ce baladent dans les onglets et les splitpane) aux splitpane que vient le probleme.

    En gros a chaque fois que je réassocie un panel à un splitpane, celui ci reprend la taille par defaut....
    C'est effectivement le comportement que j'attend à l'initialisation mais je ne trouve aucun moyen pour empecher ce comportement une fois l'appli initialisée.

    Je joins ci dessous un code qui decrit exactemet le probleme :
    au depart il y a un panel bleu et un vert.
    si on deplace le separateur et qu'ensuite on clic sur le bouton ceci remplace le panel vert par le rouge. Or les tailles sont reinitialisées alors que je souhaite que le panel rouge garde la taille 'courante' du panel vert ...

    si quelqu'un trouve une solution

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
     
    public class SplitPaneDividerDemo extends JPanel
                                      implements ActionListener {
     
        private JSplitPane splitPane;
        JPanel sd1;
        JPanel sd2;
        JPanel sd3;
        JButton reset ;
        JButton change; 
     
        public SplitPaneDividerDemo() {
            super(new BorderLayout());
            sd1 = new JPanel();
            sd1.setMinimumSize(new Dimension(30,30));
            sd1.setPreferredSize(new Dimension(60,60));
            sd1.setBackground(Color.BLUE);
     
            sd2 = new JPanel();
            sd2.setMinimumSize(new Dimension(60,60));
            sd2.setPreferredSize(new Dimension(60,60));
            sd2.setBackground(Color.GREEN);
            splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                    sd1, sd2);
            splitPane.setResizeWeight(0.5);
            splitPane.setOneTouchExpandable(true);
            splitPane.setDividerLocation(0.75);
     
     
            sd3 = new JPanel();
            sd3.setMinimumSize(new Dimension(20,20));
            sd3.setPreferredSize(new Dimension(20,20));
            sd3.setBackground(Color.RED);
     
            add(splitPane, BorderLayout.CENTER);
            add(createControlPanel(), BorderLayout.PAGE_END);
        }
     
        private JComponent createControlPanel() {
            JPanel panel = new JPanel();
            change = new JButton("change");
            change.addActionListener(this);
            panel.add(change);
            return panel;
        }
     
        public void actionPerformed(ActionEvent e) {
                splitPane.setRightComponent(sd3);
        }
     
        public static void main(String[] args) {
            JFrame frame = new JFrame();
            SplitPaneDividerDemo newContentPane = new SplitPaneDividerDemo();
            newContentPane.setOpaque(true);
            frame.setContentPane(newContentPane);
            frame.pack();
            frame.setVisible(true);
        }
    }

  5. #5
    Membre éclairé
    Avatar de seiryujay
    Profil pro
    Inscrit en
    Mars 2004
    Messages
    950
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2004
    Messages : 950
    Par défaut
    Le JSplitPane doit se baser sur la preferredSize de tes composants pour se retailler (enfin, c'est ce que je me dis).
    Du coup, ruse pour éviter le "retaillage" :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    	public void actionPerformed(ActionEvent e) {
    		int pos = splitPane.getDividerLocation();
    		splitPane.setRightComponent(sd3);
    		splitPane.setDividerLocation(pos);
    	}

  6. #6
    Membre éclairé Avatar de biozaxx
    Profil pro
    Inscrit en
    Août 2004
    Messages
    403
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2004
    Messages : 403
    Par défaut


    j'avais essayé de remplacer la taille preferrée du nouveau panel par la taille réel de l'ancien mais le probleme c'est que je ne connais pas tout le temps la composition exacte des panel que je remplace (beaucoup de jsplitpane imbriqués)

    MAIS je n'ai pas pensé une seconde à faire ce que tu decris et qui marche du tonnerre

    1000 pour ton aide

  7. #7
    Membre éclairé
    Avatar de seiryujay
    Profil pro
    Inscrit en
    Mars 2004
    Messages
    950
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2004
    Messages : 950
    Par défaut
    On est là pour ça...

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Changer le contenu sans recharger la page
    Par loliz12 dans le forum Débuter
    Réponses: 12
    Dernier message: 11/06/2011, 22h25
  2. Changer le contenu sans changer la page?!
    Par batnoir dans le forum Général JavaScript
    Réponses: 3
    Dernier message: 30/09/2009, 13h20
  3. Changer contenu sans recharger la page
    Par krustypop dans le forum Général JavaScript
    Réponses: 13
    Dernier message: 22/06/2009, 15h50
  4. [MySQL] Changer contenu sans recharger la page
    Par krustypop dans le forum PHP & Base de données
    Réponses: 4
    Dernier message: 22/06/2009, 13h11
  5. Editer sans changer le contenu existant
    Par Flavien44 dans le forum Modélisation
    Réponses: 2
    Dernier message: 12/06/2007, 12h49

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo