Why is my code doesn't make ScrollPane show??

Can anyone help to point out the mistakes, here's my code:  

public class tryToScroll extends JFrame{
    public tryToScroll(){
        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(100,100));
        JScrollPane scroll = new JScrollPane();
        scroll.add(panel);
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(scroll, BorderLayout.CENTER);
         pack();
        setVisible(true);
    }
    public static void main(String[] args){
     new tryToScroll();

    }
}
luh1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pronaneCommented:
what are you trying to do?
0
antonsigurCommented:
import javax.swing.*;
import java.awt.*;

public class prufa2 extends JFrame{
  public prufa2(){
      JPanel panel = new JPanel();
      panel.setPreferredSize(new Dimension(100,100));
      JScrollPane scroll = new JScrollPane();
      scroll.add(panel);

/// ***********
// Always show the scrollbar - this was missing...    
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS );
/// ***********
      getContentPane().setLayout(new BorderLayout());
      getContentPane().add(scroll, BorderLayout.CENTER);
       //pack();
      setVisible(true);
  }
  public static void main(String[] args){
   new prufa2();

  }
}
0
antonsigurCommented:
Other policies are:

JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED //default
JScrollPane.VERTICAL_SCROLLBAR_NEVER
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

TimYatesCommented:
I think :

    scroll.add(panel);

should be:

    scroll.getViewport().add(panel);

I may be wrong though...
0
yongsingCommented:
public class tryToScroll extends JFrame{
   public tryToScroll(){
       JPanel panel = new JPanel();
       //panel.setPreferredSize(new Dimension(100,100));
       //JScrollPane scroll = new JScrollPane();
       //scroll.add(panel);
       JScrollPane scroll = new JScrollPane(panel);
       scroll.setPreferredSize(new Dimension(100,100));
       scroll.setMinimumSize(new Dimension(100,100));
       getContentPane().setLayout(new BorderLayout());
       getContentPane().add(scroll, BorderLayout.CENTER);
       pack();
       setVisible(true);
   }
   public static void main(String[] args){
    new tryToScroll();

   }
}
0
luh1Author Commented:
the JScrollPane still doesn't show, even my drawing has exceeded the bottom of the panel. here is what i'v done.

public class Legend extends JInternalFrame{
    GraphModel gm;
    int dsID[];
    FinalGraph fg;
    JPanel cBox = new JPanel();
    JPanel card = new JPanel();
    JScrollPane sp;
    String dsTitle[];
    LegendPanel lp [];
    Color c [];

public Legend(GraphModel model,int array[],FinalGraph fg){
        super("Legend", true, true, true, true);
     gm = model;
        dsID = array;
     this.fg = fg;
        c = fg.getColorArray();
        show();
/////////////////////////////////////////////////
        sp = new JScrollPane();
        card.setLayout(new CardLayout());
        card.setPreferredSize(new Dimension(200,200));
////////////////////////////////////////////////////

          ... other codes ...
///////////////////////////////////////////////////              
        lp[0] = new LegendPanel(gm, dsID, fg, 2);
        sp = new JScrollPane(lp[0]);
        sp.setPreferredSize(new Dimension(100, 100));
        sp.setMinimumSize(new Dimension(100,100));
        card.add(sp,dsTitle[0]);

       
i did try directly put the lp[0] in the card panel, using card.add(lp[0], dsTitle[0]); and inside the class of lp i hav embeded the panel in a ScrollPane, see code below.
///////////////////////////////////////////////////
                 .... other codes....

        JComboBox cb = new JComboBox(dsTitle);

        ... codes for comboBoxListener....  
     
        cBox.add(cb);
        Container c = getContentPane();
         c.add(cBox, BorderLayout.NORTH);
         c.add(card, BorderLayout.CENTER);
    }
}

//this is how i tried to put the legendPanel lp in the SrcollPane but it doesn't seem working.

public class LegendPanel extends JPanel{
         ...other variables...
        JScrollPane sp;

public LegendPanel(GraphModel model, int array[], FinalGraph fg, int only){
     gm = model;
        dsID = array;
        this.fg = fg;
        onlyOne = only;
        c = fg.getColorArray();
        sp.setPreferredSize(new Dimension(100, 100));
        sp.setMinimumSize(new Dimension(100,100));
        sp = new JScrollPane(this);
    }
What's wrong with these codes that can never made the working srcollPane come up?? please help, thank You!
0
antonsigurCommented:
>>    sp = new JScrollPane();
       card.setLayout(new CardLayout());
       card.setPreferredSize(new Dimension(200,200));

Why do you create new JScrollPane here?

My code did show JScrollPane! Did you try it?

---
>>
public LegendPanel(GraphModel model, int array[], FinalGraph fg, int only){
    gm = model;
       dsID = array;
       this.fg = fg;
       onlyOne = only;
       c = fg.getColorArray();
       sp.setPreferredSize(new Dimension(100, 100));
       sp.setMinimumSize(new Dimension(100,100));
       sp = new JScrollPane(this);
   }

Where do you add this LegendPanel? Onto another scroll bar... hmm..
0
luh1Author Commented:
Hi antonsiqur, the tryToScroll class was for me to get an esay example to make a scrollPane, it worked on that example but didn't work on my actual program. my task is to make an JInternalFrame with Borderlayout, than hav a JComboBox at North and JPanel, which i called it 'card', with cardLayout at Center, and the LegendPanel will be generated to put onto 'card', depends on which item in ComboBox is chosen.
i tried: card.setPreferredSize(new Dimension(200,200)); then: sp.add(card); won't work.
i also tried to create a scrollPane in the LegendPanel, then put LegendPanel itself onto that sp(dont kno whether it'll work, i just had a try, but it still doesn't work):
class LegendPanel{
   ...
   JScrollPanel sp;
public LegendPanel(){
   ...
   sp.setPreferredSize(new Dimension(100, 100));
   sp.setMinimumSize(new Dimension(100,100));
   sp.add(this);
}}
did i make it clear now, if not pls feel free to tell me. Cheers!!
0
antonsigurCommented:
What is it you want to scroll? All panels?


>>  JScrollPanel sp;
public LegendPanel(){
  ...
  sp.setPreferredSize(new Dimension(100, 100));
  sp.setMinimumSize(new Dimension(100,100));
  //Try setting MaximumSize too, that way you force the size
  sp.add(this);
}}


I'm sorry, but I havn't created so complicated form.
You must have a Pane to put your JScrollPane on, then you add another pane inside your scroll pane, that pane will be scrollable, I'm sure you are doing it right..
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pronaneCommented:
The easiest way to set the view position is like this
once you receive text:


in the class constructor
class Test
{
   private JTextPane areaToUpdate = null;
   JScrollPane pane;
   JViewport port;
   Rectangle viewPortR;
   Point p;

public Test(JTextPane a, JScrollPane p)
{
    areaToUpdate = a;
    pane = p;
    port = pane.getViewport();
    viewPortR = port.getViewRect();
}
public void scrollDownAutomatically()
{
    int x, y;

    y = port.getViewSize().height;
    x = port.getViewSize().width;

    Point p = new Point(0, y);
    port.setViewPosition(p);
}

}


Its the port.setViewposition that sets the position so thta it gets the current x, y position and scrolls down to the point p.  This is the easiest and most effiecient way of doing it as you can set it to scroll to any position
0
CleanupPingCommented:
luh1:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
girionisCommented:

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points to antonsigur

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

girionis
EE Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.