Board index » jbuilder » Client fills entire JScrollPane, I do NOT want this.

Client fills entire JScrollPane, I do NOT want this.


2005-07-16 10:16:00 AM
jbuilder23
Hi, I'm trying to create a "tiling" application.
I have a JPanel with a grid layout. Currently it has 3 rows and 3
columns.
I put a jlabel into each "cell". The jlabel is set to be 50 high by 50
wide.
When however I put this Jlabel into the Jscrollpane , my prefered size
for
the Jlabels is ignored and the grow so that the Jpanel fills the entire
Scrollpane viewport!
How do i get it so that this does not happen. I want my Jlabels to stay
the
size i want, and to be bunched up in the top left corner of the
Jscrollpane.
Any ideas??
P.S. I also want to dynamically resize this JPanel, and if it is
larger
than the scrollpanes viewport obviously I want it to scroll. This part
I'm
pretty sure I know how to do (delete the old Jpanel then create a new
of the
appropriate number of rows/cols, then add it to the Jscollpane).
Here is the code for the JPanel derived component (if you look you will
see
that i actually add the MapPanel component to a vanilla Jpanle with a
gridbad layout. This was an attempt to solve the problem, but only half
worked - i could get the right size vertically or horizontally but not
both)
----------------------------------------
public class MapPanel
extends JPanel {
int rows = 3;
int columns = 3;
GridLayout gridLayout1 = new GridLayout();
public MapPanel() {
try {
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setLayout(gridLayout1);
gridLayout1.setColumns(columns);
gridLayout1.setRows(rows);
for (int row=0; row<rows; row++)
for (int column=0; column<columns; column++)
{
JLabel tempJLabel = new JLabel(Integer.toString(row));
tempJLabel.setBorder(BorderFactory.createEtchedBorder());
tempJLabel.setMaximumSize(new Dimension(50, 50));
tempJLabel.setMinimumSize(new Dimension(50, 50));
tempJLabel.setPreferredSize(new Dimension(50, 50));
this.add(tempJLabel);
}
this.setMaximumSize(new Dimension(rows * 50, columns * 50));
this.setMinimumSize(new Dimension(rows * 50, columns * 50));
this.setPreferredSize(new Dimension(rows * 50, columns * 50));
}
}
--------------------------------------
Here is where I add it to the scrollpane
JScrollPane jScrollPane1 = new JScrollPane();
JPanel SuperMapPanel = new JPanel();
GridLayout gridLayout1 = new GridLayout();
jScrollPane1.setHorizontalScrollBarPolicy(JScrollPane.
HORIZONTAL_SCROLLBAR_AS_NEEDED);
jScrollPane1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
jScrollPane1.setBorder(BorderFactory.createEtchedBorder());
jScrollPane1.setPreferredSize(new Dimension(600, 480));
SuperMapPanel.setLayout(gridLayout1);
gridLayout1.setColumns(2);
gridLayout1.setRows(2);
SuperMapPanel.add(new MapPanel());//djw hand added code
jScrollPane1.getViewport().add(SuperMapPanel);
-----------------------------------------------------
 
 

Re:Client fills entire JScrollPane, I do NOT want this.

Well, I found the answer.
In MapPanel I implemented the Scrollable interface and overrode
getPreferredScrollableViewportSize(), setting it to a large size.