Link to home
Start Free TrialLog in
Avatar of f22
f22

asked on

How to write a scrollable JDesktopPane?

I implemented the Scrollable interface in my class,then I create a JScrollPane of it, but it is still not scrollable.
Code examples is welcomed. Thank you!
Avatar of heyhey_
heyhey_

you want to create scorllable JDesktopPane, (so that when some internal frames get out of the screen JDesktopPane adds scrollers to itself) or ?
Avatar of f22

ASKER

Yes, that's what I want. But I have solve this problem. I have another question, is it possible to create graphical components of arbitrary shape? What I want is to a line shape. I need to display a lot of lines in the panel and each line is an object so I can click on them, for example. But if I use Canvas, I'm afraid the rectangle shape of canvas will cause the lines block each other.
Thank you for your attention. I will pass the points to you if you'd like to give comments on this problem.
check this question (and my comments there) - it is open. maybe this is what you want - i don't know.
https://www.experts-exchange.com/topics/comp/lang/java/Q.10110666

can you (please :) post some (little) working example about scrollable JDesktopPane? this question was discussed a long ago at DukeDollars forum (Javasoft web site) and there was partially solution there (i was going to give you the URL when you define more precisely your question:))

merry christmas
  heyhey
Avatar of f22

ASKER

Thank you for your comment! I solve the problem by using setPreferedSize of the JDesktopPane.

import java.applet.Applet;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class WorkPane extends JDesktopPane implements Scrollable, MouseMotionListener{
      public WorkPane(){
            setLayout(null);
            setPreferredSize(new Dimension(1000,1000));
            addMouseMotionListener(this);
      }
      

    public Dimension getPreferredScrollableViewportSize() {
        return getPreferredSize();
    }

    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {

        int currentPosition = 0;
        if (orientation == SwingConstants.HORIZONTAL)
            currentPosition = visibleRect.x;
        else
            currentPosition = visibleRect.y;

        if (direction < 0) {
            int newPosition = currentPosition - (currentPosition / maxUnitIncrement) * maxUnitIncrement;
            return (newPosition == 0) ? maxUnitIncrement : newPosition;
        } else {
            return ((currentPosition / maxUnitIncrement) + 1) * maxUnitIncrement - currentPosition;
        }
    }

    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
        if (orientation == SwingConstants.HORIZONTAL)
            return visibleRect.width - maxUnitIncrement;
        else
            return visibleRect.height - maxUnitIncrement;
    }

    public boolean getScrollableTracksViewportWidth() {
        return false;
    }

    public boolean getScrollableTracksViewportHeight() {
        return false;
    }
  }

then I use a JScrollPane to display it.
Please post an anwser so that I can pass you the points.
a haven't helped you so much :)
if you want, you can delete the question and get back your points.
if you think that i've really helped you, then i'll post an answer.
ASKER CERTIFIED SOLUTION
Avatar of heyhey_
heyhey_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial