Reputation: 3245
I am trying to add a scrollbar to a JApplet component. I know I shouldn't use it and should rather use JPanel, but for sake of semplicity I'll leave it like it, as in a tutorial I am following.
As you can see I tried adding a ScrollPane component, and add to it the applet. Then I add the scrollpane to the frame.
The result is that I can see a vertical scrollbar, but that it does have the ability to scrool. Actually the scroll cursor is missing. And the up and down arrows don't scrooll too. I'd like to scroll down to the part of the line I've drawn that went outside the visible area.
What am I doing wrong?
public class App {
private App() {
final int WINHSIZE = 800;
final int WINVSIZE = 600;
class MyJApplet extends JApplet {
public void init() {
setBackground(Color.black);
setForeground(Color.white);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.drawLine(0, 0, 2000, 2000);
}
}
}
JFrame f = new JFrame("Title");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
JApplet applet = new MyJApplet();
JScrollPane myScrollPane = new JScrollPane(applet, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
f.getContentPane().add("Center", myScrollPane);
applet.init();
f.pack();
f.setSize(new Dimension(WINHSIZE, WINVSIZE));
f.setVisible(true);
}
public static void main(String[] args) {
new App();
}
}
Upvotes: 0
Views: 651
Reputation: 3019
You've set them to be always visible. That means even if there is no need to scroll - they'll still appear. If you do not want to vertical scroll bar to appear, you can just do:
JScrollPane myScrollPane = new JScrollPane(applet);
Or
JScrollPane myScrollPane = new JScrollPane(applet,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
Regardless of how you implement it, the scroll bar will only be enabled and usable when it is needed. It may be visible (as your code shows), but there is no point for it to enable scrolling, so it won't allow you to.
If the size of applet
is determined to be bigger than the available size of the myScrollPane
, then the needed scroll bars will be activated. This can be done by adding more components, or overriding the setPreferredSize
, setSize
methods of the applet
as needed.
For example, this code sample shows how the scroll bar (in this case horizontal) will only be activated when needed. The rest of the time, it will be visible - but not active - which is what you are seeing.
final JFrame frame = new JFrame("Scroll Bar Test");
final JPanel content = new JPanel(new FlowLayout(FlowLayout.LEADING));
final JScrollPane scrollPane = new JScrollPane(content, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
for(int i = 0; i < 10; i++){
content.add(new JLabel("Label " + i));
}
frame.add(scrollPane);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
Upvotes: 2
Reputation: 324118
I'll leave it like it, as in a tutorial I am following.
Well your tutorial is old and you should NOT be following it. Instead you should be learning how to create a JFrame the normal way.
That is you do custom painting on a JPanel by overriding the paintComponent()
method and you add the panel to the frame. You should NOT override paint(). Read the section from the Swing tutorial (which is a far better tutorial to follow) on Custom Painting for more information. You need to make sure to override the getPreferredSize()
method so the scrollbars can work properly.
f.getContentPane().add("Center", myScrollPane);
That is not how you add a Component to a Container. You would never hardcode a constraint like that. Also you should be using:
add(component, constraint)
The BorderLayout will contain fields you can use to identify the constraint.
People don't use f.getContentPane().add(...)
anymore. Since JDK4 you can use f.add(...)
. As I said your tutorial is way out of date.
Look at the table of contents of the Swing tutorial. The examples are more up to date and will provide a better design for your application. For example you should be creating GUI components on the Event Dispatch Thread, which your code is NOT doing. Read the tutorial on Concurrency
to understand why this is important.
Upvotes: 3