开发者

SWT StyledText - Table with MeasureItem listener jumps to top

开发者 https://www.devze.com 2023-01-31 11:11 出处:网络
I have a StyledText widget with embedded Table controls.However, once I add a MeasureItem listener to adjust the size of the table cells, the table appears at the top of the StyledText widget the firs

I have a StyledText widget with embedded Table controls. However, once I add a MeasureItem listener to adjust the size of the table cells, the table appears at the top of the StyledText widget the first time I scroll, remaining there until I scroll all the way to the expected location.

Is this a known issue? Am I just doing something wrong?

Code snippet:

import org开发者_JAVA百科.eclipse.swt.SWT;
import org.eclipse.swt.custom.PaintObjectEvent;
import org.eclipse.swt.custom.PaintObjectListener;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.GlyphMetrics;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class JumpingTable {

    public static void main(String[] args) {
        // Initialize GUI
        Display display = new Display();
        Shell shell = new Shell(display);

        // Create editor
        StyledText styledText = new StyledText(shell, SWT.V_SCROLL);
        shell.setLayout(new FillLayout());

        // Insert lots of text
        for (int i=0; i<100; ++i) {
            styledText.append("Lorem ipsum\r\n");
        }

        // Create table
        Table table = new Table(styledText, SWT.NONE);
        table.setVisible(false); // *** prevents table from IMMEDIATELY jumping
        for (int i=0; i<3; ++i) {
            TableColumn column = new TableColumn(table, SWT.NONE);
            column.setWidth(100);
        }
        for (int i=0; i<3; ++i) {
            TableItem row = new TableItem(table, SWT.NONE);
            row.setText(new String[] {"a", "b", "c"});
        }

        // Place table
        int tableOffset = styledText.getCharCount();
        styledText.append("\uFFFC");  // Object Replacement Code
        StyleRange style = new StyleRange();
        style.start = tableOffset;
        style.length = 1;
        style.data = table;
        //table.pack();
        table.setSize(300, 150);  // accomodate MeasureItem's sizing
        Rectangle rect = table.getBounds();
        int ascent = 2*rect.height/3;
        int descent = rect.height - ascent;
        style.metrics = new GlyphMetrics(ascent, descent, rect.width);
        styledText.setStyleRange(style);

        // Draw table
        styledText.addPaintObjectListener(new PaintObjectListener() {

            @Override
            public void paintObject(PaintObjectEvent event) {
                Table table = (Table) event.style.data;
                table.setVisible(true);
                int y = event.y + event.ascent - event.style.metrics.ascent;
                table.setLocation(event.x, y);
            }   
        });

        // Adjust sizing
        table.addListener(SWT.MeasureItem, new Listener() {

            @Override
            public void handleEvent(Event event) {
                if (event.type == SWT.MeasureItem) {
                    event.width = 100;
                    event.height = 50;
                }
            }   
        });

        // Display GUI
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }

        // Clean up
        display.dispose();
    }
}


I have no idea, why are you doing this, but anyway..

Your table isn't located appropriately after creation, and it "sits" on base coordinates (0, 0). So you have to locate the table to right place.

Add this line after styledText.setStyleRange(style); and it will work as you need.

table.setLocation(styledText.getLocationAtOffset(styledText.getCharCount()));

And then it's not necessary, to have this line, you can delete it..

table.setVisible(false); // *** prevents table from IMMEDIATELY jumping
0

精彩评论

暂无评论...
验证码 换一张
取 消