una teclas teclado que por otra mueven mueve muevan mis las laptop hoja hacer flechas dirección desplazamiento desplaza desactivar como comando celdas celda bloqueo java swing

java - teclas - hoja de excel no se desplaza



¿Cómo hacer que JTextPane desplace el desplazamiento automático solo cuando la barra de desplazamiento está en la parte inferior y el bloqueo de desplazamiento está desactivado? (6)

Después de leer Mike Clark y la solución al azar, termino con el siguiente fragmento de código.

private boolean doAutoScroll = true; private JTextPane textPane; private JScrollPane scrollPane; public void setup() { /* Left Panel */ textPane = new JTextPane(); textPane.setPreferredSize(new Dimension(600, 400)); // width, height /* * Not update the cursor position after inserting or appending text to the JTextPane. * [NOTE] * This breaks normal typing into the JTextPane. * This approach assumes that all updates to the JTextPane are programmatic. */ DefaultCaret caret = (DefaultCaret) textPane.getCaret(); caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE); scrollPane = new JScrollPane(textPane); scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() { BoundedRangeModel brm = scrollPane.getVerticalScrollBar().getModel(); @Override public void adjustmentValueChanged(AdjustmentEvent e) { // Invoked when user select and move the cursor of scroll by mouse explicitly. if (!brm.getValueIsAdjusting()) { if (doAutoScroll) brm.setValue(brm. getMaximum()); } else { // doAutoScroll will be set to true when user reaches at the bottom of document. doAutoScroll = ((brm.getValue() + brm.getExtent()) == brm.getMaximum()); } } }); scrollPane.addMouseWheelListener(new MouseWheelListener() { BoundedRangeModel brm = scrollPane.getVerticalScrollBar().getModel(); @Override public void mouseWheelMoved(MouseWheelEvent e) { // Invoked when user use mouse wheel to scroll if (e.getWheelRotation() < 0) { // If user trying to scroll up, doAutoScroll should be false. doAutoScroll = false; } else { // doAutoScroll will be set to true when user reaches at the bottom of document. doAutoScroll = ((brm.getValue() + brm.getExtent()) == brm.getMaximum()); } } }); }

La diferencia es que también está usando MouseWheelListener para actualizar el indicador doAutoScroll , incluso si el usuario usa la rueda del mouse para desplazarse hacia arriba y hacia abajo.

¿Cómo hacer que JTextPane desplace el desplazamiento automático solo cuando la barra de desplazamiento está en la parte inferior y el bloqueo de desplazamiento está desactivado? Esto no debería tener nada que ver con caret, que es lo que parece encontrar en Google. :(


Desplazamiento de área de texto puede ser de interés.

No tengo idea de cómo la tecla de bloqueo de desplazamiento lo afecta. Encontré lo siguiente de la página de Wikipedia en Bloqueo de desplazamiento :

Por lo tanto, Scroll Lock se puede considerar como una característica difunta en casi todos los programas modernos y sistemas operativos.

Entonces no me preocuparía.


Poco tarde a esta pregunta, pero se me ocurrió esta solución.

conversationPane = new JTextPane(); final JScrollPane conversationScrollPane = new JScrollPane(conversationPane); conversationScrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() { BoundedRangeModel brm = conversationScrollPane.getVerticalScrollBar().getModel(); boolean wasAtBottom = true; public void adjustmentValueChanged(AdjustmentEvent e) { if (!brm.getValueIsAdjusting()) { if (wasAtBottom) brm.setValue(brm.getMaximum()); } else wasAtBottom = ((brm.getValue() + brm.getExtent()) == brm.getMaximum()); } });

Parece funcionar perfectamente para mis necesidades. Pequeña explicación: Esencialmente si la barra de desplazamiento no está siendo movida por una persona y la barra estaba por último en el máximo / inferior, reinícielo al máximo. Si se está ajustando manualmente, luego verifique si se ajustó para estar en la parte inferior.


Prueba esto :

JTextArea txt = new JTextArea(); JScrollPane jsp = new JScrollPane(history, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); txt.setCaretPosition(txt.getDocument().getLength()); // do this afeter any event

Espero que te ayude


Necesitaba hacer lo mismo para un área de texto de registro. Las soluciones que encontré en la web no funcionaron para mí (ya sea que detengan el desplazamiento automático al iniciar sesión en muchos mensajes rápidamente o que bloqueen la barra de desplazamiento en la parte inferior incluso si se desplaza hacia arriba con la rueda del mouse).

Lo hice de esta manera:

public static void makeTextAreaAutoScroll(JTextArea textArea) { // Get the text area''s scroll pane : final JScrollPane scrollPane = (JScrollPane) (textArea.getParent().getParent()); // Disable the auto scroll : ((DefaultCaret)textArea.getCaret()).setUpdatePolicy(DefaultCaret.NEVER_UPDATE); // Add a listener to the vertical scroll bar : scrollPane.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() { private int _val = 0; private int _ext = 0; private int _max = 0; private final BoundedRangeModel _model = scrollPane.getVerticalScrollBar().getModel(); @Override public void adjustmentValueChanged(AdjustmentEvent e) { // Get the new max : int newMax = _model.getMaximum(); // If the new max has changed and if we were scrolled to bottom : if (newMax != _max && (_val + _ext == _max) ) { // Scroll to bottom : _model.setValue(_model.getMaximum() - _model.getExtent()); } // Save the new values : _val = _model.getValue(); _ext = _model.getExtent(); _max = _model.getMaximum(); } }); }

Solo úsalo de esta manera:

makeTextAreaAutoScroll(yourTextArea);

Puedes probar con esta pieza de código:

new Timer().schedule(new TimerTask() { @Override public void run() { javax.swing.SwingUtilities.invokeLater(new Runnable() { @Override public void run() { String line = "test " + Math.random(); yourTextArea.append(yourTextArea.getText().isEmpty() ? line : "/n" + line); } }); } }, 0, 5);

Ahora su área de texto debe desplazarse automáticamente si la barra de desplazamiento está en la parte inferior, detener el desplazamiento automático si mueve la barra de desplazamiento (arrastrando la barra o usando la rueda) y vuelve a desplazarse automáticamente si vuelve a colocar la barra de desplazamiento abajo.


Creo que mi programa a continuación cumple exactamente con sus requisitos, con una posible advertencia: no está permitido escribir en el área de texto. Entonces esto sería bueno para un visor de registro, pero no para una consola interactiva. El código se ejecuta un poco largo porque lo he convertido en una demo lista para ejecutar del enfoque. Sugiero ejecutar el programa tal como está y verificar el comportamiento. Si el comportamiento funciona bien para usted, invierta un poco de tiempo en estudiar el código. He incluido comentarios en el código para resaltar algunas de las secciones más importantes.

Actualización del 17/07/2013: es posible que también desee consultar la solución de random dude en su respuesta separada más abajo en la página. Su enfoque es más elegante que el mío.

También vea Swing: Desplácese hasta la parte inferior de JScrollPane, condicional en la ubicación actual de la vista para una posible solución que no interfiera con la posición de intercalación.

El código fuente de SCCE sigue:

import java.awt.*; import java.awt.event.*; import java.util.*; import java.util.Timer; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; public class ScrollingJTextAreaExample extends JFrame { // Worker thread to help periodically append example messages to JTextArea Timer timer = new Timer(); // Merely informative counter, will be displayed with the example messages int messageCounter = 0; // GUI components JScrollPane jScrollPane; JTextArea jTextArea; public ScrollingJTextAreaExample() { initComponents(); // Boiler plate GUI construction and layout // Configure JTextArea to not update the cursor position after // inserting or appending text to the JTextArea. This disables the // JTextArea''s usual behavior of scrolling automatically whenever // inserting or appending text into the JTextArea: we want scrolling // to only occur at our discretion, not blindly. NOTE that this // breaks normal typing into the JTextArea. This approach assumes // that all updates to the ScrollingJTextArea are programmatic. DefaultCaret caret = (DefaultCaret) jTextArea.getCaret(); caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE); // Schedule a task to periodically append example messages to jTextArea timer.schedule(new TextGeneratorTask(), 250, 250); // This DocumentListener takes care of re-scrolling when appropriate Document document = jTextArea.getDocument(); document.addDocumentListener(new ScrollingDocumentListener()); } // Boring, vanilla GUI construction and layout code private void initComponents() { jScrollPane = new javax.swing.JScrollPane(); jTextArea = new javax.swing.JTextArea(); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); jScrollPane.setViewportView(jTextArea); getContentPane().add(jScrollPane, java.awt.BorderLayout.CENTER); setSize(320, 240); setLocationRelativeTo(null); } // ScrollingDocumentListener takes care of re-scrolling when appropriate class ScrollingDocumentListener implements DocumentListener { public void changedUpdate(DocumentEvent e) { maybeScrollToBottom(); } public void insertUpdate(DocumentEvent e) { maybeScrollToBottom(); } public void removeUpdate(DocumentEvent e) { maybeScrollToBottom(); } private void maybeScrollToBottom() { JScrollBar scrollBar = jScrollPane.getVerticalScrollBar(); boolean scrollBarAtBottom = isScrollBarFullyExtended(scrollBar); boolean scrollLock = Toolkit.getDefaultToolkit() .getLockingKeyState(KeyEvent.VK_SCROLL_LOCK); if (scrollBarAtBottom && !scrollLock) { // Push the call to "scrollToBottom" back TWO PLACES on the // AWT-EDT queue so that it runs *after* Swing has had an // opportunity to "react" to the appending of new text: // this ensures that we "scrollToBottom" only after a new // bottom has been recalculated during the natural // revalidation of the GUI that occurs after having // appending new text to the JTextArea. EventQueue.invokeLater(new Runnable() { public void run() { EventQueue.invokeLater(new Runnable() { public void run() { scrollToBottom(jTextArea); } }); } }); } } } class TextGeneratorTask extends TimerTask { public void run() { EventQueue.invokeLater(new Runnable() { public void run() { String message = (++messageCounter) + " Lorem ipsum dolor sit amet, consectetur" + " adipisicing elit, sed do eiusmod tempor" + " incididunt ut labore et dolore magna aliqua./n"; jTextArea.append(message); } }); } } public static boolean isScrollBarFullyExtended(JScrollBar vScrollBar) { BoundedRangeModel model = vScrollBar.getModel(); return (model.getExtent() + model.getValue()) == model.getMaximum(); } public static void scrollToBottom(JComponent component) { Rectangle visibleRect = component.getVisibleRect(); visibleRect.y = component.getHeight() - visibleRect.height; component.scrollRectToVisible(visibleRect); } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new ScrollingJTextAreaExample().setVisible(true); } }); } }