Home arrow static arrow Java Programming [Archive] - JScrollPane viewport size question
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - JScrollPane viewport size question
This topic has 2 replies on 1 page.

Posts:85
Registered: 4/23/03
JScrollPane viewport size question  
Jul 15, 2004 2:32 PM



 
Hi all,

I've a component that is very large (it has a dinamic height) but its width is always the same (fixed). I need to show this component in a JScrollPane container and only shows the vertical scrollbar. It works fine but when my applications runs, the component inside the JScrollPane (yes..my component) get a different width, which is larger than the original componet's width

I'm using the following code:
        JScrollPane scroll = new JScrollPane(myComponent);        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);


My question is, How I can do for set a fixed width to the JScrollPane (its viewport or its child component) in order to get the correct component width and only allows a dinamic height ?

thanks in advance
 

Posts:8,813
Registered: 10/4/00
Re: JScrollPane viewport size question  
Jul 15, 2004 2:53 PM (reply 1 of 2)



 
import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.util.Random;public class Test3 extends JFrame {  JPanel jp = new JPanel();  JScrollPane jsp = new JScrollPane(jp);  JLabel jl = new JLabel("", JLabel.CENTER);  Random rand = new Random();  public Test3(){    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    Container content = getContentPane();    content.add(jl, BorderLayout.NORTH);    jp.setBorder(BorderFactory.createLineBorder(Color.blue));    jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);    jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);    content.add(jsp, BorderLayout.CENTER);    JButton jb = new JButton("Random Height");    content.add(jb, BorderLayout.SOUTH);    jb.addActionListener(new ActionListener() {      public void actionPerformed(ActionEvent ae) {        int height = rand.nextInt(1000);        jp.setPreferredSize(new Dimension(jsp.getViewport().getWidth(),height));        jsp.getViewport().revalidate();        jl.setText(""+height);      }    });    setSize(300,300);  }  public static void main(String[] args) {new Test3().setVisible(true); }}
 

Posts:85
Registered: 4/23/03
Re: JScrollPane viewport size question  
Jul 15, 2004 3:11 PM (reply 2 of 2)



 
thanks a lot for your answer

its a nice solution, my little problem is that I'm using JDK 1.4 and I can't add a ActionListener to a panel.

do you know what i can do ?

thanks
 
This topic has 2 replies on 1 page.