Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Scrollpane and its scrollbars entirely rely on the size of the component in the viewport, which, by default, depends of the preferred size of that component.</p> <p>Check this code which shows you a basic example of how this all works:</p> <pre><code>import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.SwingUtilities; import javax.swing.Timer; public class TestScrollPane { public static class CustomComponent extends JPanel { private static final int RADIUS = 20; private int x = 0; private int y = 0; private double speed = 18; private double dx; private double dy; public CustomComponent() { dx = speed; dy = speed; Timer t = new Timer(20, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { x += dx; y += dy; if (x + RADIUS &gt; getWidth()) { x = getWidth() - RADIUS; dx = -speed; } else if (x &lt; 0) { x = 0; dx = speed; } if (y + RADIUS &gt; getHeight()) { y = getHeight() - RADIUS; dy = -speed; } else if (y &lt; 0) { y = 0; dy = speed; } repaint(); } }); t.start(); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.RED); g.fillOval(x, y, RADIUS, RADIUS); } @Override public Dimension getPreferredSize() { return new Dimension(600, 600); } } protected void initUI() { JFrame window = new JFrame(TestScrollPane.class.getSimpleName()); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JScrollPane scroll = new JScrollPane(new CustomComponent(), JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); window.add(scroll); window.setSize(600, 500); window.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new TestScrollPane().initUI(); } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload