Results 1 to 3 of 3
Thread: JScrollPane And ArrowKeys
- 09-02-2008, 02:11 AM #1
Member
- Join Date
- Aug 2008
- Posts
- 3
- Rep Power
- 0
JScrollPane And ArrowKeys
Hi !
How can I bind arrow keys to my JScrollPane . So Instead of mouse , I can use arrow key to navigate.
ImageIcon image = new ImageIcon ("ar.gif");
imageLabel = new JLabel(image);
imagePane = new JScrollPane(imageLabel);
imagePane.setPreferredSize(new Dimension (1200, 725));
- 09-02-2008, 03:23 AM #2
Does the JScrollPane have methods you can call to position its view? If you can find them in the API doc, then you need to capture the keys and have them call the methods to scroll the view.
- 09-04-2008, 08:22 PM #3
To see which keys are already bound to JScrollBar see this.
You could bind the page_up and page_down keys to do vertically what ctrl_home and ctrl_end keys do for the horizontal.
Java Code:import java.awt.*; import java.awt.event.*; import java.awt.image.BufferedImage; import java.io.*; import javax.swing.*; public class ArrowScrolling { JLabel label; private JScrollPane getContent(BufferedImage image) { label = new ScrollingLabel(new ImageIcon(image), JLabel.CENTER); label.setFocusable(true); return new JScrollPane(label); } public static void main(String[] args) throws IOException { File file = new File("images/redfox.jpg"); BufferedImage image = javax.imageio.ImageIO.read(file); ArrowScrolling test = new ArrowScrolling(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test.getContent(image)); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); test.label.requestFocusInWindow(); } } class ScrollingLabel extends JLabel implements Scrollable { public ScrollingLabel(ImageIcon icon, int horizontalAlignment) { super(icon, horizontalAlignment); registerKeys(); } public Dimension getPreferredScrollableViewportSize() { return getPreferredSize(); } public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) { Dimension d = ((JViewport)getParent()).getExtentSize(); return (orientation == SwingConstants.HORIZONTAL) ? d.width : d.height; } public boolean getScrollableTracksViewportHeight() { return false; } public boolean getScrollableTracksViewportWidth() { return false; } public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { return 50; } private void registerKeys() { int c = JComponent.WHEN_IN_FOCUSED_WINDOW; InputMap inputMap = getInputMap(c); ActionMap actionMap = getActionMap(); int ctrl = InputEvent.CTRL_DOWN_MASK; KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_UP, ctrl, false); inputMap.put(ks, "UP"); actionMap.put("UP", upAction); ks = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, ctrl, false); inputMap.put(ks, "DOWN"); actionMap.put("DOWN", downAction); ks = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, ctrl, false); inputMap.put(ks, "LEFT"); actionMap.put("LEFT", leftAction); ks = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, ctrl, false); inputMap.put(ks, "RIGHT"); actionMap.put("RIGHT", rightAction); } private Action upAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { JViewport viewport = (JViewport)getParent(); Rectangle r = viewport.getViewRect(); int inc = getScrollableBlockIncrement(r, SwingConstants.VERTICAL, -1); Point p = r.getLocation(); p.y = (p.y - inc >= 0) ? p.y - inc : 0; viewport.setViewPosition(p); } }; private Action downAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { JViewport viewport = (JViewport)getParent(); Rectangle r = viewport.getViewRect(); int inc = getScrollableBlockIncrement(r, SwingConstants.VERTICAL, 1); Point p = r.getLocation(); int maxY = viewport.getView().getHeight() - r.height; p.y = (p.y + inc <= maxY) ? p.y + inc : maxY; viewport.setViewPosition(p); } }; private Action leftAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { JViewport viewport = (JViewport)getParent(); Rectangle r = viewport.getViewRect(); int inc = getScrollableBlockIncrement(r, SwingConstants.HORIZONTAL, -1); Point p = r.getLocation(); p.x = (p.x - inc >= 0) ? p.x - inc : 0; viewport.setViewPosition(p); } }; private Action rightAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { JViewport viewport = (JViewport)getParent(); Rectangle r = viewport.getViewRect(); int inc = getScrollableBlockIncrement(r, SwingConstants.HORIZONTAL, 1); Point p = r.getLocation(); int maxX = viewport.getView().getWidth() - r.width; p.x = (p.x + inc <= maxX) ? p.x + inc : maxX; viewport.setViewPosition(p); } }; }
Similar Threads
-
[SOLVED] JScrollPane - HELP!
By terox13 in forum AWT / SwingReplies: 8Last Post: 05-10-2008, 03:58 AM -
JScrollPane with HTML
By Java Tip in forum Java TipReplies: 0Last Post: 03-14-2008, 11:32 AM -
jscrollpane problem
By monkey04 in forum AWT / SwingReplies: 2Last Post: 01-19-2008, 05:23 AM -
help with JScrollPane
By tommy in forum AWT / SwingReplies: 1Last Post: 08-06-2007, 07:58 PM -
JScrollPane not scrolling
By Riftwalker in forum Advanced JavaReplies: 2Last Post: 07-17-2007, 08:16 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks