This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Swing / AWT / SWT and the fly likes JScrollPane: Change of the size of content Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JScrollPane: Change of the size of content" Watch "JScrollPane: Change of the size of content" New topic

JScrollPane: Change of the size of content

Kilian Weihrauch

Joined: Sep 12, 2003
Posts: 3
I have problems with the JScrollPane and JPanel. I would like to design a table in a JPanel. This JPanel is again in a JScrollPane. If the number of lines of the table changes for example, then the size of the JPanel changes. My problem: the size of the JScrollPane does not change.
Thank you for your assistance.
Greetings, Kilian
I shortened the Class 'Row' a little bit.
/* */
import java.awt.*;
import javax.swing.*;
public class TestEnv extends JFrame {
public TestEnv() {
addWindowListener(new WindowClosingAdapter());
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
JPanel doccontainer = new JPanel();
JButton insertlineup = new JButton("test");
cp.add("North", insertlineup);
DocTable doctab = new DocTable();
cp.add("Center", doccontainer);
public static void main(String[] args) {
TestEnv frame = new TestEnv();
frame.setLocation(100, 100);
frame.setSize(800, 400);
/* */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DocTable extends JPanel {
public DocTable() {
setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
JPanel tabcontainer = new JPanel();
tabcontainer.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
tabcontainer.add(new Row(1));
tabcontainer.add(new Row(2));
tabcontainer.add(new Row(3));
tabcontainer.add(new Row(4));
tabcontainer.add(new Row(5));
tabcontainer.add(new Row(6));
tabcontainer.add(new Row(7));
tabcontainer.setSize(new Dimension(635,150));
tabcontainer.setPreferredSize(new Dimension(635,150));
JScrollPane scroll = new JScrollPane(tabcontainer,
scroll.setPreferredSize(new Dimension(700,100));
setSize(700, 100);
public Dimension getPreferredSize() {
return new Dimension(700,100);
/* */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class Row extends JPanel implements KeyListener {
private int tmp_lines = 1;
private Font tmp_font = new Font("Arial", Font.PLAIN, 14);
private JPanel tabcontainer = new JPanel();
private JCheckBox lfdnr = new JCheckBox("");
private JTextField anzahl = new JTextField("25,00");
private JComboBox einheiten = new JComboBox(einheitenliste);
private JTextPane beschreibung = new JTextPane();
private JTextField einzelpreis = new JTextField("2,00");
private JTextField gesamtpreis = new JTextField("50,00");
static final String[] einheitenliste = {"", "Std", "Stk", "Rol", "cm", "m", "qm"};
public Row(int index) {
this.tmp_index = index;
public Dimension getPreferredSize() {
int size = 21;
int newsize = getFontMetricsHeight() * tmp_lines + 2;
if (newsize > 21) size = newsize;
return new Dimension(635,size);

public Dimension getMinimumSize() {
return new Dimension(635,21);
private int getFontMetricsHeight() {
FontMetrics fontMetrics = getFontMetrics(tmp_font);
return fontMetrics.getHeight();
private void init() {
lfdnr.setBounds(4, 1, 41, 19);
anzahl.setBounds(45, 0, 65, 21);
einheiten.setBounds(110, 0, 50, 21);
String url = "file://D:/Projekte geplant/JMultiGrid/elefant.htm";
beschreibung.setBounds(160, 0, 345, 21);
einzelpreis.setBounds(505, 0, 65, 21);
gesamtpreis.setBounds(570, 0, 65, 21);
private int getRealLineCount (JTextPane text) {
int fontHeight = getFontMetricsHeight();
int lineCount;
try {
int height = text.modelToView(text.getDocument().getEndPosition().getOffset()-1).y;
lineCount = height/fontHeight +1;
} catch (Exception e) {
System.out.println("bad location");
lineCount = 0;
return lineCount;
public void keyPressed(KeyEvent event) {}
public void keyReleased(KeyEvent event) {
int newlines = getRealLineCount(beschreibung);
if (newlines != tmp_lines) {
tmp_lines = newlines;
setPreferredSize(new Dimension(635, getFontMetricsHeight()*newlines+2));
setSize(500, getFontMetricsHeight()*newlines+2);
beschreibung.setSize(345, getFontMetricsHeight()*newlines+2);
public void keyTyped(KeyEvent event) {}
Grub de Bliek

Joined: Sep 09, 2003
Posts: 19
what exactly do you mean? I've looked at your code and this is what I think you want:
You want to be able to add rows without the size of the JScrollPane to change, because you have a vertical scrollbar.
You want to be able to add a column, and because you don't want a horizontal scrollbar you want the JScrollPane to resize it's Viewport horizontally. Right?
Resizing a Scrollpane's viewport is not usual, in most cases a scrollpane's content has an unknown size and you want to be able to view it all in a viewport of a constant size. So, when you change the content of a JScrollPane from the INSIDE(by adding a row to the tablemodel), nothing will happen because then it'll just modify it's scrollbars so the user can see the content is larger.
But it must be possible to change the size of this viewport. It must look something like this:
addColumn(blablabla) {
// regular add colum to tablemodel stuff
Dimension newsize = new Dimension(// new size);
//retrieve new size from tablemodel, or use oldsize + size of newcolumn
JViewport vp = scrollpane.getViewport();
maybe you'll need a call to revalidate() at the end when Swing doesn't update it's look immediately. I always put it at the end of such a method, and remove it when it works, because I never know when it's necessary and when not...
[ September 16, 2003: Message edited by: Gerben Vermoen ]

When the compiler isn't happy, nobody is happy...
Kilian Weihrauch

Joined: Sep 12, 2003
Posts: 3
Hello Gerben,
thank you for your answer. My problem is a little bit different, your solution could however nevertheless function. I will test it afterwards.
Again, my plan:
* a table in a JPanel
* the JPanel in a JScrollPane with a vertical Scrollbar
* the height of the table changes by adding a new line
* the width of the table does not change
The problem:
After changing the height of the JPanel, the Scrollbar of the JScrollPane is not adapted, i.e. you see always exactly 635 pixels (= vertical size of the JPanel with the initialization of the JScrollPane) even if the JPanel is larger.
Greetings, Kilian
Kilian Weihrauch

Joined: Sep 12, 2003
Posts: 3
I found the error: after the change of the height of the table or a row, naturally also the height of the JPanel tabcontainer had to be adapted.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: JScrollPane: Change of the size of content