views:

896

answers:

1

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.

+2  A: 

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

OscarRyz
I added the graphpanel as viewport in the ContentPane class, and i tried this too, but it still doesn't work.
utdiscant
oohh , yeap I have just seen that part... let me try other thing.
OscarRyz
Dude, that worked, can you in a few words explain what i did wrong? Thank you so much :)
utdiscant
Oh and also, my scrollpanel doesn't really know when it needs to scroll, where should i put an option in for this?
utdiscant
Look again, did it really worked ? Because probable the other tab is not working very well.
OscarRyz
The other tab is working fine, it is not supposed to be a scroll-panel, just a simple panel. But the graph panel is still not scrolling when the graph is bigger than the scrollpanel..
utdiscant
I have just modified the code and it scrolls well. I don't know what the graph panel but I have added a JLabel see: http://pastebin.com/f5f69fe80
OscarRyz
One thing is true, if GraphPanel is a JComponent ( or a subclass ) and you try to render in two different places ( say GraphTab and InfoTab ) it will show well only in one of them.
OscarRyz
Okay, by using that i have a scrollpanel that will scroll, but the panel will be on top of the graph, and my graph is a variable size, do i need to specify the minimumsize of the graphpanel somewhere?
utdiscant
Finally, see the if you're defining preferredSize and/or minimSize. That's what the JScrollPane takes to know when to show the scrolls bar.
OscarRyz
"do i need to specify the minimumsize " yeap, I think prefferredSize is enough, that is what the JScoll takes to know if it should or not show the scrolls, "How much is visible and how much should it scroll"
OscarRyz
If your graph is variable size you'll have to implement the Scrollable Interface that is a little bit tricky.
OscarRyz
I think i just found a solution with just varying the PreferredSize when i change something in the GraphPanel
utdiscant
Yeap. In the code in pastebin, notice the JLabel has a big preffered size... :) I'm glad you have make this thing to work.
OscarRyz
I am glad you helped me make this thing work :). About that Scrollable interface, how difficult is it? Cause this approach i took, it is not working :P
utdiscant
Well let say I tried once .... aaand.. gave up :P
OscarRyz
I have implemented the methods in the interface now, but they don't seem to ever be called :/
utdiscant