cool,

it appears i have completely answered my initial question; it appears that my JPanel behaves exactly how I want. The credit to this goes to this mailing list and the mailing list on sun's java site and the sun java site. Anyway, my question is, what do you think of the solution?

MyTest3.java:

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.dnd.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import java.lang.*;

public class MyTest3 {
static OPanel p = new OPanel( );
public static void main(String[] args) {
JFrame f = new JFrame("Moo");
p.setPreferredSize(new Dimension(300,300));
JScrollPane sp = new JScrollPane(p, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
p.setScrollPane(sp);
for( int i=0; i<20; i++ )
p.add( new JButton(""+i) );
p.setAutoscrolls(true);
MouseMotionListener doScrollRectToVisible = new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
Rectangle r = new Rectangle(e.getX(), e.getY(), 1, 1);
((JPanel)e.getSource()).scrollRectToVisible(r);
}
};
p.addMouseMotionListener(doScrollRectToVisible);
sp.addComponentListener(
new ComponentAdapter() {
public void componentResized( ComponentEvent ev ) {
JComponent c = (JComponent)ev.getSource();
Insets i = c.getInsets();
p.setPreferredSize(
new Dimension(
c.getWidth()-i.left-i.right,
c.getHeight())
);
p.revalidate();
}
}
);
p.addComponentListener(
new ComponentAdapter() {
public void componentResized( ComponentEvent ev ) {
JComponent c = (JComponent)ev.getSource();
Dimension currentPrefSize = c.getPreferredSize();
Component lastComp = c.getComponent( c.getComponentCount()-1 );
int newHeight = lastComp.getLocation().y + lastComp.getHeight() + c.getInsets().bottom;
currentPrefSize.height = newHeight;
c.setPreferredSize( currentPrefSize );
c.revalidate();
}
}
);
f.setContentPane(sp);
f.setSize(300,300);
f.setVisible(true);
}
}


class OPanel extends JPanel implements Scrollable {
private JScrollPane sp;
public OPanel( ) { }
public void setScrollPane(JScrollPane sp) { this.sp = sp; }
public Dimension getPreferredScrollableViewportSize( ) { return getPreferredSize( ); }
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) { return 10; }
public boolean getScrollableTracksViewportHeight( ) {
if (this.getHeight( ) > sp.getViewport( ).getHeight( ))
return false;
else
return true;
} public boolean getScrollableTracksViewportWidth( ) { return true; } public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { return 10; }
}


_______________________________________________
Advanced-swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/advanced-swing

Reply via email to