• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

JScrollPane: Change of the size of content

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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) {}
}
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Kilian Weihrauch
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic