Example usage for javax.swing JScrollPane JScrollPane

List of usage examples for javax.swing JScrollPane JScrollPane

Introduction

In this page you can find the example usage for javax.swing JScrollPane JScrollPane.

Prototype

public JScrollPane(Component view) 

Source Link

Document

Creates a JScrollPane that displays the contents of the specified component, where both horizontal and vertical scrollbars appear whenever the component's contents are larger than the view.

Usage

From source file:Main.java

public static void main(String[] args) {
    int ROWS = 100;
    JPanel content = new JPanel();
    content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
    content.add(new JLabel("Thanks for helping out. Use tab to move around."));
    for (int i = 0; i < ROWS; i++) {
        JTextField field = new JTextField("" + i);
        field.setName("field#" + i);
        content.add(field);//from   w  ww .  j a va2  s  . c  o m
    }
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener("focusOwner",
            new PropertyChangeListener() {
                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    if (!(evt.getNewValue() instanceof JComponent)) {
                        return;
                    }
                    JViewport viewport = (JViewport) content.getParent();
                    JComponent focused = (JComponent) evt.getNewValue();
                    if (content.isAncestorOf(focused)) {
                        Rectangle rect = focused.getBounds();
                        Rectangle r2 = viewport.getVisibleRect();
                        content.scrollRectToVisible(
                                new Rectangle(rect.x, rect.y, (int) r2.getWidth(), (int) r2.getHeight()));
                    }
                }
            });
    JFrame window = new JFrame();
    window.setContentPane(new JScrollPane(content));
    window.setSize(200, 200);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
}

From source file:Main.java

public static void main(String[] argv) {
    JTextComponent textcomp = new JTextArea();
    final UndoManager undo = new UndoManager();
    Document doc = textcomp.getDocument();

    doc.addUndoableEditListener(new UndoableEditListener() {
        public void undoableEditHappened(UndoableEditEvent evt) {
            undo.addEdit(evt.getEdit());
        }/* w w  w .j a v a  2  s. c o m*/
    });

    textcomp.getActionMap().put("Undo", new AbstractAction("Undo") {
        public void actionPerformed(ActionEvent evt) {
            try {
                if (undo.canUndo()) {
                    undo.undo();
                }
            } catch (CannotUndoException e) {
            }
        }
    });

    textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");

    textcomp.getActionMap().put("Redo", new AbstractAction("Redo") {
        public void actionPerformed(ActionEvent evt) {
            try {
                if (undo.canRedo()) {
                    undo.redo();
                }
            } catch (CannotRedoException e) {
            }
        }
    });

    textcomp.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo");

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new JScrollPane(textcomp));
    frame.setSize(380, 320);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}

From source file:TreeTester.java

public static void main(String args[]) {
    JFrame f = new JFrame("Tree Dragging Tester");
    DndTree tree1 = new DndTree();
    JScrollPane leftPane = new JScrollPane(tree1);
    DndTree tree2 = new DndTree();
    JScrollPane rightPane = new JScrollPane(tree2);
    JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPane, rightPane);
    f.getContentPane().add(splitPane, BorderLayout.CENTER);
    f.setSize(300, 200);//from   w w  w .  jav  a2  s  .  c  o  m
    f.setVisible(true);
}