Pergunta

What I have: A split panel with a scroll panel in the right part. In this scroll panel, I have a JPanel. I want to have in this JPanel a series of others JPanels stacked one under the other one. I set the Layout to be a BoxLayout. Now it stacks multiple JPanels, but I have 2 problems: If my content from JPanel take less space then my frame, then will be lot of space between Jpanels. enter image description here

If my content from JPanel it's bigger then my frame, the Pannels will go over each other and my scroll from scrollPanel wont activate.

enter image description here

frame = new Frame();
splitPane = new SplitPane();
scrollPane = new ScrollPane();
frame.add(splitPane);
scrollPane.setViewportView(new Lesson());
splitPane.setRightComponent(scrollPane);
splitPane.setLeftComponent(new JTree());

Where Frame, SplitPane, ScrollPane() are classes that extends JFrame, JSplitPane, JScrollPane. Atm they only have a constructor, after it will work, I want to make some customization there.

public class Lesson extends JPanel {

  private static final long serialVersionUID = 1L;

  public Lesson() {
    customize();
    String text = "text from pictures";
    add(new Paragraph(text));
    add(new Paragraph(text));

  }

  private void customize() {
    BoxLayout boxLayout = new BoxLayout(this, BoxLayout.PAGE_AXIS);
    setLayout(boxLayout);
  }

}

public class Paragraph extends JPanel {

  private static final long serialVersionUID = 1L;

  public Paragraph(String text) {
    setLayout(new FlowLayout(FlowLayout.LEFT));
    setPreferredSize(new Dimension());
    StringTokenizer splitStringTokenizer = new StringTokenizer(text, " ");
    while(splitStringTokenizer.hasMoreTokens()){
      add(label(splitStringTokenizer.nextToken().toString()));
    }
  }

  private JLabel label(String string){
    JLabel jlabel= new JLabel(string);
    return jlabel;
  }

}

Any hints about how I can resolve this ? Ty in advance.

Foi útil?

Solução

A BoxLayout respects the maximum and minimum sizes of components added to it. You are using a FlowLayout o the Paragraph panel. The preferred size is always one line of components.

The panel will shrink until there is only one line displayed or grow to occupy all the space.

When there is more space the panels are allowed to grow.

Override the getMaximum/MinimumSize() of your Paragraph panel to return the preferred size.

The question is why are you using a panel of labels to display text. Why are you not using a text area.

Or another option may be to use the WrapLayout which will wrap components automatically and recalculate the preferred size based on the wrapping. You will still want to override the getMinimum/Maximum size calculations to return the preferred size.

I want later to add some mouse listener to some of jlabels.

Why? Again if you use a text area, you can add the MouseListener directly to the text area and then you can use the caret position (or convert the mouse position to an offset in the text area) to determine what word the mouse is over and then do your processing.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top