Domanda

Ho un widget di StyledText con i controlli Tabella incorporati. Tuttavia, una volta aggiungo un ascoltatore MeasureItem per regolare le dimensioni delle celle della tabella, le appare tabella nella parte superiore della StyledText il widget per la prima volta ho scorrere, rimanendovi fino al ho scorrere tutta la strada nella posizione prevista.

Si tratta di un problema noto? Sto solo facendo qualcosa di sbagliato?

Snippet di codice:

import org.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();
    }
}
È stato utile?

Soluzione

Non ho idea, perché stai facendo questo, ma comunque ..

La tabella non si trova opportunamente dopo la creazione, e "siede" a coordinate base (0, 0). Quindi bisogna individuare la tabella a posto giusto.

Aggiungi questa linea dopo styledText.setStyleRange(style); e funzionerà come avete bisogno.

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

E poi non è necessario, per avere questa linea, è possibile eliminarlo ..

table.setVisible(false); // *** prevents table from IMMEDIATELY jumping
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top