I have a JPanel (yellow) located in a JScrollPane .

When you type some text in JTextPane it changes, but the vertical scrollbar is still not active. yelowPanel.getSize() returns the same value as before. `(You can see it on redPanel).

So how can I update yellowPanel ? I want to scroll the panel vertically. I tried:
panelCreating.revalidate(); panelCreating.invalidate(); panelCreating.repaint();
Only panelCreating.setPreferredSize(new Dimension(333, 777)); works panelCreating.setPreferredSize(new Dimension(333, 777)); but I donβt know what size to set. It depends on the content.
There is a small example:
package swingtest; import java.awt.Color; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextPane; public class SwingTest extends JFrame { public SwingTest() { initComponents(); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { new SwingTest().setVisible(true); } }); } private JPanel panelCenter, panelCreating; private JScrollPane scrollPaneCreating, scrollPaneCenter; private JTextPane textPane1, textPane2; private JButton button1; private void initComponents() { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setMinimumSize(new Dimension(300, 300)); panelCreating = new JPanel(); panelCreating.setMinimumSize(new Dimension(160, 200)); panelCreating.setPreferredSize(new Dimension(160, 200)); scrollPaneCreating = new JScrollPane(panelCreating, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); textPane1 = new JTextPane(); textPane1.setText("a\na"); textPane2 = new JTextPane(); textPane2.setText("b\nb"); button1 = new JButton("+++"); panelCenter = new JPanel(); panelCenter.setBackground(Color.blue); scrollPaneCenter = new JScrollPane(panelCenter);
The yellow bar also uses the GridBagLayout. sorry for my English
alaster
source share