1

I want to resize JScrollPane programmatically. I do not want to resize viewport of JScrollPane but JScrollPane itself together with parent JDialog.

So far I tried to setPreferredSize of JScrollPane and revalidate it but it does not work. Here is my code that creates JScrollPane and adds it into JDialog.

public ConnectionTreeTooltip(MainFrame parent) {
        super(parent, "", false);
        super.setUndecorated(true);
        JPanel contentPane = (JPanel) super.getContentPane();
        contentPane.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED, Color.blue, Color.red));
        tipLabel = new JLabel();
        tipLabel.setBackground(Color.WHITE);
        scrollPane = new JScrollPane(tipLabel);
        scrollPane.setPreferredSize(new Dimension(100, 100));
        scrollPane.setBorder(BorderFactory.createEmptyBorder(3, 3, 1, 1));
     // super.setLayout(new BorderLayout());
        super.getContentPane().add(scrollPane);
        super.pack();
    }

So I have my JScrollPane created all right and its viewport is automatically resized whenever text of JLabel changes.

How to resize JScrollPane itself together with parent JDialog?

Thank you!

Volodymyr Levytskyi
  • 3,364
  • 9
  • 46
  • 83

1 Answers1

0

It is resolved already!

I only had to call pack() method on JDialog after I set preferredSize of JScrollPane. Like so:

scrollPane.setPreferredSize(tempLabel.getPreferredSize());
super.pack();
Volodymyr Levytskyi
  • 3,364
  • 9
  • 46
  • 83
  • 2
    Instead, override `getPreferredSize()`, for [example](http://stackoverflow.com/a/14011536/230513). – trashgod Aug 31 '13 at 15:18