JScrollPane now showing its viewport
-
19-08-2019 - |
Question
I am making an application with Java Swing and i have a problem. I have made a Tabbed Panel, which need to hold a simple panel, and a scroll-panel. The simple panel is working fine but in my scroll-panel i can only see the scrollbars, but not the viewport, my code is as follows:
ContentPane
public class ContentPane extends JTabbedPane {
private InfoPanel ip;
ScrollPanel sp;
public InfoPanel getIp() {
return ip;
}
public ContentPane(GraphPanel gp) {
this.sp = new ScrollPanel(gp);
this.sp.setViewportView(gp);
this.addTab("Graph", this.sp);
this.ip = new InfoPanel(gp);
this.addTab("Info", ip);
}
}
ScrollPanel
public class ScrollPanel extends JScrollPane {
public ScrollPanel(GraphPanel gp){
this.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
this.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.repaint();
}
}
GraphPanel
public GraphPanel(StatusBar sb) {
this.sb = sb;
zoomLevel = 5;
sm = new Simulation();
mh = new MouseHandler(this, sm);
this.addMouseListener(mh);
this.setBackground(new Color(240, 165, 98));
this.repaint();
}
Since i don't get any errors or exceptions, i am now completely lost in which aproach to take.
Solution
You should not subclass JScrollPane, it is not necessary.
But if you do so, don't forget to add the component to the scrollpane.
In your subclass you are not adding the GraphPanel to the scroll pane.:
public class ScrollPanel extends JScrollPane {
public ScrollPanel(GraphPanel gp){
// :::: HERE ::: you are not doing anything with gp
// like this.setViewPort( gp ) or something like that
this.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
this.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.repaint();
}
}
Try:
public class ScrollPanel extends JScrollPane {
public ScrollPanel(GraphPanel gp){
super( gp );
.... etc ...
And have GraphPanel extend JComponent or JPanel
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow