Results 1 to 2 of 2
  1. #1
    nidhirastogi is offline Member
    Join Date
    Jul 2008
    Posts
    16
    Rep Power
    0

    Default scroll a Jpanel in a JFrame

    Hi All
    I need to make a JPanel inside a JFrame Scrollable. I have implemented a hexagonal grid in the Jframe and need to scroll it horizontally n vertically.
    Plz. help.

    My JPanel Code is :

    public class DisplayPanel extends JPanel
    {
    private static final long serialVersionUID = 1L;
    private int xMax=1000, yMax=1000;
    // public Point2D listAP;

    // DistancefromMN distMN = new DistancefromMN();
    JLabel label;

    class MyAdjustmentListener implements AdjustmentListener
    {
    public void adjustmentValueChanged(AdjustmentEvent e) {
    repaint();
    }
    }

    public DisplayPanel(int xMax,int yMax)
    {
    super(true);
    this.xMax=xMax;
    this.yMax=yMax;
    setLayout(new BorderLayout());
    setLayout(new BorderLayout());

    JScrollBar hbar = new JScrollBar(
    JScrollBar.HORIZONTAL, 30, 20, 0, 300);
    JScrollBar vbar = new JScrollBar(
    JScrollBar.VERTICAL, 30, 40, 0, 300);

    hbar.setUnitIncrement(2);
    hbar.setBlockIncrement(1);

    hbar.addAdjustmentListener(new MyAdjustmentListener());
    vbar.addAdjustmentListener(new MyAdjustmentListener());

    add(hbar, BorderLayout.SOUTH);
    add(vbar, BorderLayout.EAST);

    }
    }


    and JFrame:

    public class DisplayFrame extends JFrame
    {
    private static final long serialVersionUID = 1L;
    private int maxX,maxY;

    public DisplayFrame()
    {
    setLayout(new BorderLayout());
    setTitle("Wireless Environment");
    setSize(1290, 1290);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Toolkit toolkit = getToolkit();
    // JScrollBar hbar = new JScrollBar(JScrollBar.HORIZONTAL, 30, 20, 0, 300);
    // JScrollBar vbar = new JScrollBar(JScrollBar.VERTICAL, 30, 40, 0, 300);
    // vbar.addAdjustmentListener(new MyAdjustmentListener());
    // add(hbar, BorderLayout.SOUTH);
    // add(vbar, BorderLayout.EAST);
    DisplayPanel p=new DisplayPanel(maxX,maxY);
    this.getContentPane().add(p);
    this.validate();
    setVisible(true);

    }
    class MyAdjustmentListener implements AdjustmentListener
    {
    public void adjustmentValueChanged(AdjustmentEvent e) {
    //label.setText(" New Value is " + e.getValue() + " ");
    repaint();
    }
    }
    public static void main(String args[])
    {
    new DisplayFrame();

    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I think that you posted this in the wrong forum, that you wanted to post this in the Swing forum. Oh well, it's close anyway. Question though: why not just use a JScrollPane? That should make this whole thing easy.

Similar Threads

  1. Replies: 6
    Last Post: 07-28-2008, 07:04 PM
  2. Controlling Page Scroll Position in Jsp....f there are fewer than 4 pages only that n
    By 82rathi.angara in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 07-05-2008, 03:11 PM
  3. code hint scroll bar ?
    By tryit in forum Eclipse
    Replies: 3
    Last Post: 04-28-2008, 08:07 AM
  4. Scroll thumb on the ScrollPanel
    By SteM in forum AWT / Swing
    Replies: 3
    Last Post: 12-05-2007, 07:35 PM
  5. Jtextarea and scroll
    By ziniestro in forum AWT / Swing
    Replies: 2
    Last Post: 06-01-2007, 04:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •