Results 1 to 10 of 10

Thread: scrollbar issue

  1. #1
    kumar_gemi is offline Member
    Join Date
    Oct 2008
    Posts
    21
    Rep Power
    0

    Default scrollbar issue

    Hello,

    I do have a jpanel to which I am adding a scrollpane. Everything is working fine till that point. I do have a set of codes which draws vertical lines and if I do move the scollbar then nothing happens.

    Here comes my issue. I am also drawing some horizontal lines. When I drag the knob of the scrollbar the horizontal lines get erased leaving the vertical lines alone.

    I tried my best to figure out the issue but couldn't solve it. Any help would be appreciated.

    I have also attached a partial code along with this thread.

    Thanks in advance
    gemi
    Last edited by kumar_gemi; 05-06-2009 at 07:14 AM.

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

    Default

    Multi-post in the Netbeans forum has been closed. I will ask that the original poster not to post the same question in multiple fora.
    Last edited by Fubarable; 05-03-2009 at 11:30 PM.

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

    Default

    One problem I see is that you may be mixing Swing and AWT components in that you appear to be using a ScrollPane not a JScrollPane here. I don't know if changing this will fix the program as I don't have access to a Java compiler, but you may wish to change this and see what happens. If you do change this, note that you will need to add your drawing panel to the JScrollPane's view port, not to the JScrollPane itself (an exception is made for adding the JPanel in the JScrollPane's constructor).
    Last edited by Fubarable; 05-03-2009 at 11:29 PM.

  4. #4
    kumar_gemi is offline Member
    Join Date
    Oct 2008
    Posts
    21
    Rep Power
    0

    Default

    I am really sorry. I didn't know that. I tried using jscrollpane but the scrollbar is not added properly. I used the following code.

    Now its not even drawing the vertical or horizontal lines.

    Java Code:
    JScrollPane jScrollPane = new JScrollPane(drawPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
           jScrollPane.add(new DrawCanvas());
           jScrollPane.setSize(925,740);
           
    
          add("left",jScrollPane);
    could u give me the code to add.

    Thanks in advance..

  5. #5
    kumar_gemi is offline Member
    Join Date
    Oct 2008
    Posts
    21
    Rep Power
    0

    Default

    still its not working....could anyone guide me further on how to proceed with it....I even tried adding it to Jscollpane viewport ...but still not working....

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

    Default

    We'd be better off not guessing here. Please show your latest code attempt.

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

    Default

    I'm not able to help based on that code since it is somewhat large and has a lot of dependencies that I don't have access to. You may want to consider creating a small compilable and runnable program that demonstrates your problem, otherwise known as a Short, Self Contained, Correct (Compilable), Example. If you create and post this (and yes, it will take a bit of effort on your part to do so), we'll be able to run your code, see your error and then manipulate your code. It will increase your chances of getting useful help umpteen-fold.

    Best of luck.

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

    Default

    For instance, an SSCCE:
    Java Code:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JComponent;
    import javax.swing.JScrollPane;
    
    public class DrawCanvas2
    {
      private static final Dimension LINE_PANEL_SIZE = new Dimension(2000, 2000);
      private static final Dimension SCROLLPANE_SIZE = new Dimension(800, 600);
      protected static final int DELTA = 20;
      private JPanel mainPanel = new JPanel();
      private JPanel linePanel = new JPanel()
      {
        @Override
        protected void paintComponent(Graphics g)
        {
          super.paintComponent(g);
          int width = getWidth();
          int height = getHeight();
          
          for (int x = 0; x < width / DELTA; x++)
          {
            g.setColor((x % 2 == 0) ? Color.black : Color.red);
            g.drawLine(x * DELTA, 0, x * DELTA, height);
          }
          for (int y = 0; y < height / DELTA; y++)
          {
            g.setColor((y % 2 == 0) ? Color.black : Color.red);
            g.drawLine(0, y * DELTA, width, y * DELTA);
          }
          
        }
      };
    
      public DrawCanvas2()
      {
        linePanel.setPreferredSize(LINE_PANEL_SIZE);
        JScrollPane scrollPane = new JScrollPane(linePanel);
        scrollPane.setPreferredSize(SCROLLPANE_SIZE);
        mainPanel.add(scrollPane);
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
      }
    
      public JComponent getComponent()
      {
        return mainPanel;
      }
    
      private static void createAndShowUI()
      {
        JFrame frame = new JFrame("DrawCanvas2");
        frame.getContentPane().add(new DrawCanvas2().getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    }

  9. #9
    kumar_gemi is offline Member
    Join Date
    Oct 2008
    Posts
    21
    Rep Power
    0

    Default

    Thank you for the code. Sorry for pasting a huge amount of code. I did as you have said ie adding JScrollPane. I debugged it by adding a print statement. Whenever I dragged the scrollbar , paint component is called by default but its not repainting. I am still working on it.

    The main issue is that I am drawing communication graph for example if a message is send from p1 to p2 I need to draw a horizontal line connecting p1 and p2. Each vertical lines indicates each process.

    I am initially drawing some vertical lines depending upon the no of processes. Then i am storing the coordinates so that I know if p1 send to p2 then I can draw line as I have teh coordinates of both p1 and p2.

    Anyways let me try for sometime and get back to you.

    Once again thanks for your help.

    kumar

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

    Default

    Perhaps you're trying to do too much inside the paint or paintComponent method, that you're tying it or the EDT up somehow.... just a guess.

Similar Threads

  1. Not another Jpanel Scrollbar problem!
    By jiexx in forum AWT / Swing
    Replies: 3
    Last Post: 03-18-2009, 03:09 AM
  2. how to add scrollbar in netbeans
    By kumar_gemi in forum NetBeans
    Replies: 1
    Last Post: 03-12-2009, 07:50 PM
  3. How to maintain scrollbar position on postback?
    By marathaWarrior in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 01-02-2009, 08:35 AM
  4. Regd the scrollbar in a table in JSP
    By itmani2020 in forum Advanced Java
    Replies: 4
    Last Post: 08-13-2008, 02:26 PM
  5. Using java.awt.Scrollbar
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-03-2008, 10:19 AM

Posting Permissions

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