JScrollPane with fixed width

Viewed 4882

I am newbie in Java Swing and I am confused about next code.

My goal is make vertical scrollable panel with 2 JTextPane(s) inside it. The first JTextPane with fixed width 70 % of parent panel and the second JTextPane with fixed width 30 %. Because the both JTextPane(s) have fixed width, they expand with more text only vertically.

I use this solution, because I want to have only one scrollbar for this 2 JTextPane(s).

My init code:

private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 616, 374);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new BorderLayout(0, 0));

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    frame.getContentPane().add(scrollPane);

    JPanel panel = new JPanel();
    scrollPane.setViewportView(panel);
    SpringLayout sl_panel = new SpringLayout();
    panel.setLayout(sl_panel);

    JTextPane leftTextPane = new JTextPane();
    sl_panel.putConstraint(SpringLayout.NORTH, leftTextPane, 10, SpringLayout.NORTH, panel);
    sl_panel.putConstraint(SpringLayout.WEST, leftTextPane, 10, SpringLayout.WEST, panel);
    panel.add(leftTextPane);

    JTextPane rightTextPane = new JTextPane();
    sl_panel.putConstraint(SpringLayout.NORTH, rightTextPane, 10, SpringLayout.NORTH, panel);
    sl_panel.putConstraint(SpringLayout.WEST, rightTextPane, 10, SpringLayout.EAST, leftTextPane);
    sl_panel.putConstraint(SpringLayout.EAST, rightTextPane, -10, SpringLayout.EAST, panel);
    panel.add(rightTextPane);

    scrollPane.addComponentListener(new ComponentAdapter() 
    {
        public void componentResized(ComponentEvent evt) {
            sl_panel.putConstraint(SpringLayout.EAST, leftTextPane, (int)(scrollPane.getWidth() * 0.7), SpringLayout.WEST, (Component)(evt.getSource()));
        }
    });
}

JTextPane(s) have no constraint for SOUTH, so they can grow up in this way.

Problems:

  • JTextPane(s) resize only after insert some text into them.
  • The vertical scrollbar is not working.
1 Answers
Related