Re: JScrollPane doesn't resize properly
Andrew Thompson wrote:
halex2000 wrote:
Thank you,
Your future lack of top-posting, will be thanks enough.
I can show you the frame class, but in order to reproduce the issue I should
send you also a data file (the content) which I'm not allowed to.
No. I do not now, nor *never* wanted, 'the content'.
The fact you would say that, indicates to me that
you did not read the document I linked to earlier.
Please read it - I think it might help you.
The link to the SSCCE again, is..
<http://www.physci.org/codes/sscce> for tips.
Below is an example of an SSCCE. It has a JPanel in a JScrollPane in a
JFrame. clicking the panel randomly resizes it, causing scrollbars to
appear as needed.
----------------------8<---------------------
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
public class PanelScroller extends JPanel implements MouseListener {
JPanel drawing;
JLabel sizeLabel = new JLabel(" Click Me! ");
Random r = new Random();
PanelScroller() {
drawing = new JPanel();
drawing.add(sizeLabel);
drawing.setBackground(Color.BLUE);
drawing.addMouseListener(this);
setPanelSize(300, 200);
JScrollPane scroller = new JScrollPane(drawing);
add(scroller);
}
void setPanelSize(int w, int h) {
sizeLabel.setText(w + " x " + h);
Dimension d = new Dimension(w, h);
drawing.setPreferredSize(d);
drawing.setMaximumSize(d);
drawing.setMinimumSize(d);
drawing.setSize(d);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame("App Title");
f.add(new PanelScroller());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
});
}
public void mouseClicked(MouseEvent arg0) {
int width = 250 + r.nextInt(100);
int height = 150 + r.nextInt(75);
setPanelSize(width, height);
}
public void mousePressed(MouseEvent arg0) {}
public void mouseReleased(MouseEvent arg0) {}
public void mouseEntered(MouseEvent arg0) {}
public void mouseExited(MouseEvent arg0) {}
}
----------------------8<---------------------
Making it work sensibly when the JFrame is resized is left as an
exercise for the reader :-)