Results 1 to 10 of 10
Thread: scrollbar issue
- 05-03-2009, 10:00 PM #1
Member
- Join Date
- Oct 2008
- Posts
- 21
- Rep Power
- 0
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
gemiLast edited by kumar_gemi; 05-06-2009 at 06:14 AM.
-
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 10:30 PM.
-
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 10:29 PM.
- 05-04-2009, 06:21 AM #4
Member
- Join Date
- Oct 2008
- Posts
- 21
- Rep Power
- 0
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.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);
Thanks in advance..
- 05-05-2009, 07:54 PM #5
Member
- Join Date
- Oct 2008
- Posts
- 21
- Rep Power
- 0
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:
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(); } }); } }
- 05-06-2009, 06:31 AM #9
Member
- Join Date
- Oct 2008
- Posts
- 21
- Rep Power
- 0
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
-
Similar Threads
-
Not another Jpanel Scrollbar problem!
By jiexx in forum AWT / SwingReplies: 3Last Post: 03-18-2009, 02:09 AM -
how to add scrollbar in netbeans
By kumar_gemi in forum NetBeansReplies: 1Last Post: 03-12-2009, 06:50 PM -
How to maintain scrollbar position on postback?
By marathaWarrior in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 01-02-2009, 07:35 AM -
Regd the scrollbar in a table in JSP
By itmani2020 in forum Advanced JavaReplies: 4Last Post: 08-13-2008, 01:26 PM -
Using java.awt.Scrollbar
By Java Tip in forum Java TipReplies: 0Last Post: 01-03-2008, 09:19 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks