JScrollPane with many components.

To wrap a component inside a scrollpanel, I've only to do (for example):

scrollPane=new JScrollPane(a_textArea_for_Example);

So I though that to put a list of labels and buttons, I had to put them in a panel, and then do

scrollPane=new JScrollPane(the_panel);

This works when the scrollpanel size is greater than panel, if not the panel part inide the scroll is painted correctly, the panel part outside the scroll is painted outside too!!! and with scrollbars working well.

To put many component in a scrollpanel which is the correct way to do it?

Why a panel inside a scrollpanel is not working properly?

I've only created a panel, setlayout to an XYLayout, put some labels inside, and create scrollPane in the normal way scrollPane=new JScrollPane(the_panel);

What's wrong?

[835 byte] By [z80z80a] at [2007-10-3 8:23:22]
# 1
Post a small demo code that is generally compilable, runnable and could reproduce your problem. See: http://homepage1.nifty.com/algafield/sscce.html and http://www.yoda.arachsys.com/java/newsgroups.html
hiwaa at 2007-7-15 3:29:23 > top of Java-index,Desktop,Core GUI APIs...
# 2

A sample dialog, it has some panels inside other to have the same situation that my application.

It's done in jbuilder and uses a borland class for the layout.

It's a dialog with a panel, inside a jscrollpane with a panel with components, and this panel is draw outsise the jscrollpanel container.

My java version is 1.4.2.01_b6.

import java.awt.*;

import javax.swing.*;

import com.borland.jbcl.layout.*;

public class Dialog1 extends JDialog {

JPanel panel1 = new JPanel();

XYLayout xYLayout2 = new XYLayout();

XYLayout xYLayout3 = new XYLayout();

XYLayout xYLayout4 = new XYLayout();

JPanel panelinsideScroll;

JScrollPane jScrollPane;

public Dialog1(Frame frame, String title, boolean modal) {

super(frame, title, modal);

try {

this.setSize(600,400);

jbInit();

pack();

}

catch(Exception ex) {

ex.printStackTrace();

}

}

public Dialog1() {

this(null, "", false);

}

private void jbInit() throws Exception {

panel1.setLayout(xYLayout3);

this.getContentPane().setLayout(xYLayout2);

panelinsideScroll = new JPanel();

panelinsideScroll.setBackground(Color.yellow);

panelinsideScroll.setLayout(xYLayout4);

panelinsideScroll.add(new Label("label 1"), new XYConstraints(0, 0, 120, 32));

panelinsideScroll.add(new Label("label 2"), new XYConstraints(20, 50, 120, 32));

panelinsideScroll.add(new Label("label 3"), new XYConstraints(20, 150, 120, 32));

this.jScrollPane=new JScrollPane(panelinsideScroll);

panel1.add(this.jScrollPane, new XYConstraints(20, 40, 264, 97));

getContentPane().add(panel1, new XYConstraints(0, 0, 600, 400));

}

public static void main(String[] args) {

Dialog1 dlg = new Dialog1();

dlg.setModal(true);

dlg.show();

}

}

z80z80a at 2007-7-15 3:29:23 > top of Java-index,Desktop,Core GUI APIs...
# 3
The code you posted:a) isn't formatted, so its hard to readb) isn't executable since you use a custom Layout ManagerTry using a [url http://java.sun.com/docs/books/tutorial/uiswing/layout/visual.html]standard Layout Manager[/url] and see if you have better results.
camickra at 2007-7-15 3:29:23 > top of Java-index,Desktop,Core GUI APIs...
# 4

> import com.borland.jbcl.layout.*;

Post a small demo code that is generally compilable, runnable and could reproduce your problem. See: http://homepage1.nifty.com/algafield/sscce.html and http://www.yoda.arachsys.com/java/newsgroups.html

Try and study this code:

import java.awt.*;

import javax.swing.*;

public class Z80z80 extends JDialog {

JPanel panel1, panelinsideScroll;

JScrollPane jScrollPane;

JLabel label1, label2, label3;

public Z80z80(Frame frame, String title, boolean modal) {

super(frame, title, modal);

setDefaultCloseOperation(DISPOSE_ON_CLOSE);

setSize(600,400);

jbInit();

}

public Z80z80() {

this(null, "", false);

}

private void jbInit(){

panel1 = new JPanel();

panel1.setLayout(null);

getContentPane().setLayout(null);

panelinsideScroll = new JPanel();

panelinsideScroll.setBackground(Color.yellow);

panelinsideScroll.setPreferredSize(new Dimension(800, 800));

panelinsideScroll.setLayout(null);

label1 = new JLabel("label 1");

label2 = new JLabel("label 2");

label3 = new JLabel("label 3");

label1.setBounds(0, 0, 120, 32);

label2.setBounds(20, 50, 120, 32);

label3.setBounds(20, 150, 120, 32);

panelinsideScroll.add(label1);

panelinsideScroll.add(label2);

panelinsideScroll.add(label3);

jScrollPane=new JScrollPane(panelinsideScroll);

jScrollPane.setBounds(20, 40, 264, 97);

panel1.setBounds(0, 0, 600, 400);

panel1.add(jScrollPane);

getContentPane().add(panel1);

}

public static void main(String[] args) {

Z80z80 dlg = new Z80z80();

dlg.setVisible(true);

}

}

hiwaa at 2007-7-15 3:29:23 > top of Java-index,Desktop,Core GUI APIs...
# 5

Arg! I found the stupid problem, thanks for the comments and help, sorry for not formatting the source, but I cant send the sample without using Borland custom XYLayout, because it was part of the problem!!!

The problem was simply using Label objects and not JLabel.

Thanks to everybody.

z80z80a at 2007-7-15 3:29:23 > top of Java-index,Desktop,Core GUI APIs...
# 6
> XYLayout, because it was part of the problem!!!No! Absolutely not!It's just that your sub-container was so small that no scrolling was needed from the viewpoint of Swing.
hiwaa at 2007-7-15 3:29:23 > top of Java-index,Desktop,Core GUI APIs...