This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques and have Mario Casciaro & Luciano Mammino on-line!
See this thread for details.
Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Help with JScrollPane

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone !
I've developed an application to draw workflows (basically just boxes connected with arrows but hey ! it's a workflow !). These drawings are placed onto a panel which is placed on a JScrollPane. To enable scrolling when a box is dragged outside the visible area I use the following code:

The above code was placed in the mouseReleased method and worked fine.
I have recently added a save/load feature for the diagrams, so that a user can load a previously created diagram. The problem is the diagram loads properly but even if it was larger than the actual screen the the scrollbars did not appear (which is normal, because no mouseReleased event occured). Then I put the same code with minor modifications in the paintComponent method of the panel:

Now the scrollbars appear but I cannot use them, because the panel keeps on scrolling to the "farthest" box on the panel.
Could anyone help me please, i'm new in swing !
Thanks,
DAN
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Putting your validation code in paintComponent means that it will be called at any time and could result in a undesirable behavior; beyond your control at a minimum.

The JScrollPane checks all of its children and asks for their preferred sizes before it lays them out. So all we need to do is arrange for a suitable Dimension to be returned form our component whenever it is asked for its PS. Whenever changes are made to the GUI (such as adding/removing components or changing graphic boundries) we signal the JScrollPane to lay out its children again by calling revalidate on the child component of its view.
So the technique is:
1 - update size parameters of our component
2 - call revalidate to signal the JScrollPane to do a new layout.

This is taken care of automatically by any scrollpane children who implement Scrollable such as JTextArea.

Here's a demo app. You can drag the rectangle past the boundries of its container to trigger scrollpane updates.

[ January 29, 2004: Message edited by: Craig Wood ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic