简体   繁体   中英

JTextPane fit to the underlaying JPanel and JFrame

I have the following problem: 在此处输入图片说明

I have a JTextPane on the left filled with content and this JTextPane is placed into a JScrollPane . This ScrollPane is added to the BorderLayout.CENTER of a JPanel and this resulting JPanel is added to BorderLayout.CENTER of the underlaying JFrame contentPane() .

Now I want to spread this JTextPane all over the JFrame/JPanel . How can I achieve this?

Here is the code:

public class FinishPanelBuilder extends JPanel
{
  /**
   * Serial version UID.
   */
  private static final long serialVersionUID = 4373044358025740572L;
  private Model model;

  /**
   * Constructor to call if you want to build up a new finish panel.
   * 
   * @param model
   */
  public FinishPanelBuilder()
  {
    buildFinishPanel();
  }

  /**
   * Builds up the panel.
   */
  public void buildFinishPanel()
  {
    setLayout(new BorderLayout());

    JTextWrapPane textPaneResult = new JTextWrapPane(); 
    //This is my own class of JTextPane to wrap text
    StyledDocument document = textPaneResult.getStyledDocument();

    textPaneResult.setEditable(false);
    textPaneResult.setBackground(Color.white);
    textPaneResult.setFont(panelLabel.getFont());
    textPaneResult.setAutoscrolls(true);
    textPaneResult.setLineWrap(false);

    JScrollPane scrollPaneTestResult = new JScrollPane(textPaneResult);
    scrollPaneTestResult.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));

    panelLabel.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
    setBorder(BorderFactory.createEtchedBorder());
    add(panelLabel, BorderLayout.PAGE_START);
    add(scrollPaneTestResult, BorderLayout.CENTER);
  }
}

This complete code is called by a

JFrame frame = new JFrame(); 
frame.add(new FinishPanelBuilder();

Okay, I did this again and it was okay. I don't know why. I think it is cause of a wrong method called getScrollableTracksViewportWidth() which was overrided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM