wood burning stoves 2.0*
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 Murach's Java Servlets and JSP this week in the Servlets 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
Author

JScrollPane: Change of the size of content

Kilian Weihrauch
Greenhorn

Joined: Sep 12, 2003
Posts: 3
Hello,
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.
******************************************************************
/* TestEnv.java */
import java.io.*;
import java.awt.*;
import javax.swing.*;
public class TestEnv extends JFrame {
public TestEnv() {
super("Test-Environment");
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();
doccontainer.add(doctab);
cp.add("Center", doccontainer);
}
public static void main(String[] args) {
TestEnv frame = new TestEnv();
frame.setLocation(100, 100);
frame.setSize(800, 400);
frame.setBackground(Color.black);
frame.setVisible(true);
}
}
******************************************************************
/* DocTable.java */
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,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setPreferredSize(new Dimension(700,100));
add(scroll);
setSize(700, 100);
}
public Dimension getPreferredSize() {
return new Dimension(700,100);
}
}
******************************************************************
/* Row.java */
import java.io.*;
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;
init();
}
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() {
setSize(635,21);
setLayout(null);
setBorder(BorderFactory.createLineBorder(Color.black));
setBackground(Color.white);
lfdnr.setHorizontalAlignment(JCheckBox.LEFT);
lfdnr.setBackground(Color.white);
lfdnr.setBorder(BorderFactory.createLineBorder(Color.black));
lfdnr.setBounds(4, 1, 41, 19);
lfdnr.addItemListener(this);
add(lfdnr);
anzahl.setHorizontalAlignment(JTextField.RIGHT);
anzahl.setBorder(BorderFactory.createLineBorder(Color.black));
anzahl.setBounds(45, 0, 65, 21);
add(anzahl);
einheiten.setBackground(Color.white);
einheiten.setBorder(BorderFactory.createEmptyBorder());
einheiten.setBounds(110, 0, 50, 21);
add(einheiten);
String url = "file://D:/Projekte geplant/JMultiGrid/elefant.htm";
beschreibung.setText("TEST");
beschreibung.setEditable(true);
beschreibung.setBounds(160, 0, 345, 21);
beschreibung.setBorder(BorderFactory.createLineBorder(Color.black));
beschreibung.addKeyListener(this);
add(beschreibung);
einzelpreis.setHorizontalAlignment(JTextField.RIGHT);
einzelpreis.setBorder(BorderFactory.createLineBorder(Color.black));
einzelpreis.setBounds(505, 0, 65, 21);
add(einzelpreis);
gesamtpreis.setHorizontalAlignment(JTextField.RIGHT);
gesamtpreis.setBorder(BorderFactory.createLineBorder(Color.black));
gesamtpreis.setBounds(570, 0, 65, 21);
add(gesamtpreis);
setVisible(true);
}
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
Greenhorn

Joined: Sep 09, 2003
Posts: 19
Hey,
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) {
//blablabla
// regular add colum to tablemodel stuff
//blablabla
Dimension newsize = new Dimension(// new size);
//retrieve new size from tablemodel, or use oldsize + size of newcolumn
JViewport vp = scrollpane.getViewport();
vp.setViewSize(newsize);
revalidate();
}
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
Greenhorn

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
Greenhorn

Joined: Sep 12, 2003
Posts: 3
Hello,
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JScrollPane: Change of the size of content
 
Similar Threads
Alignment Issue
Too many columns slowing down program
problem in JTable
Spreading out JLabels in a panel
Turning JTree into a layout manager, resizing nodes