J'ai créé jtextpane et inséré des composants à l'intérieur de TextPane (composants tels que JTextarea). (barre de défilement vertical de) JScrollPane de JTextpane est automatiquement définie sur le bas lorsque j'insère de nouveaux composants dans ce JTextpane. Je veux garder cela pour être réglé sur la position supérieure. Comment puis-je faire cela p>
merci Sunil Kumar Sahoo P>
7 Réponses :
Il existe différentes méthodes que vous pouvez utiliser, en fonction de ce qui se trouve à l'intérieur du ScrollPane. Voir Tutoriel , la toute dernière section . p>
Voici une classe d'utilité que j'utilise. Il peut être utilisé pour faire défiler jusqu'au centre supérieur, inférieur, gauche, droit ou horizonatal / vertical d'un Jscrollpane code>. public final class ScrollUtil {
public static final int NONE = 0, TOP = 1, VCENTER = 2, BOTTOM = 4, LEFT = 8, HCENTER = 16, RIGHT = 32;
private static final int OFFSET = 100; // Required for hack (see below).
private ScrollUtil() {
}
/**
* Scroll to specified location. e.g. <tt>scroll(component, BOTTOM);</tt>.
*
* @param c JComponent to scroll.
* @param part Location to scroll to. Should be a bit-wise OR of one or moe of the values:
* NONE, TOP, VCENTER, BOTTOM, LEFT, HCENTER, RIGHT.
*/
public static void scroll(JComponent c, int part) {
scroll(c, part & (LEFT|HCENTER|RIGHT), part & (TOP|VCENTER|BOTTOM));
}
/**
* Scroll to specified location. e.g. <tt>scroll(component, LEFT, BOTTOM);</tt>.
*
* @param c JComponent to scroll.
* @param horizontal Horizontal location. Should take the value: LEFT, HCENTER or RIGHT.
* @param vertical Vertical location. Should take the value: TOP, VCENTER or BOTTOM.
*/
public static void scroll(JComponent c, int horizontal, int vertical) {
Rectangle visible = c.getVisibleRect();
Rectangle bounds = c.getBounds();
switch (vertical) {
case TOP: visible.y = 0; break;
case VCENTER: visible.y = (bounds.height - visible.height) / 2; break;
case BOTTOM: visible.y = bounds.height - visible.height + OFFSET; break;
}
switch (horizontal) {
case LEFT: visible.x = 0; break;
case HCENTER: visible.x = (bounds.width - visible.width) / 2; break;
case RIGHT: visible.x = bounds.width - visible.width + OFFSET; break;
}
// When scrolling to bottom or right of viewport, add an OFFSET value.
// This is because without this certain components (e.g. JTable) would
// not scroll right to the bottom (presumably the bounds calculation
// doesn't take the table header into account. It doesn't matter if
// OFFSET is a huge value (e.g. 10000) - the scrollRectToVisible method
// still works correctly.
c.scrollRectToVisible(visible);
}
}
Il devrait être possible de définir le DefaultCaret Code> Politique de mise à jour sur NEAM_UPDATE CODE>. Voir l'article zone de texte défilement em> a> pour d'autres utilisations. P>
J'ai constaté que le moyen le plus simple de le faire est ce qui suit:
public void scroll(int vertical) {
switch (vertical) {
case SwingConstants.TOP:
getVerticalScrollBar().setValue(0);
break;
case SwingConstants.CENTER:
getVerticalScrollBar().setValue(getVerticalScrollBar().getMaximum());
getVerticalScrollBar().setValue(getVerticalScrollBar().getValue() / 2);
break;
case SwingConstants.BOTTOM:
getVerticalScrollBar().setValue(getVerticalScrollBar().getMaximum());
break;
}
}
jscrollpane.getTerticalscrollbar (). SETVALUE (1); P>
La valeur de défilement du Scrollpane est toujours comprise entre (0,0 - 1) de
Par exemple
0.0 = 0%
0,1 = 10%
0,2 = 20%
0,25 = 25%
.... Donc sur p>
et vous pouvez régler la position de défilement à l'aide de ces valeurs. Par exemple, dans Javafx P>
// suppose this is the scrollpane ScrollPane pane = new ScrollPane(); // to scroll the scrollpane horizontally 10% from its current position pane.setHvalue(pane.getHvalue() + 0.1); // to scroll 100% pane.setHvalue(1); and so on...
Cela fonctionne aussi:
JTextArea. myTextArea; // ... myTextArea.select(0, 0); // force the scroll value to the top
Veuillez ajouter une courte explication Pourquoi votre code résout la question. Le questionneur peut donc éviter des problèmes similaires, il est possible de mieux adapter votre suggestion à son code.