Question Details

No question body available.

Tags

java swing

Answers (1)

June 22, 2025 Score: 0 Rep: 442 Quality: Low Completeness: 60%

This is a classic issue in custom View implementations in Swing’s javax.swing.text framework. To fix this issue try to override layout(int width, int height) in MDListView If you don’t then the base CompositeView would not call your getChildAllocation() correctly.

@Override
protected void layout(int width, int height) {
    int n = getViewCount();
    if (n == 0) return;

int y = 0; for (int i = 0; i < n; i++) { View v = getView(i); int h = (int) v.getPreferredSpan(YAXIS); v.setSize(width, h); // Store the offset in the child allocation manually if needed childAllocations[i] = new Rectangle(0, y, width, h); // Optional cache y += h; } }

Implement getChildAllocation(int index, Shape a) like bellow

@Override
public Shape getChildAllocation(int index, Shape a) {
    if (a == null) return null;
    Rectangle alloc = a.getBounds();
    layout(alloc.width, alloc.height); // force layout

int y = 0; for (int i = 0; i < index; i++) { y += (int) getView(i).getPreferredSpan(YAXIS); } int h = (int) getView(index).getPreferredSpan(Y_AXIS); return new Rectangle(alloc.x, alloc.y + y, alloc.width, h); }

make sure setSize() is called on the root view

view.setSize(containerWidth, containerHeight);