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) {}
}