?
Solved

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

Posted on 2003-03-12
13
Medium Priority
?
299 Views
Last Modified: 2010-03-31
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();

    }
}
0
Comment
Question by:luh1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +4
13 Comments
 
LVL 1

Expert Comment

by:pronane
ID: 8120991
what are you trying to do?
0
 
LVL 2

Expert Comment

by:antonsigur
ID: 8121057
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
 
LVL 2

Expert Comment

by:antonsigur
ID: 8121065
Other policies are:

JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED //default
JScrollPane.VERTICAL_SCROLLBAR_NEVER
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 35

Expert Comment

by:TimYates
ID: 8123277
I think :

    scroll.add(panel);

should be:

    scroll.getViewport().add(panel);

I may be wrong though...
0
 
LVL 9

Expert Comment

by:yongsing
ID: 8124962
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
 

Author Comment

by:luh1
ID: 8126556
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
 
LVL 2

Expert Comment

by:antonsigur
ID: 8128426
>>    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
 

Author Comment

by:luh1
ID: 8134715
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
 
LVL 2

Accepted Solution

by:
antonsigur earned 160 total points
ID: 8135213
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
 
LVL 1

Expert Comment

by:pronane
ID: 8137010
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
 

Expert Comment

by:CleanupPing
ID: 9058737
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
 
LVL 35

Expert Comment

by:girionis
ID: 9714432

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

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month9 days, 18 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question