Thread View: borland.public.jbuilder.java.swing
2 messages
2 total messages
Started by chocawok
Sat, 16 Jul 2005 02:16
Client fills entire JScrollPane, I do NOT want this.
Author: chocawok
Date: Sat, 16 Jul 2005 02:16
Date: Sat, 16 Jul 2005 02:16
96 lines
4088 bytes
4088 bytes
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.
Author: chocawok
Date: Sun, 17 Jul 2005 19:19
Date: Sun, 17 Jul 2005 19:19
4 lines
1288 bytes
1288 bytes
Well, I found the answer. In MapPanel I implemented the Scrollable interface and overrode getPreferredScrollableViewportSize(), setting it to a large size.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads