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
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.
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).
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.
could u give me the code to add.
JScrollPane jScrollPane = new JScrollPane(drawPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
Thanks in advance..
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....
We'd be better off not guessing here. Please show your latest code attempt.
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.
For instance, an SSCCE:
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()
protected void paintComponent(Graphics 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);
JScrollPane scrollPane = new JScrollPane(linePanel);
mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
public JComponent getComponent()
private static void createAndShowUI()
JFrame frame = new JFrame("DrawCanvas2");
public static void main(String args)
public void run()
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.
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.