My Canvas is on Top of My Scroller

Hi, I've been trying to right the code for a function grapher in netBeans.

I have the grapher class which extends java.awt.Canvas and draws a coordinate plane using its paint method. This grapher class is placed inside a scrollerPane with the following code:

graphScroller =new javax.swing.JScrollPane();

graph =new Grapher(width, height);

graphScroller.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

graphScroller.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);

graphScroller.setViewportView(graph);

org.jdesktop.layout.GroupLayout graphPanelLayout =new org.jdesktop.layout.GroupLayout(graphPanel);

graphPanel.setLayout(graphPanelLayout);

graphPanelLayout.setHorizontalGroup(

graphPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(graphScroller, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 684, Short.MAX_VALUE)

);

graphPanelLayout.setVerticalGroup(

graphPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)

.add(graphScroller, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE)

);

Whenever the size of the grapher is less than the scroller everything works fine, but when, for instance, the width is greater than the width of the scroller, the grapher extends past and covers up anything else in the program. It does, however, move back and forth whenever the scrollbar is moved. Is there some way to set like a z-index or something so that it is behind all the other elements? Thanks.

[1750 byte] By [godofbpa] at [2007-11-27 8:28:52]
# 1
What is Grapher extending?
Hippolytea at 2007-7-12 20:18:59 > top of Java-index,Java Essentials,New To Java...
# 2
java.awt.Canvas
godofbpa at 2007-7-12 20:18:59 > top of Java-index,Java Essentials,New To Java...
# 3
> java.awt.CanvasBad. You are mixing AWT and Swing. Use JPanel or JComponent instead.
Hippolytea at 2007-7-12 20:18:59 > top of Java-index,Java Essentials,New To Java...
# 4
I replaced Canvas with JComponent, but in a JComponent, does the setSize(int width, int height) method still work the same, because now the scroll bars don't even work at all it seems.Message was edited by: godofbp
godofbpa at 2007-7-12 20:18:59 > top of Java-index,Java Essentials,New To Java...
# 5

I don't know about GroupLayout, but with normal layout managers, you

either override getPreferredSize (and perhaps getMinimumSize and getMaximumSize) or call the corresponding get* methods. I never call setSize, because that is called by proper layout managers on you behalf:

import java.awt.*;

import javax.swing.*;

public class ComponentExample extends JComponent implements Runnable {

public void run() {

JComponent comp = new ComponentExample();

comp.setPreferredSize(new Dimension(600,400));

JFrame f = new JFrame();

f.getContentPane().add(new JScrollPane(comp));

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setSize(400, 300);

f.setLocationRelativeTo(null);

f.setVisible(true);

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

int w = getWidth();

int h = getHeight();

g.setColor(Color.WHITE);

g.fillRect(0, 0, w, h);

g.setColor(Color.GREEN);

g.drawLine(0, 0, w, h);

g.drawLine(0, h, w, 0);

}

public static void main(String[] args) {

EventQueue.invokeLater(new ComponentExample());

}

}

Hippolytea at 2007-7-12 20:18:59 > top of Java-index,Java Essentials,New To Java...
# 6
Thanks a ton!Message was edited by: godofbp
godofbpa at 2007-7-12 20:18:59 > top of Java-index,Java Essentials,New To Java...