Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2003-03-12
13
Medium Priority
?
302 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
  • 4
  • 2
  • 2
  • +4
12 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

581 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