Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JScrollPane with fixed width

Tags:

java

swing

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.
like image 340
user3102393 Avatar asked Mar 22 '16 19:03

user3102393


1 Answers

The problem is that a scrollpane will display a component at its preferred size and then add scroll bars as required.

In your case you want the width to be constrained by the viewport of the scrollpane.

So therefore you need to implement the Scrollable interface on the component you add to the viewport. The Scrollable interface will allow you to force the component width to match the width of the viewport which in turn will limit the width of each JTextPane causing the text to wrap.

An easy way to implement this functionality is to use the Scrollable Panel. This class implements the Scrollable interface and allows you to override the Scrollable methods by using parameters.

So the basic code would be:

ScrollablePanel panel = new ScrollablePanel( new BorderLayout());
panel.setScrollableWidth( ScrollablePanel.ScrollableSizeHint.FIT );

The first JTextPane with fixed width 70 % of parent panel and the second JTextPane with fixed width 30 %

One way to do this might be to use a JSplitPane so you have a divider between the two text panes and the text doesn't merge into one.

JSplitPane splitPane = new JSplitPane();
splitPane.setLeftComponent(new JTextPane());
splitPane.setRightComponent(new JTextPane());
splitPane.setResizeWeight(0.7);
splitPane.setDividerLocation(.7);

Then you just add everything to the frame:

panel.add(splitPane);
frame.add(new JScrollPane(panel), BorderLayout.CENTER);

Now the divider location will remain at 70% and the text panes will grow/shrink as the frame is resized.

like image 184
camickr Avatar answered Nov 02 '22 15:11

camickr