-
Using JScrollbar on a JComponent
Hi,
I have a JComponent (fixed size) that shows graphics. Now I want the user to scroll the content of the component (not scroll the actual componen though)
I cant use a JScrollPance because i cant hold all the graphics in memory. I just want the user to see and use the scrollbars, capture the events and direct them to my graphics engine.
I tried JComponent.add() and JComponent.getrootPane().add() but the scrollbars dont want to show...same for the events
Any ideas how it can be done? Thanks!
-
What scrollbars are you expecting to see ? A JComponent has no hidden scrollbar
feature.
If You use a JScrollPane the bar(s) will show if the panel you add to it exceeds the scrollpane size, and you invoke its method for user interface update.
eschew obfuscation
-
I tried to add normal JScrollBar 's to my jcomponent
here is what im trying to do:
class MyClass extends JComponent{
JScrollBar jsb;
MyClass()
{
jsb = new Scrollbar(JScrollbar.HORIZONTAL, 0,10,0,100);
this.add(jsb);
}
}
and the MyClass is added to the rootPane of my JFrame (Main Application)
...
MyClass mc = new MyClass();
this.getRootPane().add(mc);
...
Now after this i qould expect the ScrollBars to show up, but hey dont...
Makes no difference weather i setsize or setpreferredsize etc.
?!?
-
This should solve some problem(s) ?
Code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ScrollFrame extends JFrame implements AdjustmentListener {
BorderLayout borderLayout1 = new BorderLayout();
JScrollBar vScroll = new JScrollBar();
JScrollBar hScroll = new JScrollBar();
public ScrollFrame() {
try {
jbInit();
validate();
}
catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ScrollFrame sf = new ScrollFrame();
sf.setBounds(20,20,600,500);
sf.setVisible(true);
sf.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private void jbInit() throws Exception {
this.getContentPane().setLayout(borderLayout1);
hScroll.setOrientation(JScrollBar.HORIZONTAL);
this.getContentPane().add(vScroll, BorderLayout.EAST);
this.getContentPane().add(hScroll, BorderLayout.SOUTH);
vScroll.addAdjustmentListener(this);
hScroll.addAdjustmentListener(this);
}
public void adjustmentValueChanged(AdjustmentEvent e) {
if (e.getSource()==vScroll) {
System.out.println("vertical:"+e.getValue());
} else if (e.getSource()==hScroll) {
System.out.println("horizontal:"+e.getValue());
}
}
}
If you place your image display panel in CENTER as a JPanel extension you
could modify some drawing coordinates and do a repaint on each invokation
of the adjustmentValueChanged method.
eschew obfuscation
-
Hi, that helped, its working now =)
Big thanks !
Similar Threads
-
Replies: 3
Last Post: 04-05-2005, 09:19 AM
-
Replies: 1
Last Post: 09-12-2000, 01:35 PM
-
By Alan Shiers in forum Java
Replies: 0
Last Post: 05-17-2000, 01:06 PM
-
By Alan Shiers in forum Java
Replies: 0
Last Post: 05-17-2000, 01:00 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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks